Jump to content
  • Advertisement
Sign in to follow this  
Sillencer

Global Variables

This topic is 2550 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 ?

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 ?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!