Sign in to follow this  
TroneX

Globals anyone?

Recommended Posts

Hi folks, in one of our projects, we used to implement a huuuuge struct containing pointers to almost everything such as the video subsystem, the audio subsystem, the script engine, etc, ... We created an instance of this struct in CGameMain, filled it with lots of data and called it m_pGD for Global Data. ;-) This member was passed to every subsystem, so that everyone had access to everything. Well. From my perspective today, this sounds like a really really bad thing to do. ;-) I heard of Singletons and that this is exactly their purpose. But: Isn't there any other way to improve the engine design? Regards, T.

Share this post


Link to post
Share on other sites
Quote:
Original post by TroneX
I heard of Singletons and that this is exactly their purpose.


Not really. The point of singletons is to control the construction order of globals, they don't magically make globals nice :)

Quote:
But: Isn't there any other way to improve the engine design?

Yep. Go though and work out what parts of your code *need* to access other parts, and by need I mean cannot get it's job done without it. You'll soon realised that there isn't as much as you think.

For example, consider your audio system. Does your renderer need it? Nope. How about input? Nope. AI? Nope. Physics? Nope. Game logic doesn't even need to know about it. You can setup triggers that your game-logic calls when certain events happen, but it doesn't need to know what those triggers do. So if that vast majority of your code doesn't need it, why is it global?

Share this post


Link to post
Share on other sites
Quote:
Original post by joanusdmentia
Not really. The point of singletons is to control the construction order of globals, they don't magically make globals nice :)

No, actually, that's not the purpose either :p The point of singletons is to ensure there is at maximum one instance of the object in existence at a time. Whether they correctly handle order of construction is specific to implementation.

Share this post


Link to post
Share on other sites
Quote:
Original post by Polymorphic OOP
No, actually, that's not the purpose either :p The point of singletons is to ensure there is at maximum one instance of the object in existence at a time. Whether they correctly handle order of construction is specific to implementation.

I've got a bad habit of skipping over the obvious so I didn't mention that (singleton). [smile]

I always thought all decent singleton implementations would define construction order, but oh well. I don't really use them much myself, it's not very often that you *need* to limit a class to only having one instantiation. An error log comes to mind, but not much else.

Share this post


Link to post
Share on other sites
If you find yourself using huge structs of globals, your design is not as good as it could be. Especially in large projects with lots of independent modules, changing one class or header can force a complete rebuild of the entire project - which is bad. Unfortunately, a lot of times once you realize you're overusing global data it is too late to change in any reasonable amount of effort.

Always plan and design your code as extensively as possible before you begin actual implementation. It can save a lot of ugliness later, and make it much easier for fresh blood to come in and understand how the system works. I'm currently working on a project that has a lot of global data sharing going on (sometimes, but not always, for good reasons) and it's taken quite a lot of effort to figure out what goes where and why things are organized the way they are. When it comes time to debug and maintain your code, a clear and well structured design can save huge amounts of valuable time.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this