JSON, which stands for JavaScript Object Notation, is a lightweight, text-based data format used for data interchange. It's easy for both humans to read and write, and for machines to parse and generate.
Change the file type to .json
To store more pieces of data at one time we can use JSON.
Create a dictionary variable called saved_data
Then create dictionary entries for values such as: player position and player health
Now convert the dictionary into JSON
Then write the JSON string to the file
Remove the previous file variable writes
A major benefit is that now order of variables is not important
since dictionaries work with key / value pairs
Note that JSON does not support Godot built-in types like Vector2
We must split the position into its component parts
Now update the load function
change the file type to .json
use the get_as_text function
to retrieve the json string
now convert the json string
This works but we have to be careful to manually parse Godot specific types (like Vector2) to work with JSON
To solve this we could save the dictionary and then read it back in a similar way to our simple saving technique. This is shown below
The issues which remain now are:
The data file is binary and difficult to debug since we cannot read it
We can easily make typos in the dictionary entries
Create Custom Resource for our saved game
Create a New Script called saved_game.gd
Set the class_name to SavedGame
Add variables to represent the data you want to save
such as player position and hearts
instance the SavedGame scene
then send values for player health and position
finally use the ResourceSaver to save the resource
Now we can also save and load dynamic game objects
In the SavedGame resource script
add an array to store the positions of
In the SaveAndLoad script
Create a loop to store the position
of every strawberry in the scene to the array