Skip to content

Commit 44d2ca4

Browse files
committed
Correct mistakes about GDScript enums
1 parent 85513fa commit 44d2ca4

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

src/register/properties.md

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,18 +162,21 @@ enum Planet {
162162
~~~
163163
is roughly the same as:
164164
~~~java
165-
const EARTH = 0
166-
const VENUS = 1
167-
const MARS = 2
165+
const Planet: Dictionary = {
166+
EARTH = 0,
167+
VENUS = 1,
168+
MARS = 2,
169+
}
168170
169171
@export_enum("EARTH", "VENUS", "MARS") var favorite_planet = Planet.EARTH
170172
~~~
171173
However, the enum is not type-safe, you can just do this:
172174
~~~java
173175
var p: Planet = 5
174176
~~~
175-
Furthermore, unless you initialize the constants with string values, you cannot retrieve their names, making debugging harder. There is no
176-
reflection either, such as "get number of enum values" or "iterate over all of them". If you have the choice, consider keeping enums in Rust.
177+
Furthermore, you can also not easily retrieve the name `"EARTH"` from the expression `Planet.EARTH`.[^enum-name]
178+
179+
See [GDScript enums][godot-gdscript-enums] for more details.
177180
```
178181

179182

@@ -212,3 +215,13 @@ As a general rule, try to stay close to Godot's own types, e.g. `Array`, `Dictio
212215
[api-var-export]: https://godot-rust.github.io/docs/gdext/master/godot/register/derive.GodotClass.html#properties-and-exports
213216
[godot-gdscript-properties]: https://docs.godotengine.org/en/stable/tutorials/scripting/gdscript/gdscript_basics.html#properties
214217
[gh-godot-packedarray]: https://github.com/godotengine/godot/issues/76150
218+
[godot-gdscript-enums]: https://docs.godotengine.org/en/stable/tutorials/scripting/gdscript/gdscript_basics.html#enums
219+
220+
<br>
221+
222+
---
223+
224+
**Footnotes**
225+
226+
[^enum-name]: You _can_ obtain `"EARTH"` if you iterate the `Planet` dictionary and compare each value (assuming there are no duplicates).
227+
That however requires that you know the type (`Planet`); the value itself does not hold this information.

0 commit comments

Comments
 (0)