Hello! Welcome back.
In our last lesson, we built the foundation for a cinematic event system, allowing us to script character movements, camera pans, and dialogue into linear cutscenes. We saw how the waitForCompletion flag, much like async/await in your web development work, is crucial for managing the timing of these asynchronous actions.
Today, we're going to make our events intelligent. A game world feels alive when it remembers and reacts to the player's choices. This lesson focuses on precisely that, addressing the learning outcome: Implement conditional event logic (IF/ELSE) based on game flags to alter dialogue and event outcomes. We'll introduce the concepts of "Switches" and "Variables"—the memory of your game—and implement the IF/ELSE logic that allows your events to respond to them.
1. Game Flags: The Memory of Your World
To create dynamic events, the game needs a way to store and retrieve state. Did the player find the hidden key? Have they already spoken to the king? Is the "volcano eruption" event active? This is the job of game flags. In RPG Maker terminology, and in our engine design, these come in two primary forms:
- Switches: Simple boolean flags. They are either ON or OFF. Perfect for tracking binary states like whether a door is unlocked or a specific cutscene has been viewed.
- Variables: Numeric values. They can store integers, allowing you to count things (e.g., "slimes defeated"), track progress in a multi-stage quest, or store a player's choice.
These concepts are fundamental and directly parallel to boolean and number data types in any programming language.
Let's start with the simpler of the two: Switches.
RPG Maker MV Tutorial #15 - Switches!
This tutorial by SomeRanDev for RPG Maker MV provides a perfect introduction to Switches. It starts with a simple, clear problem: one NPC needs to know what another NPC has done. This illustrates the core purpose of a global flag.
Please watch the first two minutes (00:00 - 02:10). Focus on: The problem statement: Event 'Bob' has no way of knowing that you've talked to event 'Joe'. The solution: Using a global 'Switch' that Joe turns ON, which Bob can then check.
2. Conditional Branches: The IF/ELSE of Eventing
Now that we have a mechanism to set a flag (Control Switches in the video), our EventInterpreter needs a command to read that flag and execute different commands based on its value. This is the Conditional Branch.
Architecturally, a conditional branch command in our JSON structure would look something like this:
{
"type": "conditionalBranch",
"condition": {
"type": "switch",
"switchId": 1, // "Talked to Joe" switch
"value": "ON"
},
"ifCommands": [
// Commands to run if the switch is ON
{ "type": "showText", "text": "Ah, Joe sent you. You may pass." }
],
"elseCommands": [
// Commands to run if the switch is OFF
{ "type": "showText", "text": "You shall not pass until Joe permits it." }
]
}
The EventInterpreter, upon reaching this command, would evaluate the condition. If true, it would insert the ifCommands into the front of its command queue to be executed next. If false, it would use the elseCommands.
RPG Maker provides two primary ways to implement this logic: within a single command list using a Conditional Branch, or by using separate "Event Pages" that are active under different conditions. For the most flexible, code-like logic, the Conditional Branch is the way to go.
RPG Maker MV Tutorial #15 - Switches!
Let's continue with the same video, which now demonstrates how to use the 'Conditional Branch' command to create this exact IF/ELSE logic within a single event.
Watch from 03:59 to 06:28. This part is crucial as it demonstrates: Creating an IF statement with the 'Conditional Branch' command. Adding the 'Else Branch' to create a complete IF/ELSE block, making the logic robust.
The resource below gives a text-based overview of the same concept and shows the variety of things you can check in a condition, far beyond just switches.
Creating and Implementing Events - RPG Maker Unite
The RPG Maker Unite documentation provides a clear summary of 'Branch Settings,' reinforcing what you saw in the video.
Read the section titled 'Event Command: Flow Control > Branch Settings'. Note the list of specifiable conditions, such as checking for items, actor status, or variables, which we'll cover next.

3. State Scope: Global Flags vs. Local Self-Switches
Your developer instincts might be firing right now. If we need a switch for every single treasure chest in the game to check if it's been opened, won't we run out of global switches and create a management nightmare?
You'd be absolutely right. This is a problem of scope. For states that only matter to a single event (like a chest being open or an NPC having given you its one-time reward), a global flag is poor design.
This is why RPG Maker includes Self-Switches. These are four private boolean flags (A, B, C, D) that are scoped exclusively to each event. Changing Self-Switch 'A' on one event has no effect on any other event. This is directly analogous to an object property (this.isOpened = true) versus a global variable.
RPG Maker MV Tutorial #17 - Self-Switches!
This follow-up tutorial from SomeRanDev perfectly explains the problem of overusing global switches and introduces the more elegant solution: Self-Switches.
Watch from 00:40 to 04:27. Pay close attention to the architectural argument: He first solves the 'infinite gold' problem with a global switch. He then points out the flaw: this doesn't scale. You have a limited number of global switches. Finally, he introduces the Self-Switch as the clean, scalable solution for event-specific state.
This distinction between global Switches (for major plot points, world states) and local Self-Switches (for individual event states) is a cornerstone of clean event architecture.
4. Beyond Booleans: Tracking Numbers with Variables
Switches are powerful, but they can only track ON/OFF states. What if you need to build a quest that requires the player to collect 5 Wolf Pelts? A switch can't count. For this, we use Variables.
Variables hold numeric values and can be manipulated (Set, Add, Subtract, etc.). They can then be checked in a Conditional Branch using comparison operators (>=, ==, <).
Guide :: Learning how to work with Event Commands
This Steam Community guide explains the distinction between Switches and Variables in terms that will be very familiar to you from a programming perspective. It then gives a great, practical example of why you would need a variable.
First, read the 'Switches and Variables' section to solidify the concepts. Then, read 'Conditional Branches and Conditions'. Focus on the example that builds in complexity: checking for one 'Iron Ore', then checking for '3 or more' using a variable, and finally adding a second condition to check for gold.
Test your understanding!
You are scripting a door that only opens if the player has a "Silver Key" in their inventory AND has flipped a "Power Lever" elsewhere in the dungeon. How would you structure the Conditional Branch in your event interpreter to handle this?
Show answer
You would use nested conditional branches (or a condition that allows AND logic).
The event logic would be:
- Outer Conditional Branch: Check if Switch "Power Lever" is ON.
IFblock (of the outer branch):- Inner Conditional Branch: Check if the party has item "Silver Key".
IFblock (of the inner branch):- Play door opening animation/sound.
- Move the event out of the way.
- Use a Self-Switch to keep it open permanently.
ELSEblock (of the inner branch):- Show text: "The door is powered, but you lack the key."
ELSEblock (of the outer branch):- Show text: "The door seems to have no power."
This ensures both conditions must be met, and provides different feedback to the player depending on which one is missing.
Conclusion
You've now added the most critical component for creating a dynamic, responsive game world: stateful logic. By combining game flags with conditional execution, your event system is no longer just a "movie player" for cutscenes; it's a true game logic engine.
Key Takeaways:
- Game Flags (Switches and Variables) provide memory for your game, tracking progress and player actions.
- Conditional Branches are the
IF/ELSEconstructs of the event system, allowing you to execute different command lists based on the state of game flags. - Understanding scope is crucial. Use global Switches for major story beats that many events might need to reference, and use local Self-Switches for an event's private internal state (e.g., an opened treasure chest).
- Variables unlock more complex logic by allowing you to count, store stages, and perform numeric comparisons.
Preview of the next lesson:
We have a powerful interpreter that can run complex, branching command lists. But how do we start these events? So far, we've implicitly assumed the player "talks" to an NPC. In the next lesson, "Implement event triggers for player interaction, automatic execution, and map entry," we will design the system that links events on the map to specific activation methods, truly bringing your game world to life.
Can't find a good explanation? Sign up and we'll make it for you
Sign up