Hello! Welcome back to our course on Finite State Machines.
In our last lesson, we defined the three core concepts of FSMs: states, transitions, and events. You learned that an FSM can only be in one state at a time, and that events trigger transitions between these states.
Today, we'll put that theory into practice. This lesson is dedicated to the second learning outcome for this module: Draw a state diagram for a simple real-world scenario. This is a critical skill for a game developer because it allows you to plan and communicate complex behaviorsโfor an NPC, a UI menu, or a game round systemโbefore you write any code.
1. The Visual Language of FSMs
State diagrams are the blueprints for FSMs. They have a simple, standardized set of symbols that allow us to visualize a system's logic clearly. Let's watch a video that introduces these symbols and reinforces our understanding of states and events.
How to draw State machine diagram?
The video 'How to draw State machine diagram?' from MADE_EASY_FOR_YOU provides a clear, step-by-step introduction to the symbols used in state diagrams and connects them back to the concepts of states and events using a simple door analogy.
Please watch from the beginning to 11:35. The first three minutes cover the core symbols. The rest of the segment applies these symbols to explain how states and events work for a door, which is a perfect real-world example.
To summarize and review the key symbols from the video:
- State: A rounded rectangle containing the name of the state (e.g.,
Open,Idle). This represents a specific condition or behavior. - Initial State: A solid black circle with an arrow pointing to the starting state. Every FSM has exactly one initial state.
- Transition: An arrow pointing from one state to another. It represents a change of state.
- Event: Text written along a transition arrow that describes the trigger for that change.
- Final State (Optional): A circle with a dot inside. This marks the end of the FSM's life. Many systems in games (like NPC AI) are designed to run continuously and may not have a final state.
You might also see states represented by circles instead of rounded rectangles. Both are common notations, but we will stick with rounded rectangles for consistency as they are standard in UML (Unified Modeling Language), a widely used modeling language in software engineering.
2. How to Draw a State Diagram: A Walkthrough
Let's apply these concepts by drawing a diagram for a familiar object: a door. We'll follow a simple, structured process.
This process is well-described in the article "How to Create a State Machine Diagram". While you don't need to read it right now, we will be using its step-by-step thinking.
Step 1: Identify the States
First, think about the distinct conditions the door can be in. A simple door can be:
ClosedOpenLocked
These will be our three states.
Step 2: Identify the Events
Next, what actions can a person take that change the door's state?
opencloselockunlock
These actions are our events.
Step 3: Draw the Diagram and Define the Logic
-
Initial State: Let's assume a new door starts off
Closed. We draw the initial state symbol (โ) with an arrow pointing to a rounded rectangle labeledClosed. -
Connect the States: Now we draw the transitions based on our events.
- If the door is
Closed, theopenevent should make itOpen. Draw an arrow fromClosedtoOpenand label itopen. - If the door is
Open, thecloseevent should make itClosed. Draw an arrow fromOpentoClosedand label itclose. - If the door is
Closed, thelockevent should make itLocked. Draw an arrow fromClosedtoLockedand label itlock. - If the door is
Locked, theunlockevent should make itClosed. Draw an arrow fromLockedtoClosedand label itunlock.
- If the door is
Step 4: Review and Refine
Look at the diagram. Does it cover all intended behavior? Equally important, what does it prevent?
- Can you lock an open door? According to our diagram, no. There is no transition from the
Openstate triggered by alockevent. If that event occurs while the FSM is in theOpenstate, it is simply ignored. - Can you open a locked door? Again, no. There is no transition from the
Lockedstate triggered by anopenevent.
This is the power of an FSM: the logic is explicit and unambiguous. Here is a professionally drawn version of a very similar door FSM.
This diagram includes an extra piece of syntax on the close transition: [doorWay->isEmpty]. This is called a guard condition. It's an additional check that must be true for the transition to happen. In this case, you can only close the door if the doorway is empty. We'll explore guards in more detail later in the course.
3. Your Turn: Diagram a Traffic Light
Now, it's your turn to practice. Let's model another simple system.
Scenario: A basic traffic light on a road. It cycles continuously from Green to Yellow, then to Red, and finally back to Green.
Grab a piece of paper or open a simple drawing tool. Try to create a state diagram for this traffic light by following the steps we just used:
- What are the distinct states?
- What event causes the light to change from one state to the next? (Hint: it's not a button press).
- Draw the diagram, including the states, transitions, events, and an initial state.
Take about 5-10 minutes to sketch this out.
...
Finished? Let's compare your sketch to a well-defined example.
How to Create a State Machine Diagram (For Absolute Beginners)
The article 'How to Create a State Machine Diagram' contains a perfect example of a simple traffic light FSM. This will allow you to check your work and see a clean, final version.
Please read the section titled 'Example 1: Simple Traffic Light (3-State Cycle)'. Compare the diagram and the explanation to the one you just drew. Note how it uses a timed event, after(30s), to trigger the transitions.
Your diagram should have three states (Red, Green, Yellow) connected in a loop. The key insight is that the event is the expiration of a timer. This is extremely common in game development, where you might have an NPC patrol for 30 seconds before transitioning back to an idle state.
4. Back to Games: An NPC AI Diagram
Drawing diagrams for doors and traffic lights is great practice, but your goal is to apply this to games. Let's look at a state diagram that directly relates to your interest in NPC AI.

Let's trace the behavior:
- The AI starts in the
IDLEstate. - When the event
FIND TARGEToccurs (e.g., the player enters its line of sight), it transitions to theCHASEstate. - From
CHASE, two things can happen:- If the event
LOST TARGEToccurs (player gets too far away), it transitions back toIDLE. - If the event
TARGET IN ATTACK RADIUSoccurs, it transitions to theATTACKstate.
- If the event
- From
ATTACK, if theTARGET TOO FAR AWAYevent happens, it goes back toCHASE.
Even with just three states, this diagram provides a clear and robust plan for an enemy's logic. This is the blueprint you would use before implementing the FSM in Luau.
Conclusion
In this lesson, you took the theoretical concepts of states, transitions, and events and used them to create a visual planโa state diagram. This is a fundamental skill that bridges the gap between design and implementation.
Key Takeaways:
- A state diagram is a visual representation of a Finite State Machine.
- The core components are states (rounded rectangles), transitions (arrows), and events (labels on arrows).
- Drawing a diagram is a structured process: identify states, identify events, and then draw the connections that represent the system's logic.
- State diagrams are your blueprint for coding complex behaviors in a clean and manageable way.
In our next lesson, we'll flip the script. Instead of drawing diagrams, you will be given existing diagrams and learn to identify the current state, possible transitions, and triggering events. This will further solidify your understanding before we dive into our first Luau implementation.