• 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

Top 5 game programming languages ​​to learn now
Tips

Top 5 game programming languages ​​to learn now

June 8, 2025
[Godot Shooter] #2: Creating characters & shooting bullets
Tips

[Godot Shooter] #2: Creating characters & shooting bullets

June 7, 2025
What do you need to learn game programming? Is it difficult? How long does it take?
Tips

What do you need to learn game programming? Is it difficult? How long does it take?

June 6, 2025
Instructions for registering chatgpt team at $ 1
Tips

Instructions for registering chatgpt team at $ 1

June 5, 2025
How to engrave the right mouse menu error on Windows
Tips

How to engrave the right mouse menu error on Windows

June 5, 2025
How to create online meme photos is very easy with a few steps
Tips

How to create online meme photos is very easy with a few steps

June 5, 2025
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

Top 5 game programming languages ​​to learn now

Top 5 game programming languages ​​to learn now

June 8, 2025
The iPhone list is updated with iOS 26

The iPhone list is updated with iOS 26

June 8, 2025
Discover the glowing effect next to the iPhone ios 18 screen

Discover the glowing effect next to the iPhone ios 18 screen

June 8, 2025
[Godot Shooter] #2: Creating characters & shooting bullets

[Godot Shooter] #2: Creating characters & shooting bullets

June 7, 2025
Top 5 game programming languages ​​to learn now

Top 5 game programming languages ​​to learn now

June 8, 2025
The iPhone list is updated with iOS 26

The iPhone list is updated with iOS 26

June 8, 2025
Discover the glowing effect next to the iPhone ios 18 screen

Discover the glowing effect next to the iPhone ios 18 screen

June 8, 2025
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

Top 5 game programming languages ​​to learn now

Top 5 game programming languages ​​to learn now

June 8, 2025
The iPhone list is updated with iOS 26

The iPhone list is updated with iOS 26

June 8, 2025
  • Home
  • Home 2
  • Home 3
  • Home 4
  • Home 5
  • Home 6
  • Next Dest Page
  • Sample Page

©2024 AnonyVietFor Knowledge kqxs hôm nay xem phim miễn phí SHBET https://kubet88.yoga/ bj88

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

©2024 AnonyVietFor Knowledge kqxs hôm nay xem phim miễn phí SHBET https://kubet88.yoga/ bj88

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