Skip to content

Instantly share code, notes, and snippets.

@kkolyan
Last active September 21, 2023 21:37
Show Gist options
  • Save kkolyan/cad311dbe78b12dce441d5a235951de4 to your computer and use it in GitHub Desktop.
Save kkolyan/cad311dbe78b12dce441d5a235951de4 to your computer and use it in GitHub Desktop.
using Godot;
using System.Linq;
[Tool]
public partial class DictStringInt : Node3D
{
private System.Collections.Generic.Dictionary<string, int> _data = new();
private void OneSecTick()
{
GD.Print($"_data: {string.Join(", ", _data.Select(it => $"{it.Key}: {it.Value}"))}");
}
public override Godot.Collections.Array<Godot.Collections.Dictionary> _GetPropertyList()
{
var properties = new Godot.Collections.Array<Godot.Collections.Dictionary>();
properties.Add(new Godot.Collections.Dictionary()
{
{ "name", "MyCustomData" },
{ "type", (int)Variant.Type.Dictionary },
});
return properties;
}
public override Variant _Get(StringName property)
{
if (property == "MyCustomData")
{
var value = new Godot.Collections.Dictionary();
foreach (var pair in _data)
{
value[Variant.From(pair.Key)] = Variant.From(pair.Value);
}
return value;
}
return base._Get(property);
}
public override bool _Set(StringName property, Variant value)
{
if (property == "MyCustomData")
{
var dict = value.AsGodotDictionary();
_data.Clear();
foreach (var pair in dict)
{
_data[pair.Key.AsString()] = pair.Value.AsInt32();
}
return true;
}
return base._Set(property, value);
}
}
[gd_scene load_steps=2 format=3 uid="uid://1v34fimr1fby"]
[ext_resource type="Script" path="res://DictStringInt.cs" id="1_poo7v"]
[node name="Node3D" type="Node3D"]
script = ExtResource("1_poo7v")
MyCustomData = {
"abc": 123
}
[node name="Timer" type="Timer" parent="."]
autostart = true
[connection signal="timeout" from="Timer" to="." method="OneSecTick"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment