Globals In Games

Started by
4 comments, last by VladR 19 years, 7 months ago
Greetings Up until now I have made a "Globals Namespace" where I would just stick all my globlas in. When I needed them I would just go Globals::some_var. Is this right or is there some better way to manage globals in a game. Thanks.
--------------------C++ Home - Check it out!Lol... - Amazing video
Advertisement
Collecting a bunch of unrelated variables into one place doesn't make them more organized. Collecting global variables all into one place doesn't make them less global. Generally, a global variable should go into the module that it controls or is controlled by (if such a module exists).
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Quote:Original post by JohnBolton
Collecting a bunch of unrelated variables into one place doesn't make them more organized. Collecting global variables all into one place doesn't make them less global. Generally, a global variable should go into the module that it controls or is controlled by (if such a module exists).


I just want to add that having all the globals in a single namespace may limit namespace polution. But JohnBolton is right: this do not change anything about globals :)

Some people may want to add all their global into a single file - the major reason, as they say, is to avoid redundancy. It do not work. With time and file groth, it do not avoid redundancy and is make things harder to read. The solution I finnaly used for embedded C code is to define my vars at the beggining of my C files, with some prefixes:
static int s_my_var_name; /* <-- static, local to the current file */int g_module_my_var_name; /* ^-- global, defined in module.c and declared in module.h */


HTH,
Typically, your global vars would be declared in the name space (or file) to which they relate.

So for example, extern CGameEngine* g_gameEngine would be declared in the file that defines the CGameEngine class, and defined in the file that defines the class.

Thats how I do it, and how the majority of people I know do it (but I do admit, I dont know every programmer in the world!)

The method of declaring all your globals in one place, while it might seem tidy, is quite difficult to follow. It doesnt make sense, to me, to have a list of _unrelated_ globals listed in a seperate file.

There might be a case of declaring the global in the main header file, but that again could be changed, as the code would need access to the file declaring the class, so again, why not put it in there?

Just my point of view... Hope it helps
Spree
Hi!

We're working on a game engine now where there's no single global variable:) Even engine object is not global, so you could potentially create 2 or more engine objects at the time and it would work.

If you really need some globals, then try to minimize them to stuff like: engine object, main window etc.

Cheers!
Maciej Sawitus
my blog | my games
But it is easier to have for example boolean variables that affect the run-time of the engine to have at one place, since you don`t have to track them down into constructors of relevant class. This way you don`t search within 5 files, but just one.

For example, I want to start the engine with following stuff on/off:
Rendering Grid : Off
Rendering Enemies : ON
Shaders : ON
Vertex Decompression : ON
Decompression Type : 02
Sound Init : ON
Music : Off
Imagine I had to search through constructors of all these different classes and set relevant variables there! It would take me ages and I might also forget the last item i wanted to change! This way the setup is done in 5 seconds and I always clearly see the startup parameters correctly.


It`s easier this way than editing some txt file, which is inconvenient inside Visual C++ IDE. True, some things is better to have in TXT file, but what if you don`t want other users of your game to have the same level of control as you have over the run of game ? Then, such organized place of seemingly unrelated variables is a better alternative.
Then again, it all depends on your needs.

VladR My 3rd person action RPG on GreenLight: http://steamcommunity.com/sharedfiles/filedetails/?id=92951596

This topic is closed to new replies.

Advertisement