Hi everybody,

is there a way to "centralize" this exported variable dropdown?

i mean a way to write the long list just once then read from there

@export_enum("Morlako", "Reception", "Room01", "Room02", "Room03", "Room04", "Room05", "Room06", "Room07", "Room08", "Room09", "RoomX", "Room05Closed") var room_name: String

  • xyz replied to this.

    You can do this:

    enum NamedEnum {THING_1, THING_2, ANOTHER_THING = -1}
    @export var x: NamedEnum

    https://docs.godotengine.org/en/4.2/tutorials/scripting/gdscript/gdscript_exports.html#exporting-enums

    read from there

    Do you mean access the enum from other nodes? Access room_name from other nodes?

      papalagi

      class_name Enums
      enum Rooms{Room1, Room2}
      @export var room: Enums.Rooms

        DaveTheCoder

        Yes, i'd basically like to have the room names list in a constant and then populate all the dropdowns reading from the constant

        your solution works, the result is not a String, but i can use str():

        enum room_name {Reception, Room01, Room02, Room03, Room04, Room05, Room06, Room07, Room08, Room09, RoomX, Room05Closed}
        @export var room: room_name

        can i put it in a CONST?

        xyz

        Thank you xyz, what's the advantage of creating a class? retrieving the values from other scripts / objects?

          papalagi Every gd script you make already is a class. Nothing extra is created. It's just explicitly named so the enum can be defined in one place and accessed via that class' namespace from any other script, effectively acting as a global enum type. Isn't that what you asked for?

            xyz

            ok, problem is that i want to retrieve a string, not an index, maybe ENUMS are not the ideal entity?

            • xyz replied to this.

              papalagi

              enum Rooms{Room1, Room2}
              print(Rooms.keys()[Rooms.Room1])

              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

              • xyz replied to this.

                DaveTheCoder Why drag along a whole node just to have an enum type definition?

                  xyz Presumably for interoperability. Nice to have one global place where enums are defined rather than classes needing to refer to each other to share enums. Doesn't need to be an autoload though, unless the autoload is doing other things for some reason.

                  • xyz replied to this.

                    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.

                      xyz Right that's what I meant. It doesn't need to be an autoload.

                      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 :-)

                      • xyz replied to this.

                        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)