In this section, I will show you how to save and load user data for the game.
Save and load
About the steps I will describe below:
+ First, I will need a new autoload script that is responsible for handling the problem of saving game data and loading game data. I use autoload to make it easy + no mess, then I create two functions, luu_game() and load_game() .
+ In the luu_game() function, I will save it under the ConfigFile type instead of the Json file like some Youtube channels do because it’s compact and not as verbose as the json file.
+ When saving the game, you will be able to choose to save normally and save as encrypted. So what is encrypted storage? That’s when you save encrypted your game save file will be re-encrypted and users can’t open it and edit it to cheat.
+ The load_game() function, I will create a variable and this variable has 2 responsibilities: both checking that the file to save the data exists and loading the file to save the data.
I created a new script named DuLieu.
Then go to Autoload and add the DuLieu script.
Save the game
I declare a const variable containing the address to save the data file.
const SAVE_PATH = "user://file_dulieu.data"
Data files you can save with different extensions such as data,ini,abc,jqk,cfg,file,… And it’s up to you.
Regarding the function:
func luu_game(): var config = ConfigFile.new() config.set_value("player","diemso",PlayerData.diemso) config.set_value("player","unlocked_maps",PlayerData.Unlocked_map) config.save(SAVE_PATH)
I create a new Config file and then I put the score data and unlocked maps into that file.
LIVE set_value() there will be 3 praramters:
+ The first one is Section: This is the data zone, each data zone will have its values.
+ Key: I call this the value stored in a Section, if you look at the above code, you will see that the key here is diemso,unlockedmap.
+ value: This is the value you need to assign to the key.
Then save the file again.
func load_game(): var config = ConfigFile.new() var check_file = config.load(SAVE_PATH) if check_file != OK: # Neu file khong bi loi hoac khong co return # Huy bo load file PlayerData.diemso = config.get_value("player","diemso") PlayerData.Unlocked_map = config.get_value("player","unlocked_maps")
In the game load, I declare a variable as check_file and then assign it = confg.load(SAVE_PATH) when running, config it will load the file + with assigning the file to check_file.
Then I check if the file is OK then continue and then get the value from the save file.
In the Menu I will add 2 lines.
func _ready(): DuLieu.load_game()
I call DuLieu then load_game() so that when I first run the game, I load the data.
At EndLevelMenu I will remove the score calculation code and add it in KhuVucDiChuyen for compactness.
extends CanvasLayer onready var level = get_parent().get_node("KhuVucDiChuyen") func _on_NutLevelKeTiep_pressed(): get_tree().paused = false get_tree().change_scene(level.LevelKeTiep) pass # Replace with function body. func _on_NutChoiLai_pressed(): get_tree().paused = false get_tree().reload_current_scene() pass # Replace with function body. func _on_NutVeMenu_pressed(): get_tree().paused = false get_tree().change_scene("res://Scences/Map/LevelMap.tscn")
In here, I will calculate everything and save the data.
extends Area2D export (String,FILE) var LevelKeTiep onready var endlevelmenu = get_parent().get_node("EndLevelMenu") var diemso onready var player = get_parent().get_node("Player") onready var map = get_parent().name func _on_KhuVucDiChuyen_body_entered(body): if body.name == "Player": endlevelmenu.get_node("EndLevelMenu").show() get_tree().paused = true PlayerData.Unlocked_map[get_parent().name] = true diemso = int( float(player.dongxu /player.max_dongxu.size()) * 100) if diemso > PlayerData.diemso[map]: PlayerData.diemso[map] = diemso DuLieu.luu_game()
After running the game, you finish playing 1 game and then log out and you will see it saved and loaded game data.
You can see the file_dulieu has been created.
You can find the file location in Project -> Open Project Data Folder
So the Platformer game programming series with Godot Engine has been completed, if you do not understand, you can ask in the community.
I will put the project below: