how to go from scene 1 to scene 2 and then back to scene 1 right where i left off

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

i know how load scene one and how to change scenes using
using get_tree().change_scene but what i am having trouble with is

when i go from scene 1 to scene 2 and back to scene 1 it starts back at 0,0 not where i
was when i called scene 2

any example (with code ) or a link to video please

:bust_in_silhouette: Reply From: Gluon

This would be a bit more complex, you would need a global script and when you go from scene 1 to scene 2 you would need to save the global position of the character into a variable in that script. You would also need code in scene 1 to check if you need to load the character into a new position so probably a boolean in the global script again which would also have been set to true when you saved the global position.

I dont know of any videos and its hard to write this much code in here but hopefully the above will give you some ideas.

If you need to know how to create a global script you can go project>project settings>autoload and in here you can set a script to autoload. This will mean that any script set to autoload will load up in every scene.

:bust_in_silhouette: Reply From: Wakatta

IMHO get_tree().change_scene() is a very horrible function and change_scene_to is not any better because they both use PackedScene’s and not instances.

It’s for this reason you should managed scene changes yourself

  • In your main scene keep a reference to all scenes
  • Create a scene change function
  • Use one of two options to change your scene
  1. Add and Remove Scene nodes
  2. Replace Scene nodes with others

Option 1

var scene_one = load("scene_one").instance()
var scene_two = load("scene_two").instance()
var current_scene = null

func _ready():

func switch_scene(new_scene):
    if new_scene.is_inside_tree():

    #Add new scene below old scene to keep 
    #the same index once old_scene is removed
    if current_scene and current_scene.is_inside_tree():
        add_child_below_node(new_scene, current_scene)
    current_scene = new_scene

Option 2

func switch_scene(new_scene):
    if new_scene.is_inside_tree():

    if current_scene and current_scene.is_inside_tree():
    current_scene = new_scene

Then from anywhere you can call


In the above examples the scenes are kept in their respective variables as is and can even be manipulated (for example a score change in scene_two can be updated in scene_one)