Use enum in signal as extra call argument

Godot Version

v4.2.1

Question

Is there any way to use a self-defined enum when connecting a signal to a method?

enum StructureType {
	Small_Stable = 0,
	Medium_Stable = 1,
	Large_Stable = 2,
}

1 Like

Yes, you can use self-defined enums when connecting to signals. Define your enum as usual and emit the signal with the enum values. Connect the signal to a method in code and handle the enum values within that method using a “match” statement or similar logic.

like this

enum StructureType {
    Small_Stable,
    Very small Stable,
    Tiny Stable,
}

signal structure_type_changed(new_type)

func _ready():
    some_node.connect("structure_type_changed", self, "_on_structure_type_changed")

func _on_structure_type_changed(new_type):
    match new_type:
        StructureType.Small_Stable: print("Small Stable")
        StructureType.Medium_Stable: print("Very small Stable")
        StructureType.Large_Stable: print("Tiny Stable")
        _: print("idk")

hope it helps :smiley:

1 Like

I mean, it’s still an int value. So if you know what type of structure you’re creating (0, 1 or 2) it shouldn’t matter if it’s an int or your enum.

I agree it would be easier to see at first glance, but once it’s set up you’ll forget about it.

1 Like

You can’t define enums with spaces in their names.
image

Also… I’m assuming the print logic is backwards? (The large_stable is called “tiny stable”)

You’re totally right—since enums are just fancy integers, you can totally use the int values directly if you know what they stand for. Enums are just there to make things look pretty and easier to understand later on.

About the spaces in enum names—sadly, Godot is a bit picky, so you’ll have to stick with underscores or camelCase. And Yeah I switched up the name cuz I have a really bad sense of humor

As long as I unterstand, there is no way to select an self-defined enum through the Godot Editor user interface. But you can use enum parameters as argument in code or use integers.

My current solution is using a string parameter with the enum’s name (so I can see directly which enum it is) and casting it back to integer.

image

func _on_construction_button_pressed(enum_name):
	var enum_value = StructureType[enum_name]
	# ...