This guide will go over what a state machine is, how to make one in gimkit, and an example of one using a queue.
What is a State Machine?
A state machine can be in a given amount of states. Take a door for example. IT can either be open or closed. You can also do things to the state machine. Continuing off of the door example, you can open the door, or you can close it. Note that you can open the door while it’s open and you can close the door while it’s closed. This means that you can do an action to any state.
Applying actions to different states can result in different outcomes.
Actions on a state machine should change the state. Opening a door changes the state from closed to opened, or from opened to opened. Here’s a wikipedia page if you want more:
State Machines in Gimkit
So what? Who cares about another random concept? Well… It turns out that using this concept actually can help us code cool systems, like the queue in this guide.
Let’s start off by trying to replicate the state feature. I’m going to use activated/deactivated triggers for this task. You could totally use a form of block segmentation by using a property and running code based on that property. But for this guide, I’m choosing to go memory-efficient and use trigger activation states. So basically we’re going to have rows of triggers that get activated and deactivated based on inputs.
Each of these rows of triggers represents a state. The first column represents what each state would do if a certain action was performed. In reality only one row of triggers would be active at any moment.
How to Make a Queue
Let’s actually apply this concept and create a queue!
What Even is a Queue???
Before making this, let’s just clarify what and why one would want this. So let’s say that we have a resource that is global, and you can’t make the scope be player. So if 2 people use the system at the same time, then the properties and other things will be mixed up, resulting in a buggy system. So we can use a queue to restrict access to the system.
Outlining the State Machine Aspect
So the queue will have 3 states. Let me just say that these states can be different for different players. I can be waiting, and Tim can be using the system, and Jimbob can not even want to use the system. So the states are:
- Using system
- Waiting to use system
- Not waiting nor using system (I’ll call this state 3 because it’s easier to do that)
There will be 2 actions that we can do on the queue:
- Player using system finishes
- Player joins queue
If the player finishes, then they will exit the queue and will be in state 3. If the player joins the queue, they will be in state 2, and if either no one is using the queue or somebody finishes, they will go to state 1.
Putting this in Gimkit
Let’s create our trigger array! Before copying the triggers, make sure you make the active scope be player.
Since the default state is not using the queue, make the using and waiting rows be inactive on game start. Now, when a player joins the queue (channel input), make the state 3-joins trigger deactivate the state 3 row and activate the waiting row. Since joining while you’re in the using and waiting states doesn’t do anything, feel free to delete those triggers. They won’t do anything.
When a player in the using state finishes, you need to do 2 things:
- Return the player that just finished to state 3
- Change one person from the waiting state to the using state
For the first one, just make the channel broadcast for finishing deactivate the using state and activate state 3. The second one is a bit harder to do. Make a relay to all other players that receives on the channel that shows that the player has finished and broadcast on “Filter Start Use [SYSTEM_NAME]”. Make the waiting-finishes trigger (in the state machine array) receive on that channel and broadcast on “Attempt Start Use”. Make another trigger that receives on “Attempt Start Use”. The activation scope should be global. The trigger that receives on “Attempt Start Use” should activate on the channel “Filter Start Use [SYSTEM_NAME]” and deactivate on the channel “Attempt Start Use”. That same trigger should also broadcast to your system, activate the using state, and deactivate the waiting state.
And that’s it! If you have any questions, feel free to ask!