Game Development Community

How to I spawn/create an object programmatically in C++?

by Jerry Gibson · in Torque Game Engine · 04/05/2003 (1:06 am) · 13 replies

Does anyone know how to spawn an object programmatically in C++, I've look at the forums and resources and there is no example on how to do this.

I've tried stepping though the code, and from the best I can tell it looks like it creates an object (ie. "ItemData::create(HealthKit);") and then that object is ran through through something like his.

WorldEditor * wEditor = static_cast(obj);
SceneObject * select = dynamic_cast(Sim::findObject(argv[2]));
mObjectList.pushBack(select);
deleteNotify(select);

About the author

Recent Threads


#1
04/05/2003 (11:59 pm)
My friend "Lucas Goodwin" (who is also a member of the GG community) he was able to figure it out. Thanks Lucas!

Here is the code to add an Item (ie. Health Kit at cords 300,300,300).

Item *pItem = new Item;
pItem = new Item;
ItemData *pHealthKit = dynamic_cast(Sim::findObject("HealthKit"));
pItem->onNewDataBlock( pHealthKit );
pItem->setPosition(Point3F(300,300,300));
if (pItem->registerObject())
{
delete pItem;
pItem = 0;
}

Here is code to remove a Item that was created from the function above.

if( pItem )
{
Con::printf("Deleting object %s", pItem->getShapeName());
pItem->deleteObject();
}

Remember to add the include file "item.h"

Also remember to remove all the Items you created before shutting the engine down or you will get an error.

-Jerry
#2
04/06/2003 (6:12 am)
O.K it is early here, so forgive if I am blind.

am I missing sometghing here

Item *pItem = new Item;
pItem = new Item;


that just does not look right to me...

-Ron
#3
04/06/2003 (7:08 am)
@Ron - The reason why that looks wrong is because it is. The code as written will leak.
#4
04/06/2003 (10:00 am)
Thanks for pointing that out.
Those first two lines should only be one line, and it should read like this.

Item *pItem = new Item;

But you mention that this will leak, I've tested this code below and it does seem to free the Item, and my ram allocated stays the same.

ConsoleFunction(test, void, 1, 1, "test();")
{
int x=0;
for (x=0; x<=35000; x++)
{
// Create Item
Item *pItem = new Item;
ItemData *pHealthKit = dynamic_cast(Sim::findObject("HealthKit"));
pItem->onNewDataBlock( pHealthKit );
pItem->setPosition(Point3F(300,300,300));
if (!pItem->registerObject())
{
delete pItem;
pItem = 0;
}

// Remove Item
if( pItem )
{
pItem->deleteObject();
}
}
}

Thanks!
Jerry
#5
04/06/2003 (10:07 am)
When an object is registered it becomes managed by the sim and deleted on by the sim. You can't instantiate this locally as it'll self destruct and wreak havoc.

deleteObject also contains "delete this".
#6
04/06/2003 (1:45 pm)
@Jerry - Under most modern operating systems leaks only last the duration of the runtime. And since the item object isn't that big the only way you'd really notice it is if you were running for an extended amount of time and allocating a lot of items with the double new code.

@Lucas - In this case the first item created never gets added to the simualtion and hence never gets deleted by it.
#7
04/06/2003 (5:11 pm)
As jerry said, he put a typo in the first code paste. It should have just been the first line, Item *pItem = new Item;

The second line, pItem = new Item; is a typo on his part. Nothing more. Shouldn't be there.
#8
04/06/2003 (5:21 pm)
That double new code (in the second post) was a typo by me.

So here is the correct code that seems to work fine for works for me.

To add an item,
// Create Item
Item *pItem = new Item;
ItemData *pHealthKit = dynamic_cast<ItemData*>(Sim::findObject("HealthKit"));
pItem->onNewDataBlock( pHealthKit );
pItem->setPosition(Point3F(300,300,300));
if (!pItem->registerObject())
	{
	delete pItem;
	pItem = 0;
	}

To remove the item that was created in the function above,
// Remove Item
if( pItem )
	{
	pItem->deleteObject();
	}

Thanks!
Jerry
#9
04/06/2003 (6:34 pm)
Very useful information. This could be used to create an easter egg of like a hidden level of some sort. :)
#10
04/06/2003 (8:28 pm)
Or to spawn nifty effects like particle explosions, jet fumes, and the splash effect.
#11
09/25/2003 (4:04 pm)
@Jerry

I'm calling the code you listed above from C code, and it is executing correctly, but no Item appears in the game. Any ideas as to what I may be missing?
#12
12/28/2005 (11:50 pm)
Anyone try this code in 1.4 yet?

I tried and got the script to add the item to the scene. The problem is the item appears on screen and even drops to the floor like it should but doesn't spin. (The datablock I'm using has rotate = true)

Any ideas why it won't spin?
#13
12/31/2005 (6:51 pm)
Hmm, does the datablock's unpack show it having rotate = true? What does the code inside the item's update loop think?