简体   繁体   中英

How do i manage my game state simplistically? c++

Hi I'm new to managing game states and i am trying to change the different states using a enum list.

enum class GENERAL_STATE : int {
menu = 1,
gamestart = 2,
};

and the header class that has the method to get and set the states

class GameControl {
private:

GENERAL_STATE generalState;

public:

GameControl();
~GameControl();

GENERAL_STATE getGeneralState() { return generalState; }
void setGeneralState(GENERAL_STATE gens) { generalState = gens; }
};

i have set the pointer to control the states using

GameControl*    gameControl;

in my game loop, i've added

switch (gameControl->getGeneralState())
{
case GENERAL_STATE::menu :
{
                             menu1.draw();
}break;
}

to draw the menu screen, but somehow it crashes and brings me to this line

GENERAL_STATE getGeneralState() { return generalState; }

any ideas?

If you haven't done so, you have to initialize the gameControl pointer.

For example like this:

GameControl*    gameControl = new GameControl();

But don't forget to delete it at the end.

Or better yet, don't use a pointer at all:

GameControl   gameControl;
switch (gameControl.getGeneralState())

But also inside GameControl you should initialize generalState .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM