• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Header logic help

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

8 replies to this topic

### #1Jossos  Members

Posted 10 January 2013 - 03:43 AM

So I have a class Gamestate:

class GameState
{
public:
virtual void HandleEvents() = 0;
virtual void logic() = 0;
virtual void render() = 0;
virtual ~GameState(){};

void SetNextState(GameStates newState);	// State status manager
void ChangeState();		// State changer

private:
};

With the functions:

void GameState::ChangeState()
{
if (nextState != STATE_NULL)
{
if (nextState != STATE_EXIT)
delete currentState;

switch (nextState)
{
case STATE_INTRO: break;
case STATE_MENU: currentState = new MainMenu(); break;
case STATE_GAME: currentState = new Game(32, 32); break;
}

nextState = STATE_NULL;
}
}

void GameState::SetNextState(GameStates newState)
{
nextState = newState;
}

Things to note here are MainMenu, and Game - each in their own header file. This is a problem because MainMenu and Game inherit from GameState:

class Game : public GameState
{
//Game stuff...
};

class MainMenu : public GameState
{
};

Gamestate needs to be able to create a new instance of MainMenu, Game, or watever other states I add, while these objects need to inherit from GameState.

See, these header files need to reference each other. Of course, this causes an infinite loop, so how do i fix this?

Help is greatly appreciated!

Edited by Jossos, 10 January 2013 - 03:44 AM.

### #2Ashaman73  Members

Posted 10 January 2013 - 04:00 AM

To solve the header problem, independently of this is good or bad coding style, you just need to forward declare the class instead of including its header. In this case you can use it in the header files, as long as you don't access it there (e.g. in inline code):

class MainMenu;
class Game;

class GameState
{
public:
virtual void HandleEvents() = 0;
virtual void logic() = 0;
virtual void render() = 0;
virtual ~GameState(){};
// new=>

void SetNextState(GameStates newState); // State status manager
void ChangeState(); // State changer

private:
};

But this is not the problem here, you can just include all headers in your cpp like this:

#include "GameState"
#include "Game"

void GameState::ChangeState()
{
if (nextState != STATE_NULL)
{
if (nextState != STATE_EXIT)
delete currentState;

switch (nextState)
{
case STATE_INTRO: break;
case STATE_MENU: currentState = new MainMenu(); break;
case STATE_GAME: currentState = new Game(32, 32); break;
}

nextState = STATE_NULL;
}
}

void GameState::SetNextState(GameStates newState)
{
nextState = newState;
}

Game.h
---------
include "GameState.h"

Game.cpp
------------
include "Game.h"

---------
include "GameState.h"

------------

GameState.h
---------
no include

GameState.cpp
------------
include "GameState.h"
include "Game.h"



Edited by Ashaman73, 10 January 2013 - 04:06 AM.

Ashaman

### #3EWClay  Members

Posted 10 January 2013 - 04:05 AM

I don't see where the GameState header needs to reference MainMenu and Game. The cpp file does, but that's no problem.

That said, I wouldn't derive MainMenu and Game from GameState. It clearly does things that aren't relevant to the other classes. Perhaps all three should derive from a common base class.

### #4Nusakan  Members

Posted 10 January 2013 - 04:55 AM

Although it is possible to forward include like Ashaman said, wouldn't be better to have a state manager class that handles all these state changes, rather then changing the state in game state and main menu state? That way you would eliminate forward including, and IMHO it would be much easier to manage.

### #5Jossos  Members

Posted 10 January 2013 - 05:02 AM

to Ashaman, thanks a heap! that fixed my problems, and I will use this technique for the rest of my life.

to EWClay, my main game loop looks like this:

while (stateID != STATE_EXIT)
{
// Events
currentState->HandleEvents();

currentState->ChangeState();

// Logic
currentState->logic();

// Rendering
currentState->render();

drawCursor();

SDL_GL_SwapBuffers();
}

If they all derive from gamestate, I can just change the state, and the loop will continue normally. (A state machine I think they call this?)

to Nusakan, I have NO IDEA how to make a class like that. The idea of "Manager" classes are too abstract for me, but if you want to show me how you would write it, I would really appreciate it, and implement it for sure.

### #6Nusakan  Members

Posted 10 January 2013 - 05:42 AM

to Nusakan, I have NO IDEA how to make a class like that. The idea of "Manager" classes are too abstract for me, but if you want to show me how you would write it, I would really appreciate it, and implement it for sure.

I would suggest this website. it explains clearly how to do this in a simple manner. http://gamedevgeek.com/tutorials/managing-game-states-in-c/

### #7Karsten_  Members

Posted 10 January 2013 - 05:50 AM

When teaching students header management, I always go through a few rules.

Always forward declare rather than including a header to a class unless...

1) The class inherits from the other class.
2) The class contains the other class (Not a pointer!)
3) The class contains (including a pointer) something from the standard library. (i.e std::string, std::string*)

This should get round the looping issue. Especially if you remember that two classes can never contain one another. One will have to only contain a pointer to the other (rule 2).

Also... beware of smart pointers. The deleter function in it will need more than just a forward declare. In this case make sure the class containing the smart pointer contains a destructor and the full header is included in the .cpp file. (A bit messy, but thats life...)

Edited by Karsten_, 10 January 2013 - 05:52 AM.

http://tinyurl.com/shewonyay - Thanks so much for those who voted on my GF's Competition Cosplay Entry for Cosplayzine. She won! I owe you all beers

Mutiny - Open-source C++ Unity re-implementation.
Defile of Eden 2 - FreeBSD and OpenBSD binaries of our latest game.

### #8EWClay  Members

Posted 10 January 2013 - 06:57 AM

to Ashaman, thanks a heap! that fixed my problems, and I will use this technique for the rest of my life.

to EWClay, my main game loop looks like this:

while (stateID != STATE_EXIT)
{
// Events
currentState->HandleEvents();

currentState->ChangeState();

// Logic
currentState->logic();

// Rendering
currentState->render();

drawCursor();

SDL_GL_SwapBuffers();
}

Sorry if I'm completely misunderstanding, but is currentState not a member of GameState? If not, why is ChangeState() a member of GameState? Game and MainMenu inherit ChangeState(), but they don't seem to need it. Something doesn't look right.

The link from Nusakan shows a good way to implement this.

### #9SiCrane  Moderators

Posted 10 January 2013 - 07:29 AM

3) The class contains (including a pointer) something from the standard library. (i.e std::string, std::string*)
Note that some standard library objects have a header that just has forward declarations: iosfwd, which forward declares the various stream classes and related classes.
Also... beware of smart pointers. The deleter function in it will need more than just a forward declare.
It depends on the smart pointer. Some smart pointers will need the full definition to declare as a class member, such as std::auto_ptr since it automatically calls delete. However some smart pointers will only need a forward declaration, such as std::shared_ptr since the deleter isn't created until an object is assigned to the pointer.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.