Game object management

Started by
2 comments, last by GameDev.net 18 years, 8 months ago
Hi, I need a little help with my game object management. I am trying to create lists of cObjects, in a std::vector list - pObjectList->add( pObject) ; I can then say: pObjectList->render(); and my app will render all the items in the list. Unfortunately, I have become muddled due to my pointers I think. Heres my pseudo code: Cobject* pTempObject = new Cobject( device ); pTempObject->create( meshfilename, position, OBJECT_TYPE_LAND ); pObjectList->add( pTempObject, "Tank" ); pTempObject->create( 2ndMesh, position, OBJECT_TYPE_AIR ); pObjectList->add( pTempObject, "PLANE" ); I get two poiunters to objects in the vectorlist, but they are both pointers to the same thing (the address of the pTempObject, I imagine!). How can I do this correctly? Ideally I am creating an object, copying it into the list, creating another, and copying that one too without affecting the first. Should I not use pointers to Cobjects? Any help, much obliged! Thanks Simon
Advertisement
You are only creating 1 object Cobject* pTempObject = new Cobject( device );

But you are calling create on the same object twice. The pointer never changes. It sounds like you want to create 2 different objects, so you need to new another Cobject for your 2nd call to create.
try this:

Cobject* pTempObject = new Cobject( device );
pTempObject->create( meshfilename, position, OBJECT_TYPE_LAND );
pObjectList->add( pTempObject, "Tank" );

Cobject* pTempObject = new Cobject( device );
pTempObject->create( 2ndMesh, position, OBJECT_TYPE_AIR );
pObjectList->add( pTempObject, "PLANE" );

but remember to delete all pointers in your list before destroying list itself
ups redefinition :p
4th line should be:
pTempObject = new Cobject( device );

This topic is closed to new replies.

Advertisement