Adding more levels
Until now, our game has only had one level. Let’s change that! Thankfully, Godot makes this fairly easy! There’s only a couple of things we’ll need to do.
- Actually create and store multiple levels.
- Load the level when we enter a specific spot.
Let’s get straight into it!
Creating Multiple Levels
To create multiple levels, we’ll want to save each level and everything in the level, as a Scene. A level should contain Everything that we want to be part of the level, such as enemies, coins, and potions. However we don’t want the player or the UI to be saved in the level.
An easy way to remember what we want to store in a level, is by thinking of what we want to belong or exist as part of the level!
Now, for our levels, we’ll obviously need our TileMapLayers! But we don’t want to be redoing all of the tileset settings each time… So what we’ll want to do, is save a blank version of our current TileMapLayers so that we can reuse them!
-
Let’s go to what is currently our testing scene, go to your TileMapLayers and erase all the tiles you’ve painted, so it’s just two blank TileMapLayers with all of our settings and our assets loaded. Now, let’s right-click the parent node of both TileMapLayers and click Save Branch as Scene calling it “Level_Template.tscn”
-
Great! Now we can get to actually setting up our first level! A level will only Need a couple of things to function:
- The TileMapLayers
- A node that represents the players spawn
Anything else, like enemies or coins, are optional
-
Let’s create a new scene with a root Node2D and call it “Level 1” drag in your Level_Template.tscn as a child of this. Also giving the root node another child of type Node2D calling “player_spawn”
-
To edit the two TileMapLayers we’ll need to right click on the Level_Template we imported, and tick editable children which makes this a unique instance of the scene, which we want anyway!
And that’s the most basic form of a level setup! From here, paint the level using the TileMap (Remembering to use the right layers for walls and floors) and adding enemies, potions, and coins, all as children of the Root node.
-
Once you’ve made a level, save the scene. Creating a new folder called “Levels” to store it in! Try creating two levels “level_1” and “level_2” making sure they’re somewhat different, so you can tell when you’ve changed levels.
-
You’ll also want to make sure you place the “player_spawn” node in the position you want the player to spawn in the level.
-
Save the scene as “level_1.tscn”
Here’s an example of a level I made, take note of the order of the nodes in the SceneTree:
Level Loader
The very last step for our game, is creating something to actually transition us between levels!
-
But first, let’s quickly clean up our main “World” scene. We’ll want to delete everything from this scene except for the Player & Camera2D and the CanvasLayer that holds the UI (And obviously keeping the root node)
-
We’ll add one additional node as a child of the root node called “levelHolder”
-
From your filesystem, drag your newly created “level_1.tscn” onto the levelHolder so that it becomes its child, as we want level 1 to be loaded from the start!
Here’s how my World scene looks:
We’ll make that level_transition node now!
-
For the final scene of our project, let’s create a new scene, with a root Node2D called “levelTransition” with a child Area2D and give that a child CollisionShape2D
-
Set the shape of the CollisionShape2D to be a square.
-
let’s create a script attached to the root node, called “level_transition.gd”
-
Let’s think about the variables for this script:
- The level we want to load (We’ll use an @export for this)
- A reference to our “levelHolder” node
- A reference to our player, to set their position
Great! The @export will allow us to assign what level want to load from each level, within the editor!
-
Now, if the player enters this region, we’ll
- Remove the last Level
- Load the new level
- Set the players position
-
Save the scene as “level_transition.tscn”
-
Now to each of our level scenes that we’ve created, add in the level_transition scene by dragging it from the filesystem. Place it somewhere you want the level to end (Ideally on some stairs, so the player expects the change) And in the inspector of the level_transition, after you’ve added an instance to a scene, find the level you want it to open in your filesystem, and drag it into the “to_load” slot.
Checklist
- I’ve at least two levels
- I’ve created the level loader
- I’ve added the level loader to my levels
- I’m able to transition between levels