Populate dropdown with a global var
Asked Answered
L

16

0

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

Leotaleotard answered 7/12, 2023 at 14:24 Comment(0)
R
0

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?

Repairman answered 7/12, 2023 at 16:34 Comment(0)
A
0

Leotaleotard

class_name Enums
enum Rooms{Room1, Room2}
@export var room: Enums.Rooms
Accustomed answered 7/12, 2023 at 17:4 Comment(0)
L
0

Repairman

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?

Leotaleotard answered 8/12, 2023 at 7:29 Comment(0)
L
0

Accustomed

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

Leotaleotard answered 8/12, 2023 at 7:31 Comment(0)
A
0

Leotaleotard 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?

Accustomed answered 8/12, 2023 at 7:41 Comment(0)
L
0

Accustomed

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

Leotaleotard answered 8/12, 2023 at 8:11 Comment(0)
A
0

Leotaleotard

enum Rooms{Room1, Room2}
print(Rooms.keys()[Rooms.Room1])
Accustomed answered 8/12, 2023 at 8:21 Comment(0)
D
0

Or slightly more readable (well, arguably): print(Rooms.find_key(Rooms.Room1))

Delectate answered 8/12, 2023 at 13:32 Comment(0)
R
0

Leotaleotard 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

Repairman answered 8/12, 2023 at 19:42 Comment(0)
A
0

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

Accustomed answered 8/12, 2023 at 19:59 Comment(0)
M
0

Accustomed 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.

Microsurgery answered 9/12, 2023 at 0:29 Comment(0)
A
0

Microsurgery 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.

Accustomed answered 9/12, 2023 at 0:49 Comment(0)
R
0

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.

Repairman answered 9/12, 2023 at 0:50 Comment(0)
M
0

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

Microsurgery answered 9/12, 2023 at 0:52 Comment(0)
L
0

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

Leotaleotard answered 9/12, 2023 at 11:35 Comment(0)
A
0

Leotaleotard
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 Tine's variant:

LOCATION.find_key(loc)
Accustomed answered 9/12, 2023 at 11:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.