How to iterate through an enum?

Godot Version

v4.3.stable.official [77dcf97d8]

Question

I’ve created an enum that has several values and want to iterate through it and pass each element to a function.

This is not working for me since the keys() of the enum returns string names and the values() return ints. Neither of these are of the type of the enum, so I cannot pass it to a function that uses typing to specify the parameter. How would I iterate through each element of my enum and get the enum constants for each iteration?

enum Dir { NW, NE, E, SE, SW, W }

...

for d in Dir.values():
    my_func(d)

...

func my_func(dir:Dir):
    match dir:
        Dir.NW:
            #do something
        Dir.NE:
            #do something
...

It looks like this works:

for d in Dir.values():
    my_func(d as Dir)
1 Like