GDScript: Treat enum values as int and enum types as dictionary

Since enums resolve to a dictionary at runtime, calling dictionary
methods on an enum type is a valid use case. This ensures this is true
by adding test cases. This also makes enum values be treated as ints
when used in operations.
This commit is contained in:
George Marques 2022-02-02 13:57:24 -03:00
parent b013c0d544
commit ceafdf347e
No known key found for this signature in database
GPG key ID: 046BD46A3201E43D
6 changed files with 127 additions and 31 deletions

View file

@ -0,0 +1,21 @@
# Enum is equivalent to int for comparisons and operations.
enum MyEnum {
ZERO,
ONE,
TWO,
}
enum OtherEnum {
ZERO,
ONE,
TWO,
}
func test():
print(MyEnum.ZERO == OtherEnum.ZERO)
print(MyEnum.ZERO == 1)
print(MyEnum.ZERO != OtherEnum.ONE)
print(MyEnum.ZERO != 0)
print(MyEnum.ONE + OtherEnum.TWO)
print(2 - MyEnum.ONE)

View file

@ -0,0 +1,7 @@
GDTEST_OK
true
false
true
false
3
1

View file

@ -0,0 +1,13 @@
enum MyEnum {
ZERO,
ONE,
TWO,
}
func test():
for key in MyEnum.keys():
prints(key, MyEnum[key])
# https://github.com/godotengine/godot/issues/55491
for key in MyEnum:
prints(key, MyEnum[key])

View file

@ -0,0 +1,7 @@
GDTEST_OK
ZERO 0
ONE 1
TWO 2
ZERO 0
ONE 1
TWO 2