How to switch back to the previous scene?

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

Hello everyone,

I currently making a game and in this I have a shop.

And when I press esc change the scene to a check if I really want to go back to main menu.

So its like a ‘Do you really want to quit’ scene…

And then I have a global script in which i have thevar prescene = null.

In my Shop script i set this var to Global.prevscene = get_tree().current_scene

But if I click on ‘No, I dont want to quit’ and it should change the scene back to get_tree().change_scene(Global.prevscene) but I get the error:

Invalid type in function ‘change scene’ in base ‘SceneTree’ Cannot convert argument 1 from Object to String

What can I do?

:bust_in_silhouette: Reply From: jgodfrey

There are lots of ways to manage and switch scenes. The issue here is that change_scene() expects its argument to be a String, containing the resource path of a scene. And, since that’s not what’s returned by the call to current_scene (which returns a Node), you get the error you show.

So, to use change_scene, you need the resource path to the scene in question. You can get that via get_tree().current_scene.filename.

So, one way to make this work is to change this:

Global.prevscene = get_tree().current_scene

to this:

Global.prevscene = get_tree().current_scene.filename

Gönner Ehre yeeeeeeeee

NotImportant | 2022-02-25 00:14