Thread: Best way to avoid using global variables

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    52

    Best way to avoid using global variables

    Hi,

    I'm working on a pacman clone in C++ and I'm using GLUT for the graphics. While I was coding the game, I was using global variables to pointers to objects pacman which is of type Pacman, and 4 pointers to objects of type Ghost. I know that it is bad practice to use global variables but I haven't been able to think of a better way to structure my program yet and I'm looking for ideas.

    I have a driver called Manager.cpp which contains the main function as follows:
    Code:
    int main(int argc, char **argv) {
    	glutInit(&argc, argv);            // initialize GLUT
    	glutInitDisplayMode(GLUT_RGBA);   // set RGBA display mode
    	glutInitWindowPosition(325, 0);
    	glutInitWindowSize(600, 600);
    	glutCreateWindow("Pacman");
    	init();
    	srand((unsigned)time(0)); //for random number generation
    	Game game = new Game();
    	glutDisplayFunc(displayScene); // set the rendering function
    	glutTimerFunc(0,Timer,0);
    	glutSpecialFunc(arrow_keys);
    	glutIdleFunc(update);
    	glutMainLoop(); // process events and wait until exit
    	return 0;
    This file (Manager.cpp) is the one which contains the global variables mentioned above and the global variables are being used in the callback function listed in main (i.e. displayScene, arrow_keys, etc).

    I want to continue to have access to single instances of the 5 pointers to the characters that are currently assigned to global variables but I'm not sure what the best way to do this is.

    Notice the line 'Game game = new Game();' in main(), I added this recently because I thought that a good way to do what I want would be to create another class, Game, which has 5 static pointers to my 5 characters as members. Would this be a clean way of solving my problem? Even if I could do it this way, how can I make the variable game available to all the functions in Manager.cpp (e.g. displayScene, arrow_keys, etc) if I don't want to make it a global variable?

    I hope my question is clear, if not please ask for any clarification that might help. Thanks!

    C

  2. #2
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    in other words, you want to have global access to something that's not a global variable.
    Sounds like contradictory requirements to me.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    52
    Yeah I realized that... What I ended up doing was I created a class Game which has pointers to all the characters as members and I have one global variable of type Game in my driver which I use to access the characters I need. This was the best I could come up with.

    Any suggestions are still appreciated.

    Would any say that having a global variable can be justified in this case?

    Cheers,

    C

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    32
    I had never seen GLUT so I just went and checked out the API. I fully expected to see at least a userdef void* somewhere in there that you could set that would get passed back to your registered callbacks. Not a one, nothing.

    I'd say it's justified. As if you really needed justification.

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Having five global variables isn't bad practice by itself. The more important thing is for your functions not to have many side-effects caused by your global variables. An example:

    Code:
    move(int x, int y)
    {
         ...
         myPacman.X = x;
         ....
    }
    
    movePacman(int x, int y)
    {
         ...
         myPacman.X = x;
         ....
    }
    
    movePacman(int x, int y, Pacman& pacman)
    {
         ...
         pacman.X = x;
         ....
    }
    Now, the first is bad. You don't know what it does. You have to see the whole function and see that a global variable myPacman is changed.
    The second has a good name, but what if you change the global variable myPacman? What if you want to give it to somebody else that makes a similar program? It is dependent from global variables.
    The third function is what you should have. Lets say you want to make a multiplayer pacman and want to move an array of pacmans. You would make a loop and pass every pacman to movePacman that accepts a parameter. Otherwise you would need a movePacman1, movePacamn2 etc etc if you used globals.

    In my example you could declare Pacman in main. But if you cannot do that and have it global that doesn't mean that you have to use them and make every function have a side-effect like above.

    What you do with Game game = new Game() seems nice as an idea. Why don't you do:
    Code:
    Game* game;
    int main()
    {
        game = new Game();
    }
    that way you have one global pointer. Only one. So you only have to worry about one variable when changing your code. For example if you want to restart your game, you can do game = new Game() and start from the beginning with a new game. That is not bad practice.

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Read about Singletons.
    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.

  7. #7
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    Quote Originally Posted by Magos View Post
    Read about Singletons.
    a glorified global variable IMO.

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    52
    Thank you everyone for your ideas. I ended up doing as C_ntua suggested which is to simply maintain a single global variable which is a pointer to game.

    Magos, I know about Singletons and actually tried to implement class Game as a Singleton before but I found it was making my code look unnecessarily complicated.

    Thanks again for the input!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 08-06-2008, 09:59 AM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. Use global variables or pointers?
    By RealityFusion in forum C++ Programming
    Replies: 5
    Last Post: 09-22-2005, 08:47 PM
  4. General global bool variables?
    By Bajanine in forum Windows Programming
    Replies: 3
    Last Post: 02-27-2005, 09:16 PM
  5. Avoiding Global Variables in games, how?
    By Unregistered in forum Game Programming
    Replies: 5
    Last Post: 03-24-2002, 07:33 AM