Populate dropdown with a global var
Or slightly more readable (well, arguably): print(Rooms.find_key(Rooms.Room1))
papalagi what's the advantage of creating a class?
You don't have to use the class_name keyword.
I have some enums defined in an autoload named Global.
For example:
enum Movement {INTERMITTENT, CONTINUOUS}
I can reference the enum in other scripts like this:
var m: Global.Movement
- Edited
DaveTheCoder Why drag along a whole node just to have an enum type definition?
- Edited
award Umm, you can just have a static class named Globals or Enums. No need for autoloaded nodes whatsoever. In fact using autoloaded nodes for any kind of global data is redundant and feels hacky in 4.x. Sadly, people use them exclusively for that in 99% of cases. It's just a perpetuation of a bad habit picked up from 3.x tutorials.
I'm using an autoload to ensure that it's in the scene tree and initialized early, so that other nodes can reference it. It's a habit I adopted in Godot 3.
Maybe a class with static variables would accomplish the same thing.
Sorry guys, very kind of you explaining things to a noob, but too many answers i can't figure out the easiest method...
I have no problems with autoloads or new classes, this i can understand.
Starting from scratch i can have this piece of code populating the dropdown and i can put the enum in a global script, no problem:
enum LOCATION {Morlako, Reception, Room01, Room02, Room03, Room04, Room05, Room06, Room07, Room08, Room09, RoomX, Room05Closed}
@export var loc: LOCATION
i then set the loc variable via the editor
i don't understand how to retrieve the choosen key in String format, can you please :-)
- Edited
papalagi
I already gave you the answer above.
In this specific case:
LOCATION.keys()[loc]
Enum name behaves like a dictionary object with strings as keys and values as their numerical counterparts. So you can access it in a way you'd access a regular dictionary.
If your enum values are not starting from zero, you'll have to use @Toxe's variant:
LOCATION.find_key(loc)