Conditional Branching
In this tutorial, we’ll learn how to create conditional branching in your game. Conditional branching allows different actions to occur based on player choices or specific conditions, making your game more dynamic and interactive.
Setting up the Map
First, we need to set up our map with the necessary elements. Add a tree
and chest
into your map. You can use the default assets from the Objects library
in your map under the Edit objects
tab.
Creating a Variable
Next, we’ll create a variable to track whether the player has found the key.
- Navigate to
Game assets > Variables
. - Click
+ New switch
to add a switch (true/false) variable. - Name the variable “Has key”.
Create Event Groups
Now, we’ll create two event groups: one for finding the key and one for opening the chest.
Got the Key Event Group
- Navigate to
Maps > Map 1 > Events
and click+ Create event group
. - Name the event group “Got the key”.
- Add the following events:
Dialog
: Add a dialog event with the text “You found a key!”Set variable
: Add a set variable event to set the “Has key” variable to true.
- Under
Map events
:- Use the
Add event group
event to add it into the map. - Set the
Interact
trigger to a tree.
- Use the
- Back in the “Got the Key” event group:
- Add a
Remove event group
event. - Select “Got the key” as the event group to remove.
- Add a
Note, we had to add the Remove event group
event after it was added because the engine only allows you to remove event groups which have been added into the map.
Open Chest Event Group
-
Create another event group and name it “Open chest”.
-
Add two conditional events to check if the “Has key” variable is true:
- If True: Add a dialog event with the text:
- If False: Add a dialog event with the text:
-
Under
Map events
:- Use the
Add event group
event to add it into the map. - Set the
Interact
trigger to the chest.
- Use the
You can expand upon this by adding more events for what happens in your game. For example, if the chest is opened, you can spawn a character, make them walk to the player, and create a dramatic scene.
Play Test
Now, try play testing to see it in action! Walk up to the tree to get the key and then go to the chest to see the different dialogues based on whether you have the key.