0 votes

I don't know how to match enumerate value.
Enumerate is constant but I can't put It into pattern key!

e.g. #error not a constant expression as key

enum { A, B, C}
func test():
    match {A:1,B:2}:
        {A:1, ..}:
            print("match")
        _:
            print("not match")
in Engine by (41 points)

2 Answers

0 votes

You should use Array, and also ensure that your enumeration value is a continuous set of positive integers starting from 0 (the default is to sort from 0 in your writing order, provided that you do not set other Integer value for one of enum value), this is the correct way to use enumerations as index values, and is faster than dictionaries. Currently, dictionaries in gdscrip should only use strings as index values.

enum { A, B, C}
func test():
    var arr_test:=[“B_string","C_string","A_string"]
    print(arr_test[A],arr_test[B],arr_test[C]

About "Array pattern",you should look this:
https://docs.godotengine.org/en/latest/getting_started/scripting/gdscript/gdscript_basics.html#match

by (204 points)
0 votes

from docs, the basic syntax is

match [expression]:
    [pattern](s):
        [block]
    [pattern](s):
        [block]
    [pattern](s):
        [block]

As far as i know you must use a variable or function for match expression
i.e:

enum { A, B, C}
func test(my_value):
    match my_value:
        {A:1, ..}:
            print("match")
        _:
            print("not match")
by (14 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.