Godot Version
4.2.2.stable.official
Question
I’d like to understand why in a specific case, “match” and “if” work differently.
I have a code like this:
enum TileState { LOCKED, SOIL_BAD, SOIL_GOOD, GRASS, WATER }
...
func spawn_world_tiles() -> void:
for tile_key in world_data.tile_states_save_data:
var tile_value = world_data.tile_states_save_data[tile_key]
match tile_value:
TileState.SOIL_GOOD:
print("yes match")
if tile_value == TileState.SOIL_GOOD:
print("yes if")
I would expect, that “yes match” and “yes if” should always be printed at the same time, but no.
I create the dictionary of tiles like this
var tile_state_data = {
Vector2(-1, -1): World.TileState.GRASS,
Vector2(-1, -2): World.TileState.SOIL_GOOD,
Vector2(0, 0): World.TileState.GRASS,
...
And I should also mention that if I just create it in memory, both MATCH and IF work. But If I serialize the tile_save_data to JSON and then load it from a file, only IF works but MATCH does not.
This is the code that loads the dictionary from file/JSON:
for tile_state_key in json.data["world_save_data"]["tile_states_save_data"]:
var parsed_tile_state_key = str_to_var("Vector2" + tile_state_key) as Vector2i
var tile_state_value = json.data["world_save_data"]["tile_states_save_data"][tile_state_key]
world_save_data.tile_states_save_data[parsed_tile_state_key] = tile_state_value
Maybe it is just my mistake, but I spent many hours debugging and looking for explanations, haven’t found any. It seems like a data type related problem, but still I would really like to understand why IF works in this case and MATCH does not.