Thứ Ba, Tháng Tám 9, 2022
  • Home
  • Health
  • News
  • Software
  • Tech
  • Travel
  • Gen Z
NATuts
  • Home
  • Health
  • News
  • Software
  • Tech
  • Travel
  • Gen Z
No Result
View All Result
NATuts
  • Home
  • Health
  • News
  • Software
  • Tech
  • Travel
  • Gen Z
No Result
View All Result
NATuts
No Result
View All Result
Home Tech

Save and load game data

9 Tháng Mười Hai, 2021
in Tech
0
Save and load game data
585
SHARES
3.3k
VIEWS
Share on FacebookShare on Twitter

Các bài viết liên quan:

Download 3DVista Virtual Tour 2019

Download 3DVista Virtual Tour 2019

8 Tháng Tám, 2022
Nha Trang Stone Bridge Port

Nha Trang Stone Bridge Port

8 Tháng Tám, 2022
Taiwan is about to rehearse the anti-occupation of the island

Taiwan is about to rehearse the anti-occupation of the island

8 Tháng Tám, 2022
Instructions for using SMS OTP V3 spam tool

Instructions for using SMS OTP V3 spam tool

8 Tháng Tám, 2022
How to Do Venmo Identity Verification [2022 Guide]

How to Do Venmo Identity Verification [2022 Guide]

8 Tháng Tám, 2022
Meta disrupts cyber espionage in South Asia Facebook abuse

Meta disrupts cyber espionage in South Asia Facebook abuse

8 Tháng Tám, 2022

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

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

I created a new script named DuLieu.

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

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.

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.

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

AreaVucDiTransition

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 28

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

Previous Post

Instructions for building a server at home to use as a VPS to run a website.

Next Post

Download Nik Collection by DxO 4

Admin Natuts

Admin Natuts

Related Posts

Instructions for using SMS OTP V3 spam tool

Instructions for using SMS OTP V3 spam tool

8 Tháng Tám, 2022
How to Do Venmo Identity Verification [2022 Guide]

How to Do Venmo Identity Verification [2022 Guide]

8 Tháng Tám, 2022
Meta disrupts cyber espionage in South Asia Facebook abuse

Meta disrupts cyber espionage in South Asia Facebook abuse

8 Tháng Tám, 2022
Instructions to install Outline VPN on Ubuntu

Instructions to install Outline VPN on Ubuntu

7 Tháng Tám, 2022
What is Conhost.exe and why is it running on my computer

What is Conhost.exe and why is it running on my computer

7 Tháng Tám, 2022
How to make the background transparent in Canva

How to make the background transparent in Canva

7 Tháng Tám, 2022
Load More
Next Post
Download Nik Collection by DxO 4

Download Nik Collection by DxO 4

Trả lời Hủy

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *

Bài viết mới

Download 3DVista Virtual Tour 2019
Software

Download 3DVista Virtual Tour 2019

8 Tháng Tám, 2022
Nha Trang Stone Bridge Port
Travel

Nha Trang Stone Bridge Port

8 Tháng Tám, 2022
Taiwan is about to rehearse the anti-occupation of the island
News

Taiwan is about to rehearse the anti-occupation of the island

8 Tháng Tám, 2022
Instructions for using SMS OTP V3 spam tool
Tech

Instructions for using SMS OTP V3 spam tool

8 Tháng Tám, 2022
How to Do Venmo Identity Verification [2022 Guide]
Tech

How to Do Venmo Identity Verification [2022 Guide]

8 Tháng Tám, 2022
Meta disrupts cyber espionage in South Asia Facebook abuse
Tech

Meta disrupts cyber espionage in South Asia Facebook abuse

8 Tháng Tám, 2022
W3Schools

Ads

Contact: [email protected]

DMCA.com Protection Status

Categories

  • Android
  • Cạm bẫy tâm lí
  • Chưa được phân loại
  • Đồ họa
  • Đời sống
  • Gen Z
  • Health
  • iOS
  • Kĩ năng mềm
  • News
  • Nhà mạng
  • Phần mềm
  • Phần mềm đồ họa
  • Review sách
  • Software
  • Tech
  • Thiết kế ảnh
  • Thiết kế video
  • Thủ thuật
  • Travel
  • Văn hóa Nam Bộ
  • Văn học
  • Window

Browse by Tag

ai là triệu phú android Apple browser Bullet Journal bản thân Chỉnh ảnh data domain download du lịch fshare game game show hosting HÌNH XĂM IKEA ios khuyến mãi kinh doanh kiến thức kiểm tra pin lừa đảo messenger miễn phí mua sắm Máy ảnh mạng mồi tiềm thức network nghệ thuật nhà Trần quảng cáo review tháp phân tầng xã hội tiếng anh tiện ích Trần Thủ Độ tên miền từ vựng viettel word xã hội Đơn giản đánh bạc

Recent News

Download 3DVista Virtual Tour 2019

Download 3DVista Virtual Tour 2019

8 Tháng Tám, 2022
Nha Trang Stone Bridge Port

Nha Trang Stone Bridge Port

8 Tháng Tám, 2022

Trang tin nóng hổi - vừa thổi vừa xem

No Result
View All Result
  • Home
  • Health
  • News
  • Software
  • Tech
  • Travel
  • Gen Z

Trang tin nóng hổi - vừa thổi vừa xem