- Home /
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.
There are lots of resources available already on State $$anonymous$$achines such as http://wiki.unity3d.com/index.php?title=Finite_State_$$anonymous$$achine , and there are other frame works and examples with a simple state machine google. If you haven't already read some of those examples its a good place to start.
That makes sense. I wasn't sure what finite state machines were when I googled. I checked out some stuff and an getting an idea of whats going on. I do have another question though. Every tutorial (or anything to do with FS$$anonymous$$s for that matter) are in C#.
I have been working in Java, and 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 FS$$anonymous$$s. I've been leaning pretty far towards biting the bullet and learning C# lately and changing all my code to it. If you cant do FS$$anonymous$$s in Java, that might the be the straw that breaks the camel's back...
Answer by Cornelis-de-Jager · Jul 16, 2015 at 01:22 AM
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
}
Answer by gribbly · Feb 12, 2013 at 02:28 AM
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.
For anyone else: Java is not JavaScript. I assume he is referring to UnityScript (which is very similar to JavaScript). JavaScript (and UnityScript) has support for closures and lambdas so you can do what you need to do even though it doesn't exactly have delegates.
unitygems appears to be dead... Any chance of mentioning the relevant parts of the tutorial here?
https://web.archive.org/web/20130805162319/unitygems.com/fsm1/ Used wayback machine to find a past version of that page. Hope it helps!
Your answer
![](https://koobas.hobune.stream/wayback/20220613092512im_/https://answers.unity.com/themes/thub/images/avi.jpg)