0 votes

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?

in Engine by (416 points)

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.

2 Answers

+2 votes
Best answer

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.

by (297 points)
selected by
+2 votes

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:

remove_child(current_level)
var packed_scene = load("res://...")
var next_level = packed_scene.instance()
next_level.data = current_data
add_child(next_level)
current_level = next_level
by (22 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.