Level Changing

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Prestidigitator
:warning: Old Version Published before Godot 3 was released.

Hey guys I’m working on my first complete game in the Godot Engine. I want it so that when I go in the goal (All the goals in each level(scene) is attached to one script) that it goes from Level 1.xml to Level 2.xml and Level 2.xml to Level 3.xml, and etc. So here’s how I thought I could do it;
"

#Not actual syntax
func ready():
  var currentScene = get_tree().get_current_scene().get_name()


func _on_Goal_body_enter( body ):
 if body.is_in_group("Player"):
		if (currentScene == "res://Scenes/Levels/Level 1.xml"):
			get_tree().change_scene("res://Scenes/Levels/Level 2.xml")
		if (currentScene == "res://Scenes/Levels/Level 2.xml"):
			get_tree().change_scene("res://Scenes/Levels/Level 3.xml")

"

Obviously that’s not the actual code, and even when the syntax was correct it didn’t work. Please Help.

:bust_in_silhouette: Reply From: volzhs

Try to use Autoload
http://docs.godotengine.org/en/latest/tutorials/step_by_step/singletons_autoload.html?highlight=global

And you can make global.gd like this

var current_stage = 1

func go_next_stage():
    current_stage += 1
    get_tree().change_scene("res://Scenes/Levels/Level "+str(current_stage)+".xml")

call it from scene

func _on_Goal_body_enter( body ):
    if body.is_in_group("Player"):
        global.go_next_stage()

I would not put this in a singleton, because if you want to test level 10 right away, reaching the end would teleport you to level 2 unless you set the number in the global somehow.
Anyways I avoid singletons for the same reason I avoid global static things in any program :stuck_out_tongue:

What I do instead is to have a next_level property inside my level_end object in the level, so choosing what is next is easier and doesn’t requires a naming convention. So when you test any level, the game can continue as normal.

As a little bonus, it also allows a level to have multiple endings. But maybe you won’t need it :slight_smile:

Zylann | 2016-09-11 14:11

…just for link update (on singleton)

Singletons (Autoload) — Godot Engine (stable) documentation in English

KaiAdrian | 2021-06-13 16:12