I'm making a platformer, and I want for the player to enter a door and enter the next level.
2 different things, but I feel like I'm doing them wrong.

1st attempt- I tried to change the scene my splitting the up the scene path I wanted to go to and editing the middle of it to change the scene. I named all my scenes numbers, so 1 can be added to them to go to the next.
Reason for failure- Error, cannot convert stringname to string! I have no clue why this is happening. This method seemed the best, so I hope I can make it work.

2nd attempt- I kept the current scene name as a variable, and added 1 to it. Then used that to change it with a similar process for the first attempt
Reason for failure- Error, Invalid get index 'current_scene' (on base: 'null instance').

Thanks in advanced!

  • Your error on first try is that godot cannot convert StringName to int directly so you have to convert it to string first (the error that godot puts out is missleading). This should work:
    var l = load("res://Level_"+str(int(str(get_tree().current_scene.name))+1))

    ... but overall i dont think its a elegant approach to code it that way.

I also have all the levels in one folder, if that matters.

on your 1st attempt you could try print() it out then see if it actually matches your level node's name.
*you'd also need to include folder path as well like "res://folder_name/Level_..."

    Gowydot
    It does, it prints "1" in level one (which is called 1) and so on.
    I don't get the error, if you could explain that to me I could probably fix it.

    The way I made a level changer in the past was to have this node setup :
    The advantage of this is that you will keep your player variables between levels.

    You basically do a modular level changer like this :

    extends Area3D
    
    @export var change_to_level_scene = "" #this is the full file path of the scene without the "", you can set it in the editor
    
    @onready var level_to_load_scene = preload(change_to_level_scene)
    
    var current_gameplay
    
    func _ready():
        if !body_entered.is_connected(body_entered):
            body_entered.connect(body_entered)
        current_gameplay = get_node("CurrentGameplay") #cant remember how to this, I'm not a home so I can't check
    
    
    
    func body_entered(body):
        if body.is_in_group("player"):
            for gameplay in current_gameplay.get_children():
                gameplay.queue_free()
            var level_to_load_instance = level_to_load_scene.instantiate()
            current_gameplay.add_child(level_to_load_instance) #you do need a reference to current_gameplay
            body.global_transform = level_to_load_instance.spawn_position #have your level scene have a position 3D that is the spawn position for the level so you can move the player to it

    Your error on first try is that godot cannot convert StringName to int directly so you have to convert it to string first (the error that godot puts out is missleading). This should work:
    var l = load("res://Level_"+str(int(str(get_tree().current_scene.name))+1))

    ... but overall i dont think its a elegant approach to code it that way.

      klaas
      Why wouldn't it be elegant (I would like to know for future cases)

      This makes sense though, I'll try it.

        Vickyboi Generally, the next step in coding is to use an array. So in your array of scenes, you might have
        scene_array = [ "opening", "living room", "out_door"] (descriptive names make code more readable)
        So when you need it, it's just scene_array[0], and you can add one or subtract or whatever.
        You put these in your autoload script so you can reach them from all scenes.
        If it works, there is nothing wrong with it, but arrays are really useful.

        I should have probably read this closer before posting because I just assumed when you use something like converting a number to a string, you are doing that kind of thing. Otherwise, never mind.