kidscancode / godot_recipes

Lessons, tutorials, and guides for game development using the Godot game engine.
MIT License
234 stars 38 forks source link

[Discussion] Basics: Saving/loading data #13

Open cbscribe opened 3 years ago

cbscribe commented 3 years ago

Discussion for http://godotrecipes.com/basics/file_io/

ota200 commented 3 years ago

where would u put the code for the file in like for example the player code?

aphilip442 commented 3 years ago

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). This might explain why you see “What if I want to use JSON to save my data?” so often. Could you explain the pros/cons of using store_var/get_var directly?

aphilip442 commented 3 years ago

So... JSON cannot store Vector data properly, as it stores them as strings (https://github.com/godotengine/godot/issues/11866). Funny how these things are not documented.

cbscribe commented 3 years ago

I suppose, although it's documented in the JSON spec, which has nothing to do with Godot at all.

cbscribe commented 3 years ago

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

That's been changed in a recent PR, although it hasn't been backported to the 3.x documentation yet.

aphilip442 commented 3 years ago

youtube to the rescue when the official doc fails you: https://www.youtube.com/watch?v=ldKFOGRQDzo (tldr: use ResourceSaver)

Saitodepaula commented 3 years ago

That's been changed in a recent PR, although it hasn't been backported to the 3.x documentation yet.

Could you provide a link to this PR please?

cbscribe commented 3 years ago

Actually, I was mistaken, it looks like it wasn't merged: https://github.com/godotengine/godot-docs/pull/4698

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.

levilindsey commented 3 years ago

:( 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. https://github.com/godotengine/godot/issues/49430

levilindsey commented 3 years ago

From discussion on the store_var/get_var issue I opened (https://github.com/godotengine/godot/issues/49430#issuecomment-857202333), 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 _get_property_list() in order to configure PropertyUsageFlags for the specific member variables that will be stored.

WinterLord15 commented 3 years ago

Thanks for the article! This was really helpful for me, 'cause first I thought I need to work with JSON, but now when I saw the in-built Godot save functions, and I discovered Godot HAS inbuilt base64 and sha256 descripting, I think I don't need to worry in the future about my games data saving and descripting. Thanks for it again!

Limofeus commented 3 years ago

@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!

uberfig commented 3 years ago

how would I do this with a dictionary?

cbscribe commented 3 years ago

store_var() saves dictionaries too.

uberfig commented 3 years ago

Thank you!

Sousio commented 2 years ago

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.

partisani commented 2 years ago

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!

PaintMeteor commented 2 years ago

The c.new() method puts it in memory, but it's not handled and deleted automatically. That may cause a memory leak.

zyq0825 commented 1 year ago

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 😭

cbscribe commented 1 year ago

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.