I've been trying to make my first game but I'm having trouble just setting up the basic game states. I don't want to use enum's and switch statements so I'm hoping to use a finite state machine to switch between screens but every article iv read about them is different and sometimes they leave out details, i'm just not understanding exactly how it works.
Say for example i had three screens. The Main Menu screen, In-Game screen and Pause screen. What would a finite state machine look like to switch between them? I don't mean for someone to write code for me, just maybe pseudo code or even an explanation.
In a state machine, all the possible conditions that the AI or program can be running are stored in some sort of data structure. When you need to be running these elements, you iterate to the specific position which would run that aspect of the system. For example, lets theorize that each mode that you postulated in the question is a collection of vertices and methods that would essentially run a game, but it is not specific as to what it is actually running. Lets call this class cState. There are three cStates: Main Menu, the in-Game screen, and the pause screen. Each one has its own collection of stuff that it utilizes to render each aspect that is unique to each state.
1 2 3 4 5 6 7 8 9 10 11
class cState{};
//... before the main loop
cState States[3];
int CurrentState = 0; //start at the menu screen
//... in the main loop
//running the main game
States[CurrentState].run();
but if the user presses "escape", you would change the States position to 2, because the user wants to pause the game:
1 2
if(Input.key == Input::Key::Escape)
CurrentState = 2; //move into the pause screen
Since c++ uses inheritance and run-time polymorphism, it is easy to implement a state machine by making cState a pure virtual base class, and having on member: Run(). If you had a game object, and that object inherits from cState, its simple to utilize this logic:
1 2 3 4 5 6 7 8 9 10 11 12 13
class cState
{
public:
virtualvoid Run() = 0;
};
class cGame : public cState
{
public:
//override the Run function
void Run()
{ //do stuff};
};
Thank you for the response. One problem i have with that, and that i have had myself when trying to implement a FSM, is that "CurrentState = 2" seems a bit ambiguous. I plan to have quite a few states and eventually its might be hard to know which state is which, and even harder for someone else to read.
The way i've been trying is by storing a pointer to each state in a vector, each state has a pointer to the state machine so it can change the current state. There's a few thing i don't understand like how does the state machine know which state to change to and which state it is changing from.
These could probably be solved by enumerations and switches, or even assigning strings to each state and then iterating through them comparing strings. But i thought the point of a state machine was so you didn't have to do that.
> how does the state machine know which state to change to and which state it is changing from.
That information should be encapsulated in each state. Each state knows about its behaviour; what it does in response to an event, and what state it must transition to.