Avoiding Global Variables in games, how?

This is a discussion on Avoiding Global Variables in games, how? within the Game Programming forums, part of the General Programming Boards category; Hey all, What are some of your recommendations to avoid global variables in games? Obviously its not a good idea ...

  1. #1
    Unregistered
    Guest

    Avoiding Global Variables in games, how?

    Hey all,

    What are some of your recommendations to avoid global variables in games?

    Obviously its not a good idea to have a global variable for your enemies, and your items, and your maps and whatever else. But obviously many functions from many classes are going to be tinkering with these values.

    So what are some of the methods you use, to avoid using global variables, but allow each class that needs access to a certain value, have access to that value?

    Some people suggest wrapping the program up in a "global class" perse, but I don't quite understand what they mean. Anyone know about that?

    And what other methods do you use?

  2. #2
    Registered User Coder's Avatar
    Join Date
    Aug 2001
    Location
    Cairo, Egypt
    Posts
    128
    Whether one likes it or not, there have to be global variable(s).

    Wrapping the variables in a global application class moves the global variables from being global on the application level, to be global on the class level ( i.e. There will be one big class that represents your app, inside this class there will be a lot of another variables, that are global with respect to this class )

    The difference between both approaches - as far as I've seen - is that wrapping global variables in a class has a great advantage. You have all your variables in one place, together ( in the global class definition). They're not scattered around in a lot of header files, thus making it easier to understand what the application does and how it does it.

    However, some people take an approach that's a mix of both, they just place their global variables in some global.h header, as well as all the global functions. This achieves the same result from the the being-easier-to-understand aspect, but it doesn't use Object Oriented Programming ( and thus doesn't use its advantages). OOP makes life a lot easier actually, although it requires some more careful planning of objects and heirarchies.

    As an example, consider a simple 2D overhead strategy game, we'd have some global Application object, this class in turn contains a map object, a UnitList object, an AIEnemy object. The map object contains a tile object. The UnitList object contains Unit objects,...etc
    All these objects and there data ( member variables ) could've been global, yet making such a container-contained relationship makes code organization & maintainability a lot better

    CRTSGame
    ----CRTSMap
    --------CMapTile
    ----CUnitList
    --------CUnit
    ----CAIEnemy
    --------CUnitList
    ------------CUnit
    .
    .
    .
    Muhammad Haggag

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    >So what are some of the methods you use, to avoid using >global variables, but allow each class that needs access to a
    >certain value, have access to that value?

    Usage of global variables can be avoided by using interfaces. If a class A needs information of class B, it calls the interface of class B to get the required information. In most software architectures, classes have get-, set- and service-functions which are the interface-functions to be used by other classes.

    I don't know which language you're using, if you're using C and want to do OOP in C, then take very good care of your software design.

  4. #4
    Unregistered
    Guest
    Hey Coder,

    Could you give me an example (with code) of how to define a global class? Because I don't know how it would work exactly. It doesn't really make all that much sense to me.

    So could you perhaps show me an example?

  5. #5
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Just define your object before main or WinMain. I avoid globals using a wrapped up window class inside a app manager class that gives me the ability to access all my data when i need it.
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  6. #6
    Registered User Coder's Avatar
    Join Date
    Aug 2001
    Location
    Cairo, Egypt
    Posts
    128
    Could you give me an example (with code) of how to define a global class? Because I don't know how it would work exactly. It doesn't really make all that much sense to me.
    Code:
    // This is a win32 console app
    
    class CTestApp
    {
    // Constructor(s) & Destructor
    public:
        CTestApp()
        {
            m_iData = SomeInitialValue;
        }
    
        CTestApp( int iData )
        {
            if( iData < 100 )
            {
                throw SomeException;
            }
            m_iData = iData;
        }
        ~CTestApp()
        {}
    
    // Method(s)
    public:
        void SetData( int iNewData )
        {
            if( iData < 100 )
            {
                throw SomeException;
            }
            m_iData = iNewData;
        }
        int  GetData()
        {
            return m_iData;
        }
    
    // Data
    private:
        int m_iData;
    };
    
    CTestApp TestApp(5);
    
    int main()
    {
    //.
    //.
    //.
    return 0;
    }
    However, take special care for this
    Code:
    CTestApp( int iData )
        {
            if( iData < 100 )
            {
                throw SomeException;
            }
            m_iData = iData;
        }
    The given sample will pass it a value of 5, thus generating an exception. This exception will not be caught by anyone and will give you "An Abnormal program termination" error.
    Constructors of global objects are called before everything else, nothing can catch their exceptions.
    Last edited by Coder; 03-24-2002 at 06:39 AM.
    Muhammad Haggag

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  2. Use global variables or pointers?
    By RealityFusion in forum C++ Programming
    Replies: 5
    Last Post: 09-22-2005, 08:47 PM
  3. Global Variables in C++?
    By DeX in forum C++ Programming
    Replies: 9
    Last Post: 03-11-2005, 07:43 AM
  4. global variables in qbasic
    By Geo-Fry in forum Game Programming
    Replies: 10
    Last Post: 10-09-2003, 07:53 AM
  5. GLobal variables
    By fuh in forum C++ Programming
    Replies: 21
    Last Post: 01-01-2003, 02:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21