Checking dictionary for values - am I doing it wrong?
Not a specific code error I'm trying to resolve - my code works, the errors are gone, but I'm not sure I'm doing this right.
In my tilemap, I have a dictionary of Vector2i keys to return cell_state enum values, which gets updated by various functions.
Many functions then need to check neighbouring cells for certain states. E.g. if cell_dictionary[cell] == cell_state.STATE: etc.
However, if the dictionary doesn't have that particular cell, I get an invalid access error.
So, every time I want to check a cell like this, I have to first check if the cell is in the dictionary and then also check it's state. E.g. if cell_dictionary.has(cell) and cell_dictionary[cell] == cell_state.STATE: etc.
This works; I don't get an error if it doesn't exist, and it checks the state correctly if it does exist. I just feel like it feels a bit, clunky? I don't know if I'm vaguely recalling seeing some more elegant solution I've forgotten, or I'm just getting hung up on how long my if statements are getting.
Posted Aug 5, 2026, 1:35 PM UTC