I’ve created a state machine to start my project. My perfectionist tendencies lead me to go over things just to make sure they’re done properly. So when looking at tutorials and being lost because the internet looks like big mess of everything and partial projects, I tend to doubt myself.
My plan is to start off with the basics and work my way toward the center of my mechanics. I’m not going to be doin anything revolutionary, just the basic foundation for a simple game. Take the word “simple” with a grain of salt, because of coarse it’s going to take time, effort, blood, sweat and tears to finish a quality product.
The start of this plan is to create a basic state machine to keep track of the players input. When adding more content to the game, more states will be added and transitions will be easier for me to follow this way. My goal was to use the update loop as a way to listen to the player, but also ignore them depending on the state.
My first hurdle was creating a script and having the scene not show my any debug logs. A note to anyone who has a similar problem, make sure when you hit play, go to the top of the console tap and to the right and make sure the exclamation bubble is on to see the debug logs. Then make sure to press the game scene with your mouse to make sure, that the game scene is actually highlighted.
I got frustrated because nothing seemed to work. Then decided to create a simple hello kitty script, just to find out that my scripts weren’t the problem. It was that damn button and the scene not being selected. The hello kitty script didn’t seem to work, until I found a single post online that talked about the exclamation button.
Finally, I checked my other scripts and they worked fine. It was just that button.
So here’s my state machine. It seems to work, but I’m not an experienced programmer. Is there any advice anyone could give to improve upon or am I worrying for nothing. This should persist through scenes and depending on the players input, should trigger an event of some kind, which should change the state.
public class GameState : MonoBehaviour
{
void Awake()
{
DontDestroyOnLoad(gameObject);
}
private static BaseState CurrentState;
void Start()
{
CurrentState = new TestState();
CurrentState.Enter();
}
void Update()
{
CurrentState.Execute();
}
public static void ChangeState(BaseState NewState)
{
CurrentState.Exit();
CurrentState = NewState;
CurrentState.Enter();
}
}
Here’s a sample state, listening for the user to do something.
public interface BaseState
{
public void Enter();
public void Execute();
public void Exit();
}
public class TestState : BaseState
{
private bool StateActive = false;
public void Enter()
{
Debug.Log("Setting Up : State One.");
StateActive = true;
}
public void Execute()
{
if (StateActive == true)
{
if (Input.GetKeyDown(KeyCode.A))
{
Debug.Log("Test State One!");
}
if (Input.GetKeyDown(KeyCode.Space))
{
StateActive = false;
GameState.ChangeState(new TestStateTwo());
}
}
}
public void Exit()
{
Debug.Log("Cleaning Up : State One.");
}
}
I’ve picked apart dozens of tutorials and came up with this. Am I missing anything important?