In this part, we'll add level switching when we reach the red flag at the end of a level.
- Select the Objective object in the scene view.
- Add a flow machine component
- Create a new macro for it named Objective
- Apply the changes to the prefab
- Delete the default Start and Update units
The first thing we need to do is detect when the player reaches the red flag. All that's needed for this is a collision check, like we just did for spikes:
But hey, that's copy-pasting! We'll try to avoid that to keep our graphs DRY. We'll create a new super unit that handles checking for collision with an object of a given tag, and telling us whether we hit or target or another object.
Cut and paste these units in a new macro called OnCollisionWith, and add Input and Output units, both found under Nesting.
On the input node, add a string value input for the tag to check:
Make sure you check the Has Default Value option, allowing us to use inline values in the super unit.
On the output node, add three outputs:
- A control output in case we hit our target
- A control output in case we hit another object
- A game object value output for the object that we hit
Then, connect the ports on the input and output units to their matching ports in our collision check graph, like so:
Going back to our Objective graph, we can now simply drag & drop our new macro to create a super unit, then fill in the Tag field with Player:
If you want, you can also replace our previous collision detection on the Spikes graph with the new super unit. Look how simple it becomes then!
Keeping your graphs DRY might seem like overkill at first, especially for simple logic like this. But it's important to remember that as your game scales, you'll need to do this to keep everything easily maintainable.
We're currently working on making this a one-click operation: select the units you want, then click a button to turn them into a super unit and save it as a macro. Stay tuned for updates!
Now that we have a solid, reusable collision detection, we just need to load the next scene. But because we'll be using that same Objective prefab across all our levels, we need a variable to configure which scene is the next. Add a new string object variable called Scene, and give it a value of Level2:
Then, apply the changes to the objective prefab so that they all have a Scene variable.
Later, in other levels, you'll need to change that variable to the name of the next scene. For example, in Level2, you'll select the Objective object, and change its Scene variable to Level3.
Finally, to load the level, simply connect the collision super unit with a Load Scene unit:
Later in this tutorial, when we create the main menu, we'll want only the levels that the player has entered at least once to be available for replay, so that they can't skip to the last level before having beat the previous ones.
To do that, we'll be using saved variables. This is a powerful feature of Bolt that acts as a built-in save system: any content you place in a saved variable will be persistent across playthroughs, even if you exit the game.
The convention we will be using is to have one saved variable per unlocked level. It will be a boolean (true or false) indicating whether the level is unlocked, and its name will be LevelX_Unlocked, where X changes for every level.
When the player reaches this objective, the next level should get unlocked. In order to do that, add the following nodes to your Objective graph:
Concat is located under Codebase > System > String. It takes the arguments you pass to it and joins them together. For example, here, the output of the concat unit will be Level2_Unlocked, which we use as the name of the saved variable.
Finally, tie up everything together in the graph with groups.
If you test your game now, you should switch to level 2 once the player reaches the red flag:
You should also see a new Level2_Unlocked variable appear in the Saved tabs of the variable window:
In this part, we will add a death mechanic when the player touches the spikes.
In this part, we will load a head-up display (HUD) on top of our game view.