In this blog post, we will show you the “Save and load” function currently used in our game.
This function will save all of a script’s variables as a single dictionary, no need to add variables manual.
Node setup
In player Node, we have a node called “character_stats” which hold player stats in its own GD script. This helps in keeping the code clean and share the same stats with other characters.
Save function
The first step is to collect variable name and value in to a dictionary.
# This will make it easier to call the character_stats node
@onready var CS = $character_stats
func game_save():
# As Vector2 is not supported by JSON we have to split x and y values
CS.pos_x = get_position().x
CS.pos_y = get_position().y
# Assings object's Script instance to a variable
var thisScript: GDScript =CS.get_script()
# list of variables you want to skip
var skip_var = ["character_stats.gd"]
var property_list ={} # Empty dictionary
#this will Go through everything in the object's Script instance
for propertyInfo in thisScript.get_script_property_list():
# this will skip any variable that is in the skip_var list
if propertyInfo.name in skip_var:
continue
#stors variable name as key and value as value of that key
property_list[propertyInfo.name] = CS.get(propertyInfo.name)
Now save this data to a file in JSON format.
#this will open savegame file
var save_game = FileAccess.open("user://savegame.dat", FileAccess.WRITE)
# var save_game = FileAccess.open_encrypted_with_pass("user://savegame.dat", FileAccess.WRITE, "password") # for encrypted file
# JSON provides a static method to serialized JSON string.
var json_string = JSON.stringify(property_list)
# Store the dictionary as a new line in the savegame file.
save_game.store_line(json_string)
Load function
Open the savegame file and read the data.
func game_load():
# open file in read mode
var save_game = FileAccess.open("user://savegame.dat", FileAccess.READ)
# var save_game = FileAccess.open_encrypted_with_pass("user://savegame.dat", FileAccess.READ, "password") # for encrypted file
var json_string = save_game.get_line()
# Creates the helper class to interact with JSON
var json = JSON.new()
# Check if there is any error while parsing the JSON string, skip in case of failure
var parse_result = json.parse(json_string)
if not parse_result == OK:
print("JSON Parse Error: ", json.get_error_message(), " in ", json_string, " at line ", json.get_error_line())
# Get the data from the JSON object
var new_nodedata = json.get_data()
Finally, reload the value from the dictionary.
var thisScript: GDScript = CS.get_script()
# list of variables you skipped while saving the data
var skip_var = ["character_stats.gd"]
for propertyInfo in thisScript.get_script_property_list():
if propertyInfo.name in skip_var:
continue
#Set value of the variables in CS(charactor_stats)
CS.set(propertyInfo.name,new_nodedata[propertyInfo.name])
#Finaly set the position
position.x = CS.pos_x
position.y = CS.pos_y
That’s it for this blog post. If you have any questions or insights, feel free to share them in the comments below.