wait for change_scene/reload_current_scene to finish before running function

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

i’m trying to mak a game, you can place down stuff

i made it so you can save stuff by basically making a copy of the main scene
issue is, i want all the objects inside to update and use a newer version of the code when loaded (basically update the savefile to the newer game version changes in the script)

to do this i tried to:
-make a new main scene (main also changes so i have to do this)
-make new children based on the old scene and copy the old children’s properties
-change to the new modified main scene

but the problem is the game doesn’t wait for the scene to refresh/change before continuing with the code, making all the new children get added to the old instance and, therefore, getting deleted

how can i wait for change_scene to finish?

:bust_in_silhouette: Reply From: Gluon

To be honest I am not 100% certain I fully understand what you are trying to do here but I think the yield and resume functions will be able to do what you want. Yield basically pauses a function while something happens and resume allows the function to continue. You can see the godot docs here

and this is a good resource to help understand yield.

https://gdscript.com/solutions/coroutines-and-yield/

Hope that helps

i copy the whole scene and its children, the children have scripts
so when i load the save in a version where said children are modified they’ll still use old code

so i want to change the scene to a fresh main scene and make new children with the old children’s data

i need to wait for the change_scene function to fully load the new Main instance before adding the new children

yielding did work, thanks

what i did:

yield(get_tree().current_scene, "tree_exited") #this is so that we wait for the old scene to unload
yield(tree, "idle_frame") # wait for an idle frame so that it completes

lettucing | 2022-03-08 21:44