Godot Version
4.4
Question
I have this enum:
enum actions {
FRONT_KICK,
SPIN_KICK,
UPPERCUT,
DOWNWARDS_PUNCH,
JUMP
}
And this function that takes as an argument one value of the enum:
func get_action_string(attack: Globals.actions) -> String:
match attack:
Globals.actions.FRONT_KICK:
return 'front_kick'
Globals.actions.SPIN_KICK:
return 'spin_kick'
Globals.actions.UPPERCUT:
return 'uppercut'
Globals.actions.DOWNWARDS_PUNCH:
return 'downwards_punch'
Globals.actions.JUMP:
return 'jump'
_:
return ''
I want to check whether a string is an action in my enum (meaning that get_action_string()
returns it), so I wrote this:
if text in Globals.actions.values().map(func(value): get_action_string(value)):
print('yes it is')
But that doesn’t work (it never prints ‘yes it is’) even for strings I know 100% are related to my enum. In order to find out what was wrong, I printed this:
print(Globals.actions.values())
print(get_action_string(0), ' ', 0 in Globals.actions.values())
print(get_action_string(1), ' ', 1 in Globals.actions.values())
print(get_action_string(2), ' ', 2 in Globals.actions.values())
print(get_action_string(3), ' ', 3 in Globals.actions.values())
print(get_action_string(4), ' ', 4 in Globals.actions.values())
print(Globals.actions.values().map(func(value): get_action_string(value)))
And the output really surprised me!!!
[0, 1, 2, 3, 4]
front_kick true
spin_kick true
uppercut true
downwards_punch true
jump true
[<null>, <null>, <null>, <null>, <null>]
From what I see in the printed text, Globals.actions.values()
returns the values that are accepted by the get_action_string()
function. Therefore, how is it possible that the map()
function can’t build an array with the strings returned by get_action_string()
?
I know that there are severals workarounds for this and I’m not worried about it, but this issue makes me think that maybe I don’t know how enums or the map()
function actually work in Godot.
Thanks!