Godot Version
4.2.1
Question
I’m trying to create a Resource in C# that acts as a dictionary to store icons for keyboard keys (and more similar ones for mouse buttons and gamepad buttons) by enumerating the names of the Key
constants and using those names to generate properties in _GetPropertyList
:
public override Array<Dictionary> _GetPropertyList()
{
Array<Dictionary> properties = new();
properties.AddRange(Enum.GetNames<Key>().Select((n) => new Dictionary()
{
{ "name", n },
{ "type", Variant.From(Variant.Type.Object) },
{ "hint", Variant.From(PropertyHint.ResourceType) },
{ "hint_string", "Texture2D" }
}));
return properties;
}
I have the actual mapping of Key
constants to Texture2D
s stored in a Dictionary<Key, Texture2D>
along with another dictionary to cache Key
constant names, which I use in _Get
and _Set
like so:
public override Variant _Get(StringName property) =>
_names.ContainsKey(property) ? _icons[_names[property]] : default;
public override bool _Set(StringName property, Variant value)
{
if (_names.ContainsKey(property))
{
_icons[_names[property]] = value.As<Texture2D>();
return true;
}
else
return false;
}
This mostly works, and I’m able to create an instance of the Resource and assign textures to Key
values, but whenever I press the “Build” button the texture’s I’ve assigned get deleted until I reload the project, even if I’m only making changes in other scripts. Am I doing something wrong? Is there something else I need to do so it will keep the values when the code is rebuilt?
Edit: reformat the code a bit so lines aren’t as long. The Array
and Dictionary
classes I’m using here are the Godot.Collections versions, so they work with the editor.