Can i access methods and variables inside dictionary keys?

Godot Version

4.5.1.stable

Question

i have a dictionary that uses resources as keys

var myDictionary [Resource,float]

if i have, for example, a var called data inside the Resource can i access it with code, if yes how do i do it?

You can get the array of keys from the dictionary. Also when you iterate the dictionary using the for loop, the iterator values are keys.

Technically you can. But in order to know which Dictionary, you need to know which value it has (as float). And technically you can have the same value for different keys, you may end up calling the wrong Dictionary if you have same values.
I recommend to change your logic so that you don’t need to access the keys of any Dictionary to get data.
But if you really insist on implementing as it is, you can use find_key()

var myDictionary [Resource, float]
var resource_data = myDictionary.find_key(value).data

originally the dict was needed because the resource logic (the resource being a chemical) needed the float (quantity) in order to work, basically every time the chem did a reaction it would have consumed a bit of it’s quantity

it ended up being needlessly complicated so i switched it with an array containing a new resource i made called ChemSlot, witch contains both the chem and the quantity.

now i can access the processes inside the chem resource easily and also have the quantity stored in the same place.

1 Like