导出后 res:// 是只读的。存档路径用 user://,Godot 会指到各系统的用户目录。
读写最高分
gdscript
extends Node
const PATH := "user://save.cfg"
var high_score := 0
func _ready() -> void:
load_game()
func load_game() -> void:
var cfg := ConfigFile.new()
if cfg.load(PATH) != OK:
return
high_score = int(cfg.get_value("score", "high", 0))
func save_high(score: int) -> void:
if score <= high_score:
return
high_score = score
var cfg := ConfigFile.new()
cfg.set_value("score", "high", high_score)
cfg.save(PATH)把这个脚本做成 Autoload Save。结算时 Save.save_high(score)。
不要存复杂对象
ConfigFile 适合数字、字符串、bool。背包数组用 JSON.stringify 另存 user://inv.json,或 FileAccess.store_var()。
调试看文件
编辑器 → 打开用户数据文件夹。找不到存档,多半写到了 res://,在导出包里会失败。
微型游戏一个 save.cfg 够了。别一上来上 SQLite。
