Skip to main content
gamedev.net
🔒 Locked

static object

Started by bartiss Jan 26, 2007 at 3:34 AM 4 replies 950+ views
Original Post
bartiss
bartiss
Hi, I'm trying to add sound system to a quite big hierarchy tree of objects. I've created a cSoundSystem class and I want all the game objects to use it. My current approach is: 1. Create cSoundSytem object dynamically in the app class. 2. Set a global pointer to the sound system. 3. Use extern in the game root object to get the sound system pointer. Well, it works. But I don't like this solution. I know it's possible to make it like this: 1. Create single instance of cSoundSystem 2. Access it through cSoundSystem:: from any object knowing the type How do you do this? And am I right to try it?
Zakwayda
Zakwayda
Quote:
Original post by bartiss
How do you do this? And am I right to try it?
Many will tell you that singletons are nothing more than 'glorified globals', and should be avoided in most (perhaps all) cases. They can solve the problem of initialization order (or not, depending on how you implement them), but otherwise they have many of the same drawbacks as globals (strong coupling, excessive dependencies, etc.). Some even go so far as to call the singleton an antipattern.

A good question to ask yourself is, who really needs to know about (and have access to) the sound system? When an object is available as a singleton, it becomes all too easy to just grab the instance and do things 'where it's convenient' rather than where it 'should be done'. For example, game objects end up playing sounds directly via the manager rather than, say, sending a message that a sound should be played.

I'm not saying these problems are easy to solve, because they aren't! Also, I'm no software engineer, so take the above with a grain of salt. In any case, I'd recommend taking the AP's advice and doing a search on the term 'singleton', as there are many articles and forum threads on the topic to be found online.
bartiss
bartiss
Thanks! I think I'll just carry on with the global pointer ;-)
arudson
arudson
I would recommend reading up on them regardless, and trying them out for yourself. A little extra knowledge never hurts, and if you're planning on working professionally in the game industry, they *are* used at many studios. So even if you don't like them, or they don't work for you in this instance, at least you will know how they work, and what problem they solve.

Bottom-line is if you try it, and find you don't like it -- don't use it.

Cheers.
JohnBolton
JohnBolton
Ideally, since the App creates a cSoundSystem, it should not be global. Making it global allows the possibility of accessing it before it has been created.1 The App should pass a reference to any class it controls that might need to access the cSoundSystem. Classes external to the App would access the cSoundSystem through the App or query the App for a reference to the cSoundSystem.

In practical terms, such a system is can be very inconvenient. It results in lots of parameters and pointers, leading people to use globals -- giving up some correctness, robustness, and maintainability for the sake of convenience.

1 Singleton prevents this, but that feature is usually overlooked.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!

Topic Locked

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

Sign in to reply to this topic.