Godot存档系统
可能是最简单的存档系统?ConfigFile功能的应用
读取数据
1 2 3 4 5 6 7 8 9 10
| public LineEdit playerName; public ColorPickerButton playerColor;
public override void _Ready() { playerName = this.GetTree().CurrentScene.GetNode<LineEdit>("Control/PlayerName/LineEdit"); playerColor = this.GetTree().CurrentScene.GetNode<ColorPickerButton>("Control/PlayerColor/HBoxContainer/ColorPickerButton"); }
|
保存数据
ConfigFile相关函数:
1 2 3 4
| public void SetValue(string section, string key, Variant value) public Error Save(string path)
|
在Godot中 ”user://pathname“ 的路径格式可将文件保存至游戏目录外(C盘区),相当安全哈
1 2 3 4 5 6 7 8
| public void Save() { ConfigFile config = new ConfigFile(); config.SetValue("Settings", "name", playerName.Text); config.SetValue("Settings", "color", playerColor.Color); config.Save("user://settings.cfg"); }
|
File paths in Godot projects — Godot Engine (stable) documentation in English
读取数据
1 2 3 4
| public Variant GetValue(string section, string key, Variant @default = default) public Error Load(string path)
|
1 2 3 4 5 6 7 8 9 10 11
| public void Load() { ConfigFile config = new ConfigFile(); Error result = config.Load("user://settings.cfg"); if(result == Error.Ok) { playerName.Text = (string) config.GetValue("Settings", "name"); playerColor.Color = (Color)config.GetValue("Settings", "color"); } }
|
存档加密
将Save()替换成SaveEncryptedPass(),将Load()替换成LoadEncryptedPass(),后接string变量作为密钥。
1 2 3 4 5
| private string key = "guoyuxiniubi";
config.SaveEncryptedPass("user://settings.cfg", key); Error result = config.LoadEncryptedPass("user://settings.cfg", key);
|
优雅地保存任何数据!基于Resource类的存档系统