Skip to main content
gamedev.net
🔒 Locked

Object management question

Started by King of Men Jan 18, 2005 at 9:19 PM 4 replies 350+ views
Original Post
King of Men
King of Men
Suppose my game has a bunch of foo objects, which need to be updated once every game cycle. Now I can think of two ways of doing this : The game engine can maintain an array / list / vector / whatever of foo, and iterate through it. Or the foo class can maintain a static array / whatever containing all active foo objects, and the game engine asks for the next one, or calls a static updateNext() method. Now my question is, is it the foo class's responsibility to know how many foos the game is using, as my second method requires? Conversely, why should the game have to maintain lists of all its classes? There might be bar, baz, whatever classes with similar problems. Opinions?
To win one hundred victories in one hundred battles is not the acme of skill. To subdue the enemy without fighting is the acme of skill.
SumDude
SumDude
You can use a manager to keep track. Or you could use the world to keep track as well. I think it would be much more easier to use the world or a manager than have the foos keep track. The foo should only keep track of itself, the one foo.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                                                          
Looking for video game music? Check out some of my samples at http://www.youtube.c...ser/cminortunes            
                                                          
I'm currently looking to create music for a project, if you are inte
leiavoia
leiavoia
I've used both methods.

Remember that the "internal static list of objects" technique works good if you dynamically allocate all the objects (Which i normally do anyway). If you don't, your constructors and destructors will mess up the list when you create copies or temporary objects on the stack. However, i do use this method for keeping track of small subsections of the global "object population". Specifically, i use them to iterate over my collision objects, with static functions for doing the collisions as well.

I also use the external "manager" technique. This is nice because you can "fire and forget" and let the manager take care of memory cleanup, updating, drawing, timing, and whatever else you can think of. like this

ObjectManager->RegisterObject( new Object(params) );

That is how i do it for my global object list.

Again, i think both techniques work well. However, if you go with the internal lists, i would recommend installing static functions to work through instead of a public list that anyone can manipulate.
kaysik
kaysik
I've been thinking about this recently and I came up with this: If you want all the foo's to know about each other and talk to each other then have a static array inside the foo class. If you don't want them to know about each other or want to keep mutiple lists then keep the array inside the world class. If you unsure then put it in the world class because as this is a bit safer (or a Manager like SumDude said)
paulecoyote
paulecoyote
Draw it and see how it looks. Sounds silly to those whom don't design, but if you have boxes and things on paper and have clear responsibilities assigned to your classes - it will jump out at you. First list responsibilities... then the classes you've identified, then assign them. If you have say Staff and Manager classes: "Manager hires and fires staff", "Staff actually do work", etc.

http://www.gnome.org/projects/dia/ is a freebie uml tool - though pen and paper will probably suit you just as well (or better).

There isn't really a general right answer to what you asked, just guidelines you can follow. You have to choose what's right for your own project. At the end of the day also don't get too worried about this, just try it and see what happens. Throw away prototyping is fine - esp if you've written things so you don't have to throw away much!
Anything posted is personal opinion which does not in anyway reflect or represent my employer. Any code and opinion is expressed “as is” and used at your own risk – it does not constitute a legal relationship of any kind.
King of Men
King of Men
Kaysik, that's an excellent point. In the particular case I was thinking of, the foo objects do indeed need to know about each other, in fact their interaction is a large part of my game logic. So a private static dynamically-updated list seems to be the way to go, yes.
To win one hundred victories in one hundred battles is not the acme of skill. To subdue the enemy without fighting is the acme of skill.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.