I’m working on figuring out C++ still. What I’ve been trying to do is make global instances of certain classes (the class controlling input stored in a global variable, the class controlling graphics stored in a global variable, the class controlling the overall game system stored in a variable, etc.) It’s been working fine so far. But now I need to give an instance of the input controller data from the system controller.
In my game.h file (the one that runs the main loop and a few other things), I have defined the system as such
“global gameSystem”
in game.cpp I can access a variable in there named “keyCodes”
In my input.cpp file, I’ve included game.h and global.h (it didn’t work with just game.h, so thought I’d try global.h too), and I can see the instance of gameSystem, but I cannot access it’s keyCodes variable.
Any idea how to fix this? This sort of thing worked for me when I programmed in AS3 and Ruby. I like how modular it keeps things. So I’d really like to do it the same way in this x.x
It is hard to guess exactly what you are doing. From my perspective I understand that you have a global variable in one file and want to access it in another. If that is the case you have to do the following (I’ll make an example with an
intbut any other type is the same.Create a
File1.hheader file where you tell the world that a globalintvariable, namedMyGlobalIntexists with the following line:This just announces that the variable exists. It does not declare the variable. To declare the variable you do so in a
File1.cpp:File1.cpp declares the variable. Now it can be used in any file that includes
File1.h. For instance inFile2.cpp:In you case, you should put
in file
game.h. Filegame.cppshould#include "game.h"and declare effectivelykeyCodes:Now, every file that includes “game.h” will be able to use
keyCodes.