how can i hold data from scene to scene other than use autoload?

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

how can i hold data from scene to scene?

for example if i have skill cooldown, i want my cd keep decrease form old scene, what method i should use?

what is different between save at exist scene then load it back to next scene and using autoload?

If you’re wondering about saving a scene and then loading it back, versus autoloading it (or some similar data) is that the autoloaded scene is already ready to go, while it may take a while to save and load that scene.

Ertain | 2021-05-23 23:34

:bust_in_silhouette: Reply From: archeron

Autoload is the way to go here.

You could save the skill cooldown value in a file somewhere in the user:// path (you can’t save things to res://), but I/O is some of the slowest stuff you can do on a computer. You don’t want to do I/O in the middle of the game if you can avoid it. What’s even worse is that just saving a single cooldown value (possibly a single float) is extremely inefficient - you’ll need to open and close a file just to write something like four bytes to it, and the operating system overhead for opening and closing files is huge. Finally, if you’re running on an SSD or some kind of flash memory and your OS doesn’t automatically batch writes, you’ll degrade your flash memory, especially if you have many scene switches (that’s most likely not a problem and rather theoretical, but it’s still something to keep in the back of your mind if you plan on doing lots of writes to disk).

Using Autoload instead will keep the data in memory for you and sidestep all these issues.

Only store the cooldown value to disk when you want to quit the game and restart it having the same cooldown state, and store it with a bunch of other stuff that needs to be saved so the overhead you incur is worth it.

A third option might be to send the cooldown value to a server if you’re writing a networked application, but again, this hardly seems worth it just to keep a value when switching scenes. The network overhead is huge, and the network might not be there when you need it. This option mainly applies when you write a multiplayer game which needs to update game state all the time anyway.

:bust_in_silhouette: Reply From: chotto

Autoload is a great way to do this but you can also manage scenes inside your current one. That is, you don’t use SceneTree to change scenes but instead you instance them as nodes, add them as children, and pass data to them as properties.

You could do something like this:

var packed_scene = load("res://...")
var next_level = packed_scene.instance() = current_data
current_level = next_level