Header logic help

Started by
7 comments, last by SiCrane 11 years, 3 months ago

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
{
   //MainMenu stuff...
};

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!

Advertisement

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=>
     MainMenu myMainMenu;     


    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"
#include "MainMenu"


    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;
    }

Summary of headers/cpps/includes:


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

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

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

MainMenu.cpp
------------
include "MainMenu.h"

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

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

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.

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.

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.

[quote name='Jossos' timestamp='1357815763' post='5019832']

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.
[/quote]

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/

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...)
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.

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.

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.

This topic is closed to new replies.

Advertisement