Game crashes for no obvious reason, help please

Started by
10 comments, last by Paradigm Shifter 11 years, 3 months ago
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!!
Advertisement
It calls the default constructor of the base class GameState. It's not actually necessary to do that since if you don't call the base class constructor it calls the default constructor (if there is one) anyway.
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

This topic is closed to new replies.

Advertisement