• 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

Collection of Windows 11 wallpapers for computers and phones
Tips

Collection of Windows 11 wallpapers for computers and phones

July 26, 2026
Don’t install leaked Windows 11
Tips

Don’t install leaked Windows 11

July 25, 2026
Microsoft announced the end of support date for Windows 10
Tips

Microsoft announced the end of support date for Windows 10

July 24, 2026
Microsoft confirms leaked Windows 11 is real
Tips

Microsoft confirms leaked Windows 11 is real

July 22, 2026
HeroSMS là gì? Cách thuê số điện thoại ảo nhận SMS OTP online không cần SIM
Tips

HeroSMS là gì? Cách thuê số điện thoại ảo nhận SMS OTP online không cần SIM

July 22, 2026
How to download documents on 123doc for free
Tips

How to download documents on 123doc for free

July 21, 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

Recent News

How to delete hidden files on Samsung phones, free up device space

How to delete hidden files on Samsung phones, free up device space

July 27, 2026
Collection of Windows 11 wallpapers for computers and phones

Collection of Windows 11 wallpapers for computers and phones

July 26, 2026
Don’t install leaked Windows 11

Don’t install leaked Windows 11

July 25, 2026
Instructions on how to get 1 year of VIP Hidely VPN for free

Instructions on how to get 1 year of VIP Hidely VPN for free

July 25, 2026
How to delete hidden files on Samsung phones, free up device space

How to delete hidden files on Samsung phones, free up device space

July 27, 2026
Collection of Windows 11 wallpapers for computers and phones

Collection of Windows 11 wallpapers for computers and phones

July 26, 2026
Don’t install leaked Windows 11

Don’t install leaked Windows 11

July 25, 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

How to delete hidden files on Samsung phones, free up device space

How to delete hidden files on Samsung phones, free up device space

July 27, 2026
Collection of Windows 11 wallpapers for computers and phones

Collection of Windows 11 wallpapers for computers and phones

July 26, 2026
  • Home
  • Home 2
  • Home 3
  • Home 4
  • Home 5
  • Home 6
  • Next Dest Page
  • Sample Page

say88 tỷ lệ kèo nhà cái kèo nhà cái 5 febet

No Result
View All Result
  • Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office

say88 tỷ lệ kèo nhà cái kèo nhà cái 5 febet

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