Jump to content

  • Log In with Google      Sign In   
  • Create Account


#Actualparinho7

Posted 02 January 2013 - 05:26 PM

A pattern that I frequently use is to have my state->update() method return a shared_ptr to the state the game should switch to. If update returns an empty pointer, no state change occurs. Something like this (very quick, incomplete and un-tested, but it shows the gist of it)

enum EStates{	STATE_NULL,	STATE_MENU,	STATE_PLAYING,	STATE_EXIT};class GameState{	public:	GameState(){}	virtual ~GameState(){}	virtual std::shared_ptr update()=0;	virtual void event()=0;	virtual void render()=0;	virtual unsigned int getState()=0;};class MainMenuState : public GameState{	public:	MainMenuState() : GameState(), nextState(STATE_NULL){}	~MainMenuState(){}	unsigned int getState()	{		return STATE_MENU;	}	std::shared_ptr update()	{		switch(nextState)		{			case STATE_NULL: return std::shared_ptr(); break;  // Return empty ptr			case STATE_PLAYING: return std::shared_ptr(std::make_shared()); break;			case STATE_EXIT: return std::shared_ptr(std::make_shared()); break;			default: return std::shared_ptr(); break;		}	}	void event()	{		cin >> nextState;	}	void render()	{		cout << "MAIN MENU" << endl;		cout << "Press '3' to play"  << endl;		cout << "Press '0' to continue in Main Menu"  << endl;	}	private:	unsigned int nextState;};int main(){	std::shared_ptr state=std::make_shared();	while(state->getState()!=STATE_EXIT)	{	    state->render();	    state->event();		std::shared_ptr newstate=state->update();		if(newstate)		{			state=newstate;			newstate.reset();		}	}}
Update performs a switch on nextState as in your code, and returns either an empty pointer if no state change should occur, or it creates a new state object (std::make_shared news an object and assigns it to a shared_ptr in one call, safely) and returns a shared_ptr to the new state object. In the main loop, the return result of update is checked, and if it is a non-empty pointer, then state is assigned to the new pointer. This essentially overwrites the old state that was assigned previously, which will then drop out and be automatically and safely deleted.

Any time you find yourself doing what amounts to delete this you are almost certainly making an error, regardless of whether or not it seems to work. That sort of thing is just not wise to do unless you are experienced enough to know what is really going on.




It works perfectly, thank you.

Thanks everyone for the replies, you were all very helpful. May I ask one last thing? Recommendations on book and links to study about FSM for games and also some advanced matters of C++. For example, I now know what this line does MainMenuState() : GameState() but I don't know what the syntax actually mean. Where can I find that? I was also unaware of the existence of "shared_ptr" where can I study about stuff I don't know they exist?!.

Thanks again!!

#2parinho7

Posted 02 January 2013 - 05:24 PM

A pattern that I frequently use is to have my state->update() method return a shared_ptr to the state the game should switch to. If update returns an empty pointer, no state change occurs. Something like this (very quick, incomplete and un-tested, but it shows the gist of it)

enum EStates
{
	STATE_NULL,
	STATE_MENU,
	STATE_PLAYING,
	STATE_EXIT
};

class GameState
{
	public:
	GameState(){}
	virtual ~GameState(){}

	virtual std::shared_ptr update()=0;
	virtual void event()=0;
	virtual void render()=0;

	virtual unsigned int getState()=0;
};


class MainMenuState : public GameState
{
	public:
	MainMenuState() : GameState(), nextState(STATE_NULL){}
	~MainMenuState(){}

	unsigned int getState()
	{
		return STATE_MENU;
	}

	std::shared_ptr update()
	{
		switch(nextState)
		{
			case STATE_NULL: return std::shared_ptr(); break;  // Return empty ptr
			case STATE_PLAYING: return std::shared_ptr(std::make_shared()); break;
			case STATE_EXIT: return std::shared_ptr(std::make_shared()); break;
			default: return std::shared_ptr(); break;
		}
	}

	void event()
	{
		cin >> nextState;
	}

	void render()
	{
		cout << "MAIN MENU" << endl;
		cout << "Press '3' to play"  << endl;
		cout << "Press '0' to continue in Main Menu"  << endl;
	}

	private:
	unsigned int nextState;
};

int main()
{
	std::shared_ptr state=std::make_shared();

	while(state->getState()!=STATE_EXIT)
	{
	    state->render();
	    state->event();
		std::shared_ptr newstate=state->update();
		if(newstate)
		{
			state=newstate;
			newstate.reset();
		}
	}
}

Update performs a switch on nextState as in your code, and returns either an empty pointer if no state change should occur, or it creates a new state object (std::make_shared news an object and assigns it to a shared_ptr in one call, safely) and returns a shared_ptr to the new state object. In the main loop, the return result of update is checked, and if it is a non-empty pointer, then state is assigned to the new pointer. This essentially overwrites the old state that was assigned previously, which will then drop out and be automatically and safely deleted.

Any time you find yourself doing what amounts to delete this you are almost certainly making an error, regardless of whether or not it seems to work. That sort of thing is just not wise to do unless you are experienced enough to know what is really going on.

 



It works perfectly, thank you.

Thanks everyone for the replies, you were all very helpful. May I ask one last thing? Recommendations on book and links to study about FSM for games and also some advanced matters of C++. For example, I now know what this line does MainMenuState() : GameState() but I don't know what the syntax actually mean. Where can I find that?

Thanks again!!

#1parinho7

Posted 02 January 2013 - 05:23 PM

<blockquote class="ipsBlockquote" data-author="JTippetts" data-cid="5016585"><p>A pattern that I frequently use is to have my state-&gt;update() method return a shared_ptr to the state the game should switch to. If update returns an empty pointer, no state change occurs. Something like this (very quick, incomplete and un-tested, but it shows the gist of it)<br /><br /></p><pre class="_prettyXprint _lang-code _linenums:0">enum EStates
{
STATE_NULL,
STATE_MENU,
STATE_PLAYING,
STATE_EXIT
};

class GameState
{
public:
GameState(){}
virtual ~GameState(){}

virtual std::shared_ptr&lt;GameState&gt; update()=0;
virtual void event()=0;
virtual void render()=0;

virtual unsigned int getState()=0;
};


class MainMenuState : public GameState
{
public:
MainMenuState() : GameState(), nextState(STATE_NULL){}
~MainMenuState(){}

unsigned int getState()
{
return STATE_MENU;
}

std::shared_ptr&lt;GameState&gt; update()
{
switch(nextState)
{
case STATE_NULL: return std::shared_ptr&lt;GameState&gt;(); break; // Return empty ptr
case STATE_PLAYING: return std::shared_ptr&lt;GameState&gt;(std::make_shared&lt;PlayingState&gt;()); break;
case STATE_EXIT: return std::shared_ptr&lt;GameState&gt;(std::make_shared&lt;ExitState&gt;()); break;
default: return std::shared_ptr&lt;GameState&gt;(); break;
}
}

void event()
{
cin &gt;&gt; nextState;
}

void render()
{
cout &lt;&lt; "MAIN MENU" &lt;&lt; endl;
cout &lt;&lt; "Press '3' to play" &lt;&lt; endl;
cout &lt;&lt; "Press '0' to continue in Main Menu" &lt;&lt; endl;
}

private:
unsigned int nextState;
};

int main()
{
std::shared_ptr&lt;GameState&gt; state=std::make_shared&lt;MainMenuState&gt;();

while(state-&gt;getState()!=STATE_EXIT)
{
state-&gt;render();
state-&gt;event();
std::shared_ptr&lt;GameState&gt; newstate=state-&gt;update();
if(newstate)
{
state=newstate;
newstate.reset();
}
}
}
</pre><br />Update performs a switch on nextState as in your code, and returns either an empty pointer if no state change should occur, or it creates a new state object (std::make_shared news an object and assigns it to a shared_ptr in one call, safely) and returns a shared_ptr to the new state object. In the main loop, the return result of update is checked, and if it is a non-empty pointer, then state is assigned to the new pointer. This essentially overwrites the old state that was assigned previously, which will then drop out and be automatically and safely deleted.<br /><br />Any time you find yourself doing what amounts to <strong>delete this</strong> you are almost certainly making an error, regardless of whether or not it seems to work. That sort of thing is just not wise to do unless you are experienced enough to know what is really going on.<br />&nbsp;<br /><p><br /></p></blockquote><br />It works perfectly, thank you.<br /><br />Thanks everyone for the replies, you were all very helpful. May I ask one last thing? Recommendations on book and links to study about FSM for games and also some advanced matters of C++. For example, I now know what this line does MainMenuState() : GameState() but I don't know what the syntax actually mean. Where can I find that?<br /><br />Thanks again!!

PARTNERS