Jump to content
  • Advertisement
Sign in to follow this  
nuclear123

Global Variables in games?

This topic is 2481 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

According to alot of people they say global variables in a program is a sign of bad programming. My question is the following:
"Why do so many popular game companies perform this so called "Bad programming" by creating most of their MAIN classes/structs/variables as globals in their program?"

are there certain situations where you cannot get away from making a global variable?


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Global classses/points also are VERY easy to alter for hackers. This is because their values get initialized at runtime before the actual entrypoint of the program( for example, a global classes VTABLE/pointers get initialized before winmain()/main(). Therefore a hacker could change their values in memory once the program has loaded up and actually entered its main entrypoint of winmain()/main(),) Hackers can almost garauntee these pointers will not change ( especially for a vtable ) and can avoid having to worry about the life of the variable and it getting reallocated and reinitialized at runtime therefore needing to keep up with the variable and reupdating their pointers they want altered every time the class comes back into scope.

ANYWAYS my main question is...Couldn't Game Companies Avoid alot of hacking problems of the .data section by getting rid of all global variables. This would prevent the hackers from easily taking advantage of the unscannable .data section and could continue monitoring the .code section to watch for unknown binary code that could be altering initialization of certain classes.

Share this post


Link to post
Share on other sites
Advertisement
You _can_ get away from global variables, but it doesn't solve the hacker problem.

First of all, it's not only global variables that end up in that .data section. Any static variable will as well. Also, even if you have no static/global variables, almost every program will have some big central datastructure that will be allocated/initialized at an early point of execution. The pointer to that datastructure will almost certainly end up on the same offset on the stack each time, making it just as easy to follow and manipulate. DRM systems usually fix this problem (in theory) by encrypting the executables and decrypting them on startup, checking for loaders/debuggers while doing that. There are still ways to get around that obviously, but it's hard.

People use globals and static member variables because they make life easier sometimes, especially quick fixes and dirty hacks needed when a deadline is closing in.

Share this post


Link to post
Share on other sites

According to alot of people they say global variables in a program is a sign of bad programming. My question is the following:
"Why do so many popular game companies perform this so called "Bad programming" by creating most of their MAIN classes/structs/variables as globals in their program?"

are there certain situations where you cannot get away from making a global variable?


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Global classses/points also are VERY easy to alter for hackers. This is because their values get initialized at runtime before the actual entrypoint of the program( for example, a global classes VTABLE/pointers get initialized before winmain()/main(). Therefore a hacker could change their values in memory once the program has loaded up and actually entered its main entrypoint of winmain()/main(),) Hackers can almost garauntee these pointers will not change ( especially for a vtable ) and can avoid having to worry about the life of the variable and it getting reallocated and reinitialized at runtime therefore needing to keep up with the variable and reupdating their pointers they want altered every time the class comes back into scope.

ANYWAYS my main question is...Couldn't Game Companies Avoid alot of hacking problems of the .data section by getting rid of all global variables. This would prevent the hackers from easily taking advantage of the unscannable .data section and could continue monitoring the .code section to watch for unknown binary code that could be altering initialization of certain classes.


Security through obscurity is proven not to work, ever.

Even if you don't use globals you will normally create some key structures on the stack very early in the program and they won't be deallocated until the program exits. (in general reallocation is expensive and thus not something you want to do just to throw hackers off).

Globally mutable state should be avoided to improve code clarity and maintainability. constants, pure functions, etc are perfectly fine to have globally accessible (In an appropriate namespace when the language supports it).

If you want to stop cheaters the best solution is to never trust the client (If its a singleplayer game you shouldn't care if someone hacks the game in order to cheat), if you want to reduce piracy then stop worrying about the pirates and focus on giving your customers a product they want to pay for, if the game uses a service you control you can restrict access to that service easily anyway.

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!