Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualJossos

Posted 10 January 2013 - 03:44 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
{
   //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!


#1Jossos

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


PARTNERS