State Machines and Queues

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.

image

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:

  1. Using system
  2. Waiting to use system
  3. 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.

image

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!

23 Likes

whoa. That was amazing. as soon is i saw this in wikipedia i knew that this was not just any guide

2 Likes

Nice guide! I never thought of the concept of a queue before.
Very creative.
You’ve been spewing high-quality guides for quite a bit. Take a break, man.

6 Likes

i would like that post but im out of likes :frowning:

1 Like

Awesome guide!

3 Likes

I never thought of this before! Great guide. I’m not judging you but I wouldn’t use Wikipedia cause it’s a unreliable source

2 Likes

That page has the right information and explains it well…

4 Likes

Thanks! This doesn’t really tire me out that much. I think I got this done in 2 hours? Idk. I just make them whenever I think of them.

4 Likes

wikipedia should not be used for current events or politics or anything with the potential to be controversial
other than that it’s fine

state machines are not controversial
literally no one cares enough to put out disinformation about state machines

5 Likes

This is amazing and detailed. It should be complicated, but it isn’t. This can open a whole new realm of possibilities, not just with queues, but with progression.

1 Like

ummm, I think Wikipedia does a pretty good job with definitions…

2 Likes

Okay i apologize for the disruption please go on talking about your state machines fair well fellow gimkiters

1 Like

I had no idea what this was until I read it, great guide! :smiley:

3 Likes

Bump