How to make a Cross scene Respawn Point?

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

Hi,
I’ve been doing Godot for a little while now. I have come across a problem and have no clue how to solve it. I need to make a spawn point that sends you back to an old scene when you die. E.G You save in scene 1 go to scene 2 and it sends you back to scene 1’s respawn point.

(Im saving the SavePos and SaveScene in an Autoloaded script)

For the respawn point i have:

func _on_Area2D_body_shape_entered(body_id: int, body: Node, body_shape: int, area_shape: int) -> void:
if body.name == "Player":
	Global.SavePos = position
	Global.SaveScene = get_tree().get_current_scene()
	used = true

For in the root nodes script i have:

func _on_Stalactite_hurt_enemy() -> void:
get_tree().change_scene(Global.SaveScene)
$Player.position = Global.SavePos

This script saves the current scene and the position in a Autoload script and then when you walk into the Stalactite you get sent back to the scene and the position. When this happens though you get sent to a blank scene with nothing there.
Something i found is that when you remove the change_scene you get sent to the right position its just the scene that’s not working.

If you need the scene tree just ask, i don’t know if its relevant or not and honestly can’t be bothered adding it rn.

Thankyou for any help you can give me.

:bust_in_silhouette: Reply From: twi

Your problem is that, as get_current_scene() is a reference to a Node within the SceneTree, if that Node is ever destroyed i.e during a scene change the reference is lost. You should use a duplicate() or instance() instead.

EDIT : here’s an MRP: {link}
Click the green Godette to get the current SavedPos and SavedScene, run into the red Godette (arrow keys) to “kill yourself”, (nothing else happens but that the current scene is stored; this is to demonstrate that changing scenes is what destroys the reference), and click the scene label to switch between scenes.
Godot is actually smarter than I realized in that it will return “[Deleted Object]” rather than simply “Null” if you try to access a node/object under these circumstances.

Thanks for the answer it helped alot

Martogh | 2020-12-17 03:23

:bust_in_silhouette: Reply From: jgodfrey

It looks like you’re calling change_scene() with a scene Node (returned from get_current_scene(). Looking at the docs, change_scene() expects a node path (so, a string), not a Node.

You can get the node path from a scene via it’s filename property.

Thankyou for the answers this helped alot

Martogh | 2020-12-17 03:22