Binary serialize class with FileAccess in c#
Can you provide, or link to an example of saving and restoring binary data using resources? I experimented with that, but couldn't figure it out.
- Edited
DaveTheCoder Can you provide, or link to an example of saving and restoring binary data using resources? I experimented with that, but couldn't figure it out.
Just save it as *.res instead of *.tres. ResourceSaver
determines the format according to extension.
class_name DeadParrot extends Resource
var pushing_up_the_daisies = true;
func _ready():
var polly = DeadParrot.new()
ResourceSaver.save(polly, "res://polly.res")
ResourceSaver.save(polly, "res://polly.tres")
Regarding the OP. I tried to do the same in GDScript but also got some funky errors. Didn't have time to investigate further. Object serialization is always tricky so better play it safe and go with engine's preferred system - and that's resource objects/files.
- Edited
Moep Yeah, for now i just serialize the properties one at a time with storevar() and then compose the class when loading back the simple property types.
No need to do it like this. It's error prone, tedious and non-scalable. Simply inherit the class from Resource
and use ResourceSaver.Save()
and ResourceLoader.Load()
to (de)serialize the object.
I know that resources are the easiest to handle solution, but this is why i shy away from them for now https://github.com/godotengine/godot-proposals/issues/4925
Also Godot provides an entire documentation page on saving and loading a game and binary serialization based on fA, so i don't think that resources are the one accepted/intended way to handle this.
- Edited
Moep Resources are not the only way. They are the easiest way though. The security problem is relevant only if you're handling user generated content shared between users.
Anyway, you should perhaps report this as an issue and see what happens, because it looks like it should work. I tried doing this in GDscript and it appears to be working in 3.5 but it's throwing class name shadowing error in 4.1