• Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office
AnonyViet - English Version
  • Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office
No Result
View All Result
  • Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office
No Result
View All Result
AnonyViet - English Version
No Result
View All Result

[Tạo 2D Platformer Game với Godot] Part 29: Saving and loading game data

AnonyViet by AnonyViet
January 26, 2023
in Tips
0

In this section, I will show you how to save and load user data for the game.

Join the channel Telegram of the AnonyViet 👉 Link 👈

Save and load

Steps

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 as 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.

Autoload

[Tạo 2D Platformer Game với Godot]  Part 29: Saving and loading game data 7

I created a new script named DuLieu.

[Tạo 2D Platformer Game với Godot]  Part 29: Saving and loading game data 8 [Tạo 2D Platformer Game với Godot]  Part 29: Saving and loading game data 9

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 1 Section, if you look at the above code, you will see the key here is diemso,unlockedmap.

+ value: This is the value you need to assign to the key.

Then save the file again.

Load Game

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.

In 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")

AreaVucDiTrans

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()

Result

After running the game, you finish playing 1 game and then log out and you will see it saved and loaded game data.

[Tạo 2D Platformer Game với Godot]  Part 29: Saving and loading game data

You can see the file_dulieu has been created.

[Tạo 2D Platformer Game với Godot]  Part 29: Saving and loading game data 10

You can find the file location in Project -> Open Project Data Folder

Summary

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:

DeathGM/platformer-game-elephant-godot-engine

The article achieved: 5/5 – (100 votes)

Tags: dataGameGodotloadingPartPlatformerSavingtạovới
Previous Post

Lesson 18: Percentage – Percentage – Basic Excel

Next Post

If you want to hack Windows you need to know these CMD commands

AnonyViet

AnonyViet

Related Posts

How to add sliders to Facebook Stories to easily rewind videos
Tips

How to add sliders to Facebook Stories to easily rewind videos

April 21, 2026
How to change the default font on Windows 10
Tips

How to change the default font on Windows 10

April 13, 2026
5 tips for using a Browser to replace an App (helps save RAM, time and money)
Tips

5 tips for using a Browser to replace an App (helps save RAM, time and money)

April 13, 2026
How to make funny MeMe photos without Photoshop within 10 seconds
Tips

How to make funny MeMe photos without Photoshop within 10 seconds

April 11, 2026
How to quickly design your own Logo without Photoshop
Tips

How to quickly design your own Logo without Photoshop

April 10, 2026
How to convert Website into App on Windows
Tips

How to convert Website into App on Windows

April 9, 2026
Next Post
If you want to hack Windows you need to know these CMD commands

If you want to hack Windows you need to know these CMD commands

0 0 votes
Article Rating
Subscribe
Login
Notify of
guest

guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Recent News

Instructions for looking up prescriptions on VNeID instead of paper medical books

Instructions for looking up prescriptions on VNeID instead of paper medical books

April 26, 2026
Tips to completely turn off CAPTCHA codes on iPhone and Mac are super simple

Tips to completely turn off CAPTCHA codes on iPhone and Mac are super simple

April 25, 2026
Compare Poco X8 Pro and Poco X7 Pro: A new step for the “king” of mid-range performance

Compare Poco X8 Pro and Poco X7 Pro: A new step for the “king” of mid-range performance

April 24, 2026
How to compress online videos for free without losing quality

How to compress online videos for free without losing quality

April 24, 2026
Instructions for looking up prescriptions on VNeID instead of paper medical books

Instructions for looking up prescriptions on VNeID instead of paper medical books

April 26, 2026
Tips to completely turn off CAPTCHA codes on iPhone and Mac are super simple

Tips to completely turn off CAPTCHA codes on iPhone and Mac are super simple

April 25, 2026
Compare Poco X8 Pro and Poco X7 Pro: A new step for the “king” of mid-range performance

Compare Poco X8 Pro and Poco X7 Pro: A new step for the “king” of mid-range performance

April 24, 2026
AnonyViet - English Version

AnonyViet

AnonyViet is a website share knowledge that you have never learned in school!

We are ready to welcome your comments, as well as your articles sent to AnonyViet.

Follow Us

Contact:

Email: anonyviet.com[@]gmail.com

Main Website: https://anonyviet.com

Recent News

Instructions for looking up prescriptions on VNeID instead of paper medical books

Instructions for looking up prescriptions on VNeID instead of paper medical books

April 26, 2026
Tips to completely turn off CAPTCHA codes on iPhone and Mac are super simple

Tips to completely turn off CAPTCHA codes on iPhone and Mac are super simple

April 25, 2026
No Result
View All Result
  • Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office

wpDiscuz
0
0
Would love your thoughts, please comment.x
()
x
| Reply