Skip to content

Instantly share code, notes, and snippets.

@sean-clayton
Created August 13, 2020 22:14
Show Gist options
  • Save sean-clayton/daad5e40020a65c8877a3b29c44344bb to your computer and use it in GitHub Desktop.
Save sean-clayton/daad5e40020a65c8877a3b29c44344bb to your computer and use it in GitHub Desktop.
using Godot;
public class PlayerPreferences : Node
{
private static ConfigFile Cfg = new ConfigFile();
[Export] private static string Filename = "player_preferences.cfg";
private static string Filepath = "user://" + Filename;
public override void _Ready()
{
LoadOrSave();
}
private static void LoadOrSave()
{
switch (Cfg.Load(Filepath))
{
case Error.Ok:
Load();
break;
default:
Save();
break;
}
}
private static void Save()
{
Cfg.SetValue("GameWindow", "Width", OS.WindowSize.x);
Cfg.SetValue("GameWindow", "Height", OS.WindowSize.y);
switch (Cfg.Save(Filepath))
{
case Error.Ok:
GD.Print("Saved!");
break;
default:
GD.PushError("Could not save file!");
break;
}
}
private static void Load()
{
var ScreenSize = new Vector2((float) Cfg.GetValue("GameWindow", "Width", OS.WindowSize.x),
(float) Cfg.GetValue("GameWindow", "Height", OS.WindowSize.y));
OS.WindowSize = ScreenSize;
}
}
open Godot
type PlayerPreferences() =
inherit Node()
let cfg = new ConfigFile()
[<Export>]
member val filename = "player_preferences.cfg" with get, set
member this.pathToFile = "user://" + this.filename
override this._Ready() =
this.loadOrSave ()
member this.loadOrSave() =
match cfg.Load(this.pathToFile) with
| Error.Ok ->
this.Load()
| _error ->
this.Save()
member this.Save() =
cfg.SetValue("GameWindow", "Width", OS.WindowSize.x)
cfg.SetValue("GameWindow", "Height", OS.WindowSize.y)
match cfg.Save(this.pathToFile) with
| Error.Ok ->
GD.Print(sprintf "Saved data to: %s/%s" (OS.GetUserDataDir()) this.filename)
| _ ->
GD.PushError(sprintf "Could not save file at %s/%s" (OS.GetUserDataDir()) this.filename)
member this.Load() =
let screenSize =
Vector2
(cfg.GetValue("GameWindow", "Width", 1440.0f) :?> float32,
cfg.GetValue("GameWindow", "Height", 900.0f) :?> float32)
OS.WindowSize <- screenSize
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment