I have a function that detects tilemap tiles and does the correct thing based on which tile it collided with however I have a big block of if statements just asking what the tile is then responding accordingly and It seems very inefficient. However, I was not able to find a way that doesn't include tons of if statements EX Code: if tile_id == 1: do this if tile_id == 2: do something else if tile_id == 3: do something else
How can I replace If statements with a better form of logic?
maybe it suits you better with mach, it's better than using many if
- Edited
TheDiavolo meant using a match
statement, like this:
match tile_id:
1:
do this
2:
do something else
3:
do something else
_:
default case (run if none of the above cases are matched)
GDQuest claims that if-elif chains are 20% faster than match statements. I don't know if that's accurate for the current version. https://www.gdquest.com/tutorial/godot/gdscript/optimization-code/
Generally there's nothing wrong with if statements. But the terseness you are seeking is exactly what match statements are for.
a year later
Megalomaniak added the Godot Help tag .