How to transition to next level by using the integer in the level name

Godot Version

v4.1.2.stable.mono.official [399c9dc39]

Question

So I was following this old tutorial for godot 3 where it showed you could transition to the next level by taking the level name, finding the integer in it’s name, and adding +1 to it to load the next level. (https://www.youtube.com/watch?v=c2mkyW_TymY)

get_tree().change_scene_to_file(“res://scenes/level_” + str(int(get_tree().current_scene.name)+1) + “.tscn”)

Problem is this code no longer works for Godot 4. Any thoughts on how it could be converted to Godot 4?

Just convert int to str using

str(value)

I checked in my godot v4.3 and it worked:

func _input(event: InputEvent) -> void:
	if event is InputEventKey and event.keycode == KEY_SPACE:
		get_tree().change_scene_to_file("res://lvl_" + str(int(2)) + ".tscn")

Yes, I know you can convert int to string. That’s what: str(int(get_tree().current_scene.name)+1) was supposed to do. It gets the only integer value in the name and converts to a string. or at least that’s what it’s supposed to do.

Sorry only checked code from video, the problem is StringName cant be converted to int by int(), but it has a function .to_int() that does the same as int() so you just need to write it like this:

... + str(get_tree().current_scene.name.to_int()+1) + ...
1 Like

This still doesn’t return anything. I tried this and it’s only picking up the +1

What is your current_scene.name? Have you tried printing that out?

1 Like

Thank you, I realized the main node wasn’t named the same as the scene name so therefore it wasn’t obtaining any integers. Thank you so much!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.