godot
godot copied to clipboard
GDScript enum fixes & refactor
This PR fixes a bunch of issues with enums.
Fixes & features
It does a general refactor of how enums are internally and in GDScript, cleaning up the code & making it more consistent across GDScript enums and native enums. It also aims to unify warnings&errors between in-editor, runtime, and export debug/release templates (native enum behavior on release templates will also require #69607).
Some examples:
class_name OuterClass
enum MyEnum { V0, V1, V2 }
class InnerClass:
enum MyEnum { V0, V1, V2 }
It is now possible to access each enum in all possible ways:
In OuterClass
, referring to:
-
OuterClass.MyEnum
:V1
,MyEnum.V1
andOuterClass.V1
,OuterClass.MyEnum.V1
-
InnerClass.MyEnum
:InnerClass.V1
,InnerClass.MyEnum.V1
andOuterClass.InnerClass.V1
,OuterClass.InnerClass.MyEnum.V1
In InnerClass
, referring to:
-
OuterClass.MyEnum
:OuterClass.V1
,OuterClass.MyEnum.V1
-
InnerClass.MyEnum
:V1
,MyEnum.V1
,InnerClass.V1
,InnerClass.MyEnum.V1
,OuterClass.InnerClass.V1
,OuterClass.InnerClass.MyEnum.V1
For native enums, e.g. with TileSet
, you can now use TileSet.TILE_SHAPE_SQUARE
or TileSet.TileShape.TILE_SHAPE_SQUARE
, and a warning will be issued if the native type does not have an accessed property.
All the following accesses to GDScript enums behave the the same in-editor, in debug and release exports.
Typing with enums now works properly for variables, function parameters and function returns, and comprehensive unit tests have been added to check this.
To do
- Further unify behavior between native enums and GDScript enums. ~~- Add more error unit tests to catch incompatible parameters~~
- Static check that values assigned to enum-typed variables are within bounds
Notes
Added a couple of quick extra context lookups that I believe will be superseded by https://github.com/godotengine/godot/pull/69471 and https://github.com/godotengine/godot/pull/69587. These have been noted as TODO
comments in this PR, to be rechecked once all PRs are merged.