Best way for objects to keep track of objects.

Hi.

I was wondering, what is the best way to keep track of objects.

I want all GameObject to have access to a list of all the other GameObject on the game play layer.

Should I loop throuh all of the CCNodes on that layer and typecast to GameObject to see if the object is a GameObject, or is it slow to typecast stuff.

The other option would be that all GameObject, put a reference to themselves in a list, and that all GameObject have access to that list. The objects would then remove themselves for the list when removed from the scene. Only problem is, I don’t know what to use for this list.

in C# I would have used a List<> class. In c++, there is vector, but it is slow to remove stuff from the middle of the list. An array needs to be declared a fixed size and not so easy to remove stuff from middle.

Any Ideas for the most performing thing to do?

You can have a singleton class and have arrays (CCArray) to store those sprites.

Thanks for the quick reply.

I’ll look into the CCArray class.

Which singleton class are you referring to?

Create your own singleton (static) class, refer to CCDirector::shareDirector()

Ok. I see. You mean to put the CCArray* or objects in a singleton so that I can have access to it from everywhere.

I should probably have done it that way. Thanks for the tip.

I’ve put the CCArray of items in my gameplay layer. All objects have a reference to this layer it, so it’s just as accessible as if it where in a singleton. Then the Objects add themselves to the CCArray via their constructor, and remove themselves from their desctructor. This keeps an up to date CCArray to iterate through to perform collisions tests e.t.c…

You will still need to typecast if you’re using CCArray since CCArray::objectAtIndex* returns aCCObject**-type object.
This is needed especially if you subclassed**CCNode* or CCSprite and added unique methods and functions.