Storing a scene's filename at ready

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By SnapCracklins

Hello, I am trying to make a scene where, when the enemy “catches” the player, they go to a “chance screen” and if they survive, they return to the previous scene.

I have tried using get_tree().get_edited_scene_root.filename and keep getting “nil” errors. What’s a best practice for this? my scene changes are handling fine except for this one.

I am trying to use a global variable to store the scene using my door class in each room. I only have three rooms loaded at any one time so I don’t need a complex level-change system.

Singleton ## hold current scene if in kill_screen

var sceneJar = preload("res://assets/TESTROOM_00.tscn")

Then the “update” in each room, running at ready:

func room_store():
	_currentRoom = get_tree().get_current_scene().get_name()
	Global.sceneJar = _currentRoom

Then the “kill screen” function that should reload that room but fails to do so, because i keep getting errors: (which i don’t get, because i use this method with everything else) Note: my Global variable IS updated at the ready here.

func _ready():
       _escape_dest = Global.sceneJar

func escape():
	var _free = load(_escape_dest)
	var _free_me = get_tree().change_scene_to(_free)

I keep getting Resource not found errors but the filepaths it is showing are accurate. What is happening? Why does this method work for everything and not this? It’s loading the path, getting the data, it’s just not connecting the scenes.

Also: sorry if this question is common. I searched and kept finding deprecated answers.

:bust_in_silhouette: Reply From: jgodfrey

Your problem is likely that get_name() doesn’t return what you think it does. If you print that out, you’ll see it’s just the node’s unique name (like Area2D or Line2D). That’s not a valid value for loading a scene from the file system.

Depending on the details, I’m guessing you probably want the value of the filename property instead.

So, this:

 _currentRoom = get_tree().get_current_scene().filename

I was trying this method, was having some trouble with it but maybe it was my syntax. I will give it a go!

SnapCracklins | 2022-06-06 02:51