Reload scene while variable remains the same

Godot Version

4.2.2

at the moment my game works so that when the game is started a random number of objects are spawned when all objects are removed by the player the scene is reloaded so that new objects spawn. but now i want to add a score the problem is that the variable that is responsible for the score is restarted with the scene and the score is again set to 0 everything else works.
So my question is how can i keep the variables the same when the scene is reloaded or how can i re-spawn the objects without reloading the scene.

the code to spawn the objects:


and this is the code for reloding the szene:

make the var a global var so that no matter what scene your in it will always be the same until changed you can do that by making a new script than adding said script to the project autoload

If you want things to happen only once use the _init function.

_ready will be called Everytime you add the node to the tree.

So if you remove and add child, _ready will be called again, but _init will not.

reload_current_scene() will delete the scene and reinitialize it, in this case everything will be lost. I would avoid this function and have a scene manager handle your scene you care about and save the values before reloading the scene.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.