Save / Load Objects From Config File

Godot Version

4.3

Question

I am working on saving and loading for my game, and that includes trying to save not only multiple SceneTreeTimers, but also an array of Curve objects. Now this breaks the config file parser even for subsequent non-object items when trying to load them.
I tested the SceneTreeTimers using both var_to_str(), var_to_bytes() and var_to_bytes_with_objects() in a test-project opposed to saving the objects by themselves like I had before and all three wouldn’t work.

Is there a way to save objects in a config file? Or do I have to save all the relevant data and then reconstruct them upon loading?

Thanks.

Have you looked at using resources here is tutorial How to Save and Load Godot Game Data.

2 Likes

I have tested using resources, and that has worked since the Curve objects are now saved as sub-resources and that stops the parser breaking. But the SceneTreeTimers are not and therefor still break the parser.
Is there a way to make the SceneTreeTimers be saved as sub-resources like the Curves? Since if so I think this would solve this.

Save the scene as a whole and use Timer instead of SceneTreeTimer, or have those crucial tasks restart.

1 Like

Good point, It hadn’t crossed my mind to just not save them honestly.
Since these can be easily reconstructed using a already made function, I’ll just save a null/empty value and then remake them before setting everything upon loading.
Thanks! Will definitely be using resources to save over config files in the future for stuff like this.

You need to recreate or create a ref to because they disappear when completed.

From the docs

The timer will be dereferenced after its time elapses. To preserve the timer, you can keep a reference to it. See RefCounted.

I was vouching for saving the whole node because you can actually do that. Just save the entire scene node to disk and you’ll have everything you need there. When you load it the next time, you just have to take into account to start the scene tree timers in ready.

These timers are not used by nodes is the issue; their references are stored inside dictionaries and their timeout signal is connected to functions to give them their functionality, so their isn’t a node to save them too.

var objects : Array[Dictionary]
 = [{"name": "my_name", "timer": SceneTreeTimer()}]

Unless I saved all the timers under the node holding the dictionaries, which I could do but it seems simpler for what I need them for to save them as null and then remake them later, means I don’t have to instantiate a whole bunch of timers. But if these timers were used by individual nodes I agree that would be the better option.

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