How do i make sure that the compared enums are of same type?

Example: class Enums.gd:


class_name Enums
enum Armor {
	FULL, 
	NAKED
}

enum Action {
	ATTACK,
	DEFEND
}

Now if i try to compare Enums.Action.ATTACK == Enums.Armor.FULL the result will be true ...

Now if i make a function that accepts only this type like

func _receive_action(action: Enums.Action)

Then if i call this function with incorrect enum then there is an error and that is good.
But if i emit signal with incorrect value there is no type check.
Maybe there is a way to enforce signal argument type?

  • Toxe replied to this.

    Which version of Godot?

    I'm not sure whether that's a bug. The values of Enums.Action.ATTACK and Enums.Armor.FULL are the same, the integer 1, so it makes sense to say that the values are equal.

    But when _receive_action() is called, the parameter is the value of an Enums.Action, which is an integer, like Enums.Armor. So that seems in conflict with the previous case.

    It probably should be reported as a bug. If it's not really a bug, someone will explain why.

    If you don't plan on reporting it, let me know, and I will either report it or look for an explanation.

      lxknvlk I just checked, looks like the arguments to signals and lambda functions are always treated as variants and the static type hint is ignored. I looked through the manual but could not find where it explicitly states that signal and lambda arguments are always treated as variants but it would be nice if we could get an error in obvious cases.

      signal sig(abc: ABC)
      
      enum ABC { a, b, c }
      enum XYZ { x, y, z }
      
      func foo(abc: ABC) -> bool:
          return abc == ABC.a
      
      func sig_handler(abc: ABC) -> void:
          print(abc == ABC.a)
      
      func _ready() -> void:
          print(ABC.a == XYZ.x)  # true
      
          print(foo(ABC.c))  # false
          # print(foo(XYZ.x))  # error: invalid argument
      
          sig.connect(sig_handler)
          sig.emit(ABC.c)  # false
          sig.emit(XYZ.x)  # true (should be an error)
      
          var fn := func(abc: ABC) -> bool: return abc == ABC.a
          print(fn.call(XYZ.x))  # true (should be an error)