Thread: Global variables.. how bad?

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    83

    Global variables.. how bad?

    Hey. I'm making an RPG, and there are a lot of global variables. The mob database array, the spell database array, the item database array, and the player class. I'm wondering if theres away around these global variables, or if they're really not that bad. If you want to look at the source, go to http://prpg2.com.co.nr and download the source (make sure it's .64a). Thanks alot!

  2. #2
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Usually the presense of global variables means that the code is "harder" to read and mantain. The data is more "scatttered" and e.i. if you have a bug in your code it will be a lot harder to trace it down because you will have to look in every function that might use that data. This wouldnīt be the case if you had declared it locally or in a class. Encapsulation is the key here.

    If you have a good design structure of your project (app) a lot of global variabels could (should) be elliminated. My guess is that you donīt want to rewrite the entire project so my tip is
    • Global variables that are only used in one function could be replaced with a static variable
    • Are global varibales really necessary or could it be replaced with a local variable
    • Declare a local variable and pass them as a reference(to functions).


    Of course there are more ways of doing it but these are the ones that come to mine head as i am writing. But if you are really serious about "good programming" you should learn how to to program OOP (which c++ support of course).
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    161
    >I'm wondering if theres away around these global variables, or if they're really not that bad.

    With games, a few global variables usually make the project easier to maintain and the code more readable. Without globals, you end up passing your game state to every function and that can get tiring and messy very fast. You don't generally want a lot of globals though-- what I tend to do is make a wrapper (call it GameState) that holds pointers to all the game data. You can even use the singleton pattern if you don't really want global variables.


    > But if you are really serious about "good programming" you should learn how to to program OOP

    OOP is not the only method of "good programming" Sometimes, in fact, blindly following the OOP methods can lead to code bloat and convoluted relationships. C++ is my favorite language because it supports multiple paradigms-- it is easy to solve different problems with different approaches within one project.

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    83
    thanks a lot everyone, but what is a wrapper?

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    A class that manages all the resources.

    For example, this is my typical main() function for any and all games (I removed exception handling code because it's actually longer than the function itself -- that is the reason I have rval as it is):

    Code:
    int main(int argc, char *argv[])
    {
    	int rval;
    	Game myGame;
    	rval = myGame.Run();
    	return rval;
    }
    The Game class handles instantiating all the necessary game variables, display, sound, etc. in its constructor, frees them in its destructor, and Run() is the main game loop. There are no globals, only members of Game.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  6. #6
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    but be careful with using stack memory. It is often better to create something like Cat's Game class on the heap. On the home versions of windows like 95 and ME I've run into stack memory problems that were very difficult to find.

    On the Heap == dynamic
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    My Game classes have very little stack memory, typically 256 bytes or less

    The constructor allocates memory on the heap for big stuff. The class itself is mainly just pointers.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  8. #8
    Registered User
    Join Date
    Oct 2003
    Posts
    83
    I'm just beggining learning about OOP and classes. How is myGame available to the rest of the functions?

  9. #9
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Avoiding global data is only an optimization for the human coder. Down at the technical level, global data is much more efficiant than using locals and arguments, since they will be pushed/popped from the stack all the time.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  10. #10
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    "only an optimization for human..."

    not true. it is also a critical point of problems in multithreaded apps. Additionally, I've run time trials on stack memory vs global memory and the result is contrary to what you're saying. In many cases, stack memory is actually FASTER than global memory. Now modern compiler tricks may play a role but the old dinosaur method of doing things for speed purposes is no longer a speed-up.

    ammended:
    there is a slight speed-up, so I don't wish to portray it the way I did in this post. But the speed difference is virtually non-existant.

    Here's a pretty good test app for use as example:
    Code:
    #include <windows.h>
    #include <iostream>
    
    int GlobalArray[2];
    int GlobalSwap;
    
    int main(void)
    {
         int *HeapArray = new int[2];
         int *HeapSwap = new int;
         int LocalArray[2];
         int LocalSwap;
         int start, end;
         int i;
    
         start = ::GetTickCount();
         for(i=0; i < 100000000; i++)
         {
              LocalSwap = LocalArray[0];
              LocalArray[0] = LocalArray[1];
              LocalArray[1] = LocalSwap;
         }
         end = ::GetTickCount();
    
         std::cout << "Local trial -> " << (int)(end-start) << " milliseconds" << std::endl;
    
         start = ::GetTickCount();
         for(i=0; i < 100000000; i++)
         {
              *HeapSwap = HeapArray[0];
              HeapArray[0] = HeapArray[1];
              HeapArray[1] = *HeapSwap;
         }
         end = ::GetTickCount();
    
         std::cout << "Heap trial -> " << (int)(end-start) << " milliseconds" << std::endl;
    
         start = ::GetTickCount();
         for(i=0; i < 100000000; i++)
         {
              GlobalSwap = GlobalArray[0];
              GlobalArray[0] = GlobalArray[1];
              GlobalArray[1] = GlobalSwap;
         }
         end = ::GetTickCount();
    
         std::cout << "Global trial -> " << (int)(end-start) << " milliseconds" << std::endl;
    
         delete [] HeapArray;
         delete HeapSwap;
         return 0;
    }
    So the risk you present to multi-threaded apps and multi-instance code FAR outweighs any gains you get in using globals.
    Last edited by FillYourBrain; 11-24-2003 at 12:34 PM.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  11. #11
    Registered User
    Join Date
    Oct 2003
    Posts
    83
    Could you guys do me a favor? Could you criticize my code so I can make it better? It's my first program, I really need your help. I know it's not done, just look at what I have. One thing I noticed that was bad.. PLAYER takes up 1300 bytes or so.

    PS: Dont criticize the game; criticize my code so far. I know I need more races, commands, etc. Thanks!

  12. #12
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    winzip says your zip is not a valid archive. Sorry. Can't critique your code yet.

    Did you ftp it in ascii mode by chance?
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  13. #13
    Registered User
    Join Date
    Oct 2003
    Posts
    83
    Ah, I fixed that. WinRAR can't make WinZipable ZIP files. I made a WinZip compatable Zip file now. I also made a few improvements, which make it kindof confusing. But with this, PLAYER is now 300 byes or so instead of 1000 bytes or so. Also, criitique my remarking. I know, it's pretty bad. Tell me how I can improve upon it.

    http://prpg2.com.co.nr

    (push refresh and try again; especially if it's near to this post.. I screwed up again)
    Last edited by punkrockguy318; 11-25-2003 at 04:07 PM.

  14. #14
    Registered User
    Join Date
    Oct 2003
    Posts
    83
    It's all ready to be downloaded; I got it fixed

  15. #15
    Registered User
    Join Date
    Oct 2003
    Posts
    83
    bump bump bump

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 08-06-2008, 09:59 AM
  2. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  3. global variables in qbasic
    By Geo-Fry in forum Game Programming
    Replies: 10
    Last Post: 10-09-2003, 07:53 AM
  4. global variables - okay sometimes...?
    By MadHatter in forum C++ Programming
    Replies: 21
    Last Post: 01-21-2003, 04:23 PM
  5. C global variables
    By Dohojar in forum C Programming
    Replies: 18
    Last Post: 02-25-2002, 12:00 PM