How to Change Scenes in Godot 4 – Complete Guide

One game level is fine, but many games require more. For Godot, this involves scenes, and being able to change scenes in Godot is likewise super important.

In this in-depth guide, we shall explore setting up multiple levels and learn how we can change Godot scenes. We will begin by creating an end flag feature for our game, which transports the player to the next level or returns them back to the first level.

Creating an immersive, exciting gameplay experience with multiple levels is a fantastic way to engage your players. If you are interested, you can also review our full course, Develop a 3D Platformer with Godot 4 which covers these concepts in more detail.

Without delay, let’s jump into it.

Project Files

Download the project files that accompany this tutorial by clicking the link below. Inside, you’ll find all the assets to help guide you through this tutorial.

Download Project Files Here

CTA Small Image
FREE COURSES AT ZENVA
LEARN GAME DEVELOPMENT, PYTHON AND MORE
ACCESS FOR FREE
AVAILABLE FOR A LIMITED TIME ONLY

End Flag

In this first, we are going to set up an end flag for our levels. The end flag is an object located at the end of each level, and when the player collides with it, they are either teleported to the next level or returned to the first level if they have reached the end of the game. In other words, this is the trigger we will use to change scenes in Godot.

Creating the End Flag Object

Our EndFlag object will have a very similar setup to our coins and enemies. To start with, we will be using an Area3D node as the root node for our End Flag.

Area3D

Remember to rename this to “EndFlag” so that it can be easily identified.

EndFlag object

For this object, we will be using the flag.obj asset as our Model.

flag.obj asset

We will make this a child of our EndFlag root node, and rename it to “Model“.

child of EndFlag nood

We will set the Position value to (0, 0, 0) and set the Scale to around 1.3 to match the size of the player.

Position and Scale

Finally, we will add a CollisionShape3D node to the object.

CollisionShape3D node

We will make this a BoxShape3D and scale it to about the size of the flag.

BoxShape3D

Scripting the Flag

With our node tree set up for the object, we are ready to add a new script to the EndFlag root node, which will handle the player collisions.

Script Node

We won’t need the _ready or _process functions, so we can remove these from the file. However, we will need to connect a body_entered signal to the script.

body_entered signal

Before we begin adding code to this method, however, we need to create a variable to hold the next scene to load into. We can call this “next_scene”, which needs the @export_file(“*.tscn”) tag to make it visible in the inspector as a scene input box.

@export_file("*.tscn") var next_scene

We can test this by selecting our EndFlag object in the scene, and dragging our “Level1.tscn” scene into the Next Scene property in the inspector.

Scene property inspector

We can now add code in the _on_body_entered method to check if the player has touched the spike, and if so, to change the scene to the next_scene value.

func _on_body_entered(body):
    if body.is_in_group("Player"):
        get_tree().change_scene_to_file(next_scene)

Here, we use the get_tree method to get the root of the current scene, and then use the change_scene_to_file method to switch out the scene to the next level. If you play the game to test this now, you should find that when you touch the flag the scene is reset, as it is reloading the same level. We can now save the EndFlag object as its own scene, by dragging it into the FileSystem.

EndFlag FileSystem

In the next part, we will be setting up multiple levels so that we have something for our EndFlag object to connect to (as to change scenes in Godot you do, in fact, need multiple scenes).

Multiple Levels

With everything set up for us to change scenes in Godot, we are ready to create a second level for our game. To start with, we are going to create a New Scene using the button in the Scene menu dropdown.

New Scene

Like with our Level1 scene, this will be a 3D Scene.

3D Scene

We will rename the root node of the scene to “Main”.

rename root node

We can now save this scene (CTRL + S) as “Level2.tscn”.

save scene as Level2.tscn

We can now begin dragging in all of our different elements from the FileSystem. For our example game, we have many scenes which can be dragged in:

  • Coin
  • Enemy
  • EndFlag
  • Player
  • Spikes
  • PlatformLarge

These will act as our building blocks for all of our levels, and we can use them and duplicate them around any new scene to create a brand new level. Remember to make sure to test it often to make sure it’s possible to make all of the jumps without being too easy. Here is our design for a second level.

levels

As this is our second – and currently last – level, we need to make sure the Next Scene value of the EndFlag returns to Level1.tscn, which it should already. For our End Flag on Level1 we want our Next Scene property to point to level 2, so that when you touch the End Flag on level 1, it loads the second level.

Scene property

We also want to copy our DirectionalLight and WorldEnvironment nodes into our Level2 scene from Level1 so that our second level doesn’t look bland.

DirectionalLight and WorldEnvironment

We can also create a new environment on the WorldEnvironment node, to do this, select Make Unique from the dropdown so that it doesn’t overwrite the Level1 WorldEnvironment.

Make Unique

We can then change values like the Background Color to make the atmosphere different from the first level.

Background Color

You can then use this to make as many new levels as you want for your game.

Godot – Change Scenes Wrap-Up

We have reached the end of this tutorial on how to change scenes in Godot. You have successfully learned how to set up an end flag feature and build multiple levels in your game using Godot. We also faced the challenge of designing levels so they are tough but not frustratingly impossible. Now you can make your games more immersive and engaging for players who crave levels of increasing difficulty.

You can continue to experiment with more complex scenarios and design, keeping in mind the principles we used here. With a little creativity, you can build fantastic multi-level games that will keep players coming back for more. You can also explore further applications with the ability to change scenes in Godot – such as multiple maps for RPGs. Whatever you pick, you now have a new tool to leverage in your arsenal.

All the best in your Godot journey and keep creating amazing games!

Want to learn more? Try our complete DEVELOP A 3D PLATFORMER WITH GODOT 4 course.

Did you come across any errors in this tutorial? Please let us know by completing this form and we’ll look into it!

FREE COURSES
Python Blog Image

FINAL DAYS: Unlock coding courses in Unity, Godot, Unreal, Python and more.