Advertisement

global gamestate variable

Started by January 02, 2003 11:24 AM
5 comments, last by Da cobra 21 years, 10 months ago
I''m trying to make a global gamestate variable as follows
  
globals.h
enum GameState {loadmenu, menu, loadoptions, options, loadhighscore, highscore, 
				loadgame, game, pause, kill, credits} ;
extern GameState g_GameState ;


main.cpp

#include "globals.h"
GameState g_GameState ;


loadmenu.cpp

#include "loadmenu.h"


loadmenu.h
#include "globals.h"

  
so I guess my g_GameState is also in my loadmenu.cpp, right? but when I want to change g_GameState=menu then I get these 2 errors : error C2065: ''g_GameState'' : undeclared identifier error C2065: ''menu'' : undeclared identifier so what did I do wrong? thanx in advance
You are declaring g_GameState in global name space in your main.cpp file.

You can use it in your globals.h file because you declared it using the extern keyword.

Simply use the extern keyword in your loadmenu.cpp file to use it.
Advertisement
but if I include "globals.h" in my loadmenu.cpp,which has the extern g_GameState,
isn't that the same as including extern g_GameState in my loadmenu.cpp?

edit : I included extern GameState g_GameState in my loadmenu.cpp and now I get 2 error messages which I don't understand

source :

  #include "loadmenu.h"#include "StdAfx.h"extern GameState g_GameState ;void loadmenu(){	g_GameState = menu ;} // end of loadmenu  


errors :

error C2146: syntax error : missing ';' before identifier 'g_GameState'
fatal error C1004: unexpected end of file found



[edited by - Da Cobra on January 3, 2003 5:15:39 AM]
I don''t use globals alot, but what I did last time I used them, it did the following:

extern int g_iGameState;#include "globals.h"int g_iGameState;#include "globals.h"#define MENU 100g_iGameState = MENU; 


That should work. You just declare it as extern in your globals file, and then declare it in just 1 file.

Hope this helped,

Sand Hawk

----------------
-Earth is 98% full. Please delete anybody you can.


My Site
----------------(Inspired by Pouya)
well I did just that, declare it in main.cpp
then put it as extern in my globals.h and then include globals.h everywhere, but he still doesn''t recognize it?!?
You must include stdafx.h before other headers everywhere you do include it. If you don''t, the compiler will ignore all of the includes above it.
Advertisement
When you use "enum" I think it creates one instance of the name (like structs). If you want to use it to create instances I think you need:

typedef enum tagGAMESTATE{blah, blah, blah} GAMESTATE, *LPGAMESTATE;

This allows you to declare the gamestate instance as a type:

GAMESTATE g_GameState;

In C++ this might not matter but it definately does in C.

>>>>>>>>>>>>>>>>>
Ilthigore
<<<<<<<<<<<<<<<<<
>>>>>>>>>>>>>>>>>Ilthigore<<<<<<<<<<<<<<<<<

This topic is closed to new replies.

Advertisement