Global Variables

Started by
13 comments, last by szecs 12 years, 8 months ago
If I have some variables, that are used often, like pointer to graphic context, Display Window, resources reference, is ok to put all these inside a header file and include it in a precompiled header ?
Advertisement
ya I think you'll live :D

Stefano Casillo
TWITTER: [twitter]KunosStefano[/twitter]
AssettoCorsa - netKar PRO - Kunos Simulazioni

I used a class to store all of my global variables and I can just pass a pointer to that class to anywhere I need to access that data.



ya I think you'll live :D


what do you mean ? It's a really bad idea ? another way to do this ?
Well, nobody's going to come to your house and punch you in the face for using globals, but that doesn't mean you won't regret it at some point.

Some details on your code would be nice, so we can suggest specific alternatives.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Globals are very often problematic to design and can make maintenance a nightmare, but that said, they aren't always evil. Eventually someone is going to come in and suggest a Singleton, which sounds like a great idea but is generally just a global by a different name. As ApochPiQ said though, without further details... who knows.
There are many objects in a game that you only generally want one of within your game, and you need frequent access from multiple sources. Those are great candidates to for global access.

The problem is not that globally accessible objects are inherently bad. They happen all the time, and there are many good reasons for them.

The difficulty is using them such that they are always in a good state. You will need to consider how you intend to use them, and take steps to ensure you limit access to a way that integrates well with your design. If they can become invalid then obviously it is a problem. If it does not work well with your design then it is a problem.
I use ClanLib SDK.... Every entity object has CL_Sprite object that contains entity animation.... to create a CL_Sprite object i need to pass as parameters: GraphicContext, resources reference, and finally a string with the sprite name...

this->sprite = CL_Sprite(gc, name, &resources)

So for every entity I need to pass 3 parameters , and this is just a base class... And I'd like to reduce this to just one parameter: a string which contains the sprite name...

Was thinking maybe about a SpriteManager class, so I could pass the string, and it would return a CL_Sprite object...

But I'm not sure what is the best way. Am I going the wrong way ?


I use ClanLib SDK.... Every entity object has CL_Sprite object that contains entity animation.... to create a CL_Sprite object i need to pass as parameters: GraphicContext, resources reference, and finally a string with the sprite name...

this->sprite = CL_Sprite(gc, name, &resources)

So for every entity I need to pass 3 parameters , and this is just a base class... And I'd like to reduce this to just one parameter: a string which contains the sprite name...

Was thinking maybe about a SpriteManager class, so I could pass the string, and it would return a CL_Sprite object...

But I'm not sure what is the best way. Am I going the wrong way ?




A SpriteManager of sorts is most likely exactly what you want to do. From your example, I don't see your global use. If you are looking at storing name, resources or gc as a global, the answer is an emphatic, no to all of the above.



You however have come to your own answer, the SpriteManager and perhaps a GraphicsManager class as well ( that SpriteManager calls to get the context in the first place ). That said, it seems a bit like bad design that a sprite is so closely coupled to a GraphicsContext in the first place, but obviously thats not your library and there isn't much you can do about.



TL;DR, yes, make a SpriteManager class.
Use global functions operating on static variables.
Anthony Umfer

This topic is closed to new replies.

Advertisement