using globals? the verdict

Started by
1 comment, last by Ferinorius 22 years, 4 months ago
Ok, it seems like a battle to me. Books on good coding methods say that globals are bad news, but books about game development say that globals are good, and promote better speed. I think that there is no other way for some of my stuff to work without globals, namely for the players in my rpg. many different aspects of the game use, or change the player''s hp or money stats. Should the player be defined as globals, and everything else be kept secret? Or should there be access functions that have to be used to change the vars, and only those. Are globals good or bad? Neo-Toshi - The City That Never Sleeps
Advertisement
In general, you want to avoid globals. They add a lot of mess to your program, and increase the interdepence of different parts of your code, making it more difficult to re-use code later on.

There are lots of things you could do to encapsulate your player data so it didn''t have to be global. You could put it all in a C++ class that included the functions to operate on the player data. You could keep it in a singleton object -- only one step removed from being global data. Or make yourself some kind of object cache or some simple functions that allows you to store and retrieve structs through global function calls. *Something*.

Globals are not always bad. Sometimes it really isn''t practical to hide or wrap up some piece of data -- just about all the Windows programs I''ve seen have at least got a global g_hInstance floating around. But I wouldn''t put too much creedence into those kind of books that tell you that globals are good because they''re fast. It isn''t necessary to fuss with tricks like that to optimize your program. If you want your program to run fast, focus on good design and optimization on your algorithms and processes rather than on calling conventions, scope tricks, and byte alignment. Focus on good coding practices, because a well-organized and maintainable program is going to be easier to optimize and extend in the long run than some hack you put together based on some bad advice in a game coding book. You will save a ton of time for yourself in the long run by doing things cleanly, and you''ll have more time to make your game fast once it works at all.

Also, eliminating globals and abstracting stuff it not always about keeping things "secret" as you put it. Encapsulation gives you a layer in which you can change things around without having to propogate those changes throughout your code. For example, you''ll want to write functions to modify your players'' HP and money rather than modifying global data directly, because that will make it easier for you to change the way damage is dealt later on, or to add cheat codes or spell effects that make your players invulnerable. The changes can be made in one place - the function that deals damage - rather than everywhere in your code where you might otherwise modify the player data directly.

Don''t forget that you can make global functions and variables whose scope is limited to a single .cpp file by using the static keyword. It''s a useful way to have globals when you need them that don''t *act* like globals throughout your project.
Thanks for that reply, it helps a lot. Ive been reading a lot of Code Complete lately, and I am trying to redesign my code so that it is much easier to use, change, and read. I think I am getting the picture.




Neo-Toshi - The City That Never Sleeps

This topic is closed to new replies.

Advertisement