Share [Tạo 2D Platformer Game với Godot] Phần 29: Lưu và load dữ liệu game

bluefrog538

New member
ủng thuật lưu và tải ## lưu và tải

Trong phần này, tôi sẽ chỉ cho bạn cách lưu và tải dữ liệu người dùng cho trò chơi.

### Các bước

1. Tạo một tập lệnh mới có tên là `Dulieu`.
2. Truy cập `autoload` và thêm tập lệnh` dulieu`.
3. Trong tập lệnh `dulieu`, khai báo một không đổi chứa địa chỉ tệp dữ liệu.
4. Tạo một chức năng để lưu dữ liệu trò chơi.
5. Tạo một chức năng để tải dữ liệu trò chơi.

### autoload

1. Tạo một tập lệnh mới có tên là `Dulieu`.

`` `
Mở rộng nút
`` `

2. Truy cập `autoload` và thêm tập lệnh` dulieu`.

### Lưu trò chơi

1. Trong tập lệnh `Dulieu`, khai báo một không đổi chứa địa chỉ tệp dữ liệu.

`` `
const save_path = "người dùng: //file_dulieu.data"
`` `

2. Tạo một chức năng để lưu dữ liệu trò chơi.

`` `
func luu_game ():
var config = configFile.new ()
config.set_value ("người chơi", "diemso", playerdata.diemso)
config.set_value ("người chơi", "Unlocked_maps", playerdata.unlocked_maps)
config.save (save_path)
`` `

3. Gọi hàm `luu_game ()` trong hàm `_Ready ()` của cảnh `menu`.

`` `
func _Ready ():
Dulieu.luu_game ()
`` `

### Tải trò chơi

1. Tạo một chức năng để tải dữ liệu trò chơi.

`` `
func load_game ():
var config = configFile.new ()
var Check_file = config.load (save_path)
Nếu Check_File! = OK: # nếu không tìm thấy tệp
Trả về tệp tải Huy Bo
Playerdata.diemso = config.get_value ("người chơi", "diemo")
Playerdata.unlocked_maps = config.get_value ("người chơi", "Unlocked_maps")
`` `

2. Gọi hàm `load_game ()` trong hàm `_Ready ()` của cảnh `menu`.

`` `
func _Ready ():
Dulieu.load_game ()
`` `

### Kết quả

Sau khi chạy trò chơi, bạn sẽ hoàn thành 1 trò chơi và sau đó thoát khỏi nó sẽ thấy nó được lưu và tải dữ liệu trò chơi.

Bạn có thể thấy `file_dulieu` đã được tạo.

Bạn có thể tìm thấy vị trí để lưu tệp trong `Dự án -> Mở thư mục dữ liệu dự án`.

### Bản tóm tắt

Vì vậy, loạt chương trình trò chơi platformer với động cơ Godot đã được hoàn thành.Nếu bạn có bất kỳ câu hỏi, bạn có thể đến cộng đồng để hỏi.

Tôi sẽ rời khỏi dự án dưới đây:

[https:
=======================================
ủng thuật Save and Load ## Save and Load

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

### Steps

1. Create a new script named `Dulieu`.
2. Go to `Autoload` and add the script `Dulieu`.
3. In the `Dulieu` script, declare a constant containing the data file address.
4. Create a function to save the game data.
5. Create a function to load the game data.

### Autoload

1. Create a new script named `Dulieu`.

```
extends Node
```

2. Go to `Autoload` and add the script `Dulieu`.

### Save the game

1. In the `Dulieu` script, declare a constant containing the data file address.

```
const Save_path = "User://file_dulieu.data"
```

2. Create a function to save the game data.

```
func luu_game ():
var config = Configfile.new()
config.set_value("Player", "Diemso", Playerdata.diemso)
config.set_value("Player", "Unlocked_maps", playerdata.unlocked_maps)
config.save(save_path)
```

3. Call the `luu_game()` function in the `_ready()` function of the `Menu` scene.

```
func _ready():
Dulieu.luu_game()
```

### Load the game

1. Create a function to load the game data.

```
func load_game():
var config = Configfile.new()
var check_file = config.load(save_path)
if check_file != OK: # if the file is not found
return # huy bo load file
Playerdata.diemso = config.get_value("Player", "diemo")
Playerdata.unlocked_maps = config.get_value("Player", "Unlocked_maps")
```

2. Call the `load_game()` function in the `_ready()` function of the `Menu` scene.

```
func _ready():
Dulieu.load_game()
```

### Result

After running the game, you will finish 1 game and then get out of it will see it is saved and loading the game data.

You can see `file_dulieu` has been created.

You can find the location to save the file in `Project -> Open Project Data Folder`.

### Summary

So the Platformer game programming series with Godot Engine has been completed. If you have any questions, you can go to the community to ask.

I will leave the project below:

[https://github.com/Deathgm/Platform...om/Deathgm/Platformer-Game-Voi-Godot-English)
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top