gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > object management code

#29320 - greyboy - Wed Nov 17, 2004 9:37 pm

I wasn't sure whether to put this under beginner, coding, or C, so I put it here, since you all are obviously the smartest bunch (shh don't tell the others). :D

Anyhow, I'm wondering about people's opinions on which is better. (For a project in C) Is it better to have an object manager in which everyone calls CreateObject( OBJ_TYPE_BOMB, etc ) which then calls the corresponding create functions for the bomb, for instance. This obj mgr would have fn ptrs to create/update/destroy. Or is it better to have people calling CreateBomb() which would allocate the bomb and register the ptr to the base object data with the obj manager? The base obj of course would be the first element in any "derived" objects. It sounds like the latter is better, but I thought I had seen the former done.

Or perhaps both these approaches are lame and you have a better suggestion? I just have trouble figuring the advantages of the former approach. I figure as long as the derived objects register themselves with the obj manager and the mgr has ptrs to the destroy and update fns it should be ok.

Can someone advise, or even point out areas I have completely neglected that I need to flag?

Thanks

#29323 - poslundc - Wed Nov 17, 2004 9:55 pm

There are a couple of ambiguities in the terminology you're using that lend to multiple interpretations of how you would go about implementing these two systems. I'm inclined to tell you it doesn't matter very much anyway; you should do what seems most appropriate for your particular project. Try to strike a balance between robustness (having an abstract object manager) and specialization (your object-specific wrapper functions). When in doubt, I tend to lean towards specialization and recode and increase robustness as I deem it necessary, but this is a highly personal thing.

Dan.

#29324 - sajiimori - Wed Nov 17, 2004 10:00 pm

The latter method is superior because the base classes don't have to know anything about the specialized classes, which results in a clean top-down design. Looking down the heirarchy is almost the definition of "hack".

#29327 - Touchstone - Wed Nov 17, 2004 11:06 pm

As I see it CreateObject(OBJ_TYPE_BOMB) is simply a wrapper for CreateBomb(), which can be very usefull.

If you want your game to be content driven you would need to wrap all "create" functions anyhow. For example, in your level data you might have a list of objects that should live in the level. Having that wrapper let you have the object list simply as a list of OBJ_TYPE bytes for example, and iterate through the entire list and do CreateObject(object type n from level data).

For stuff that you are going to implement by code you would probably want to use the CreateBomb() directly.
_________________
You can't beat our meat

#29330 - sajiimori - Thu Nov 18, 2004 12:04 am

Quote:
As I see it CreateObject(OBJ_TYPE_BOMB) is simply a wrapper for CreateBomb(), which can be very usefull.
Yes. When used for a scripting module it ceases to be a hack. That's because the scripting module is often the most abstract component of the entire program, and nothing ever calls "up" to it -- it delegates work to everybody else, so there's still a clean hierarchy.