What is the best way to implement Game States?

So I am trying to create an RPG, and off the top of my head I will need about 4 different game states that changes what button input does. The first would be the normal state (when your just walking around in the world), the second would be an event state (where dialogue happens and game events take place (the only real input would be confirm to make the text move forward), a battle state (which would handle all the fighting functions) and a menu state where you would call up the main game menu (use up down to cycle through the options and confirm cancel to select). My question is what is the best way to get this done?

I am guessing I would create a script (maybe a static class?) with each state as a static boolean variable, and an enumerator var that has each state as an option and whichever is selected as the enumerator var that boolean is true and all the rest are false.

That being said, is there a way to make each be called like the start and update functions? For example write out:

function Event(){

code for the event state;

}

and so on for each different state, so I wouldn’t have to to say

if(GameState == Event){

code for the event state;

}

Ive found that I run into strange problems with other booleans within if statements.

Any help or pointers to send me down the right path on starting to create this system would be helpful. I have a good idea in my head of how I could make it work, but I’ve found that there are often far better ways to do things like this that are already used by lots of people (usually the way the game engine intends for you do it). I find myself getting a lot of my stuff working, run into problems with it and when I go looking for ways to fix it I realize I should have started out doing things a lot differently and end up having to start over.

Thanks in advance, any help is greatly appreciated.

Hi I am aware this is super late. But hope I can still help.

For c# you can use

public enum gameState {
  none,
  state1,
  state2
}

 private gameState myState = gameState.none;

//Test Code
if (myState == gameState.state1) {
//Some code
} else if (myState == gameState.state2) {
//Some code
}

I was wondering if the ones Ive seen are all done in C# because there is something C# can do that Java cannot that relates to the FSMs

Yes. Delegate functions.

This is a great tutorial on how to implement FSMs in Unity:

Note: There is a series of three. It starts simple, and introduces delegates (the “real” way to do it) in the later ones.