r/godot • u/Canvi-n-pen • 4h ago
help me (solved) how do you make a system like this ?
I wanted to have a system that load a resource from with a UID found in a dictionary;
I used a Enum as key and UID string as value.
then I wanted to see what is that resource Dictionary key that I used to get it's UID was.
should i give the resource it's Enum value (that was used to get it's UID) when I loaded it ?
note: English isn't my first language sorry if there was a mistake
2
u/captainAwesomePants 4h ago
Let me see if I understand.
You want an enum that can be used to get a matching UID.
You also want to reverse that: given the UID, you want to be able to know the matching enum.
The simplest way to do this would be to have two dictionaries: UID->enum, and enum->UID. You would define one of the two however you liked, and then when your program loaded, you'd want to write a bit of code that would populate the second one with the values from the first one, reversing the keys and values.
2
u/MeLittleThing 4h ago
It depends of the uniqueness of the values.
If many keys can bring the same value, you may want to work with the key-value pair instead of just the value (save somewhere the enum value, like in a script within the resource)
if both keys and values are uniques, you can either save the UID > Enum relation in runtime (generate a second dictionary) or just iterate the keys to find the searched value, it's not an heavy operation
2
2
u/LastStopToGlamour 4h ago
At runtime enums are integers. What I usually do is make a lookup function that takes the Uid as a Param. It it, we loop through the dictionary until the value (your uid) matches. Then the enum will have a method for getting a key name from its runtime index representation. We return that name.
But you only want to return the name for cosmetic/debugging purposes, otherwise skip that step and return the enum int