I have a question about using var_to_str() and str_to_var() for saving and loading game objects to a file.

Godot Version

4.1.1

Question

I’ve searched for countless ways to save game objects to a file in Godot. I see that using var_to_str() in my save function and str_to_var() in my load function both using a FileAccess object allows me to save my custom object node to a text file and subsequently read data from it upon loading it successfully.

I’m afraid to fully implement this further due to the fact that I haven’t seen a single tutorial mention this. This is the only solution that appears to work for me so far and every other solution either fails for me, has security vulnerabilities (malicious code injection to saved resource files), or requires too much set up for my custom game objects (creating dictionaries that contain a reference to each property of a complex custom data type). var_to_str() and str_to_var() seem to be a promising simple way to accomplish my goals.

Is there any reason NOT to use these functions for saving and loading custom game objects to files??

If theres no security risk, theres only the question of file-size, but if you dont care about that you can do that of course.

You might also want to consider var_to_bytes() and bytes_to_var() as you can also compress PackedByteArrays to reduce size

You would use from FileAccess the following methods:

FileAccess

open_encrypted(path: String, mode_flags: ModeFlags, key: PackedByteArray) static

FileAccess

open_encrypted_with_pass(path: String, mode_flags: ModeFlags, pass: String) static

There is an example how to use it with encryption.

1 Like