0 votes

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:
$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.

Godot version 3.2.2
in Engine by (36 points)

2 Answers

+1 vote
Best answer

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.

by (53 points)
selected by

Thanks for the answer it helped alot

+1 vote

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.

by (14,788 points)

Thankyou for the answers this helped alot

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.