Jump to content
  • Advertisement
Sign in to follow this  
ukdeveloper

How to use extern properly

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

Hi, Here's a theoretical problem, based on one of my own. You have a boolean variable called Running, declared globally at the top of main.cpp. You create a separate sourcefile called gamestate.cpp, and it needs to use Running's value to determine whether or not the game is still running, and to act accordingly. Running is true when the game is running, false when it isn't - simple enough. In main.cpp, it's

while(Running)
{

// Game functions called in here

}

// If running is false, it breaks the loop and return 0 is called - exit

The problem is, gamestate.cpp can't use Running because it's declared in main.cpp (gamestate.cpp has a function whereby pressing Escape causes Running to be false, and to end the program). I can think of two ugly hacks around such problems: 1) Pile all the global variables into a header file included in every .cpp file 2) Pile absolutely everything into one source file. I tried passing in Running as a reference, but VS2005 didn't like that at all. I tried using extern, declaring Running globally as
extern bool Running;
, and got compiler errors. Running was an undeclared identifier in gamestate.cpp, even though it was declared as an extern. So, how do I use extern properly to solve such problems? Or are my two ugly hacks the only way of doing it? I'd like to be able to cut my source down into separate files and the functions in each one be able to communicate with each other, without undefined variable errors. Thanks in advance, ukdeveloper.

Share this post


Link to post
Share on other sites
Advertisement
You have a global variable. You might as well admit it and move on with your life. In a header put extern bool Running; and in your main.cpp put bool Running; Then source files including the header should be able to access the variable.

Share this post


Link to post
Share on other sites
Does anything else besides GameState and main() use Running? What's stopping you from putting it in GameState as a data member, then exposing that via a bool IsRunning() accessor member function?

:stylin: "Make games, not war."

Share this post


Link to post
Share on other sites
Quote:
Original post by ukdeveloper
1) Pile all the global variables into a header file included in every .cpp file


That won't actually work, you will have multiple variables named "running" but they won't necessarily have the same value.

Do what SiCrane said.

Share this post


Link to post
Share on other sites
I agree with stylin on this one.. Using an accessor would also make it easy to add synchronization if you are doing multithreading.

If you really want to use extern, I always put the extern in the header file of the source it is declared in. So, if your global variable comes from Engine.cpp, than Engine.h would contain the extern.
Most of the time, you would include this header anyway if you want to use anything related to Engine, including the global.

Share this post


Link to post
Share on other sites
Quote:
Original post by stylin
Does anything else besides GameState and main() use Running? What's stopping you from putting it in GameState as a data member, then exposing that via a bool IsRunning() accessor member function?

:stylin: "Make games, not war."


So you say I could make GameState into some kind of class? Then I could use the accessors to check the value of the bool variable?

Share this post


Link to post
Share on other sites
Quote:
Original post by ukdeveloper
So you say I could make GameState into some kind of class? Then I could use the accessors to check the value of the bool variable?

That's definitely an option:

// game.h
class GameState {
private:
bool isRunning;
..
public:
bool IsRunning() const { return isRunning; };
void Update();
};

Then in your main loop, call GameState::IsRunning() to see if you should quit or update gamestate.

:stylin: "Make games, not war."

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!