-
-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Discussion] Basics: Saving/loading data #13
Comments
where would u put the code for the file in like for example the player code? |
Hi, you advise to not use json files for saving data, while the official doc recommends to use the methods to_json and parse_json (in combination with store_line/get_line) to save games (https://docs.godotengine.org/en/stable/tutorials/io/saving_games.html). |
So... JSON cannot store Vector data properly, as it stores them as strings (godotengine/godot#11866). Funny how these things are not documented. |
I suppose, although it's documented in the JSON spec, which has nothing to do with Godot at all. |
That's been changed in a recent PR, although it hasn't been backported to the 3.x documentation yet. |
youtube to the rescue when the official doc fails you: https://www.youtube.com/watch?v=ldKFOGRQDzo (tldr: use ResourceSaver) |
Could you provide a link to this PR please? |
Actually, I was mistaken, it looks like it wasn't merged: Regardless, it's an outstanding issue, and needs to be changed. Recommending JSON in the tutorial leads beginners to think it's the only (and not the worst) choice. |
:( I really wish this worked, but AFAICT this article is advocating something that just doesn't work. Object serialization is not supported in Godot. I opened an issue to better illuminate whether this is truly working as expected, and whether the docs should make this more obvious. |
From discussion on the store_var/get_var issue I opened (godotengine/godot#49430 (comment)), it sounds like there is at least a way to make this pattern work. In any class we want to store (and presumably in any classes indirectly depended on through member variables), we need to override |
Thanks for the article! |
@WinterLord15 I have absolutely the same story as you do! I searched for tutorials on saving a lot of variables in one file with godot, but most of them were using dictionaries and JSON parsers, which i knew would result in poor performance + larger file sizes, but it seems that store_var() function was just what i have been looking for! |
how would I do this with a dictionary? |
|
Thank you! |
I had encountered only with JSON saving/loading in the documentation (except for the binary resources), so I didn't hesitate to test the built-in serializing as you've described, because it makes a lot more sense. However testing this with a simple dictionary variable resulted in a file twice the size, comparing to the JSON one. (96 bytes vs 44 bytes). So I just wonder why this happened, or whether it scales to big files/scenes as well. |
In the same place, you will find "Using custom resources", Here, and if you are smart enough ( no, i'm not saying that you are not ) you can use it for creating a save / load system! |
The c.new() method puts it in memory, but it's not handled and deleted automatically. That may cause a memory leak. |
So in the new Godot 4 saving/loading data article: https://kidscancode.org/godot_recipes/4.x/basics/file_io/index.html, they suggest us do not use JSON for save files. But in the Godot doc, the saving game tutorial is still using JSON for Godot 4.1: https://docs.godotengine.org/en/stable/tutorials/io/saving_games.html. GDQuest's video tutorial from last year said JSON is safer than resources: https://www.youtube.com/watch?v=j7p7cGj20jU. I'm just very confusing now... what should I use for save/load exactly 😭 |
Use what provides the functionality you need. JSON is not well-suited to storing arbitrary data types, that's why Godot itself doesn't use it. Resources can execute code, so that's a concern, yes, but not really a major one unless you are in a situation where your players are sharing game saves. That still leaves the native serialization as suggested in this tutorial - I would still recommend using it. Try things and see what works for you. The save game tutorial is old and nobody has had the energy to tackle updating it, so it remains. |
Discussion for http://godotrecipes.com/basics/file_io/
The text was updated successfully, but these errors were encountered: