How to change scene to the text of a label

Godot Version

4.6.1

Question

so im trying to make a cutscene system for my game since i dont want to make a different script for every cutscene, i thought itd be smart to, when the animation is done, have a label, set its text to the path of whatever scene i wanna switch to and just switch to that scene, but it isnt working

this is my code:

extends Node3D

var nextscene = $SceneName.text

func _ready():
	$AnimationPlayer.play("cutscene")
	nextscene =  $SceneName.text


func _on_animation_player_animation_finished(anim_name: StringName) -> void:
	get_tree().change_scene_to_file(nextscene)

(the label is called SceneName, its text in this situation is “res://Assets/Scenes/MainMenu.tscn” which is the path to the main menu scene)

can anybody help me??? it does detect when the animation finishes but it doesnt switch scene

these are the 2 errors i get:

image

thanks in advance!!!

You can’t get child nodes without @onready, you do use the same code in _ready which would work fine, so you can remove the initial assignment.

However it would be best to use an @export variable, which can be assigned per-node similar to editing a label’s text, but it won’t be a new game object that potentially shows ingame.

@export_file will help you pick out files specifically.

@export_file("*.tscn") var next_scene: String