Search code examples
godotgdscript

How to declare a function parameter of type enum in Godot?


This is the code I have right now:

extends Control

var _handles:Array = [];
var _handleSize:int = 10;
var _mouseOverHandleIndex = -1;
var _draggingHandleIndex = -1;



class Handle:
    enum MovementConstraint { Horizontal, Vertical, Both, None}

    var _movementConstraint: MovementConstraint = MovementConstraint.Both;
    var _position: Vector2 = Vector2.ZERO;
    
    func init(position:Vector2, movementConstraint: MovementConstraint):
        _position = position;
        _movementConstraint = MovementConstraint;

Godot parser is complaining about "The indentifier 'MovementConstraint' isn't a valid type (not a script or class), or couldn't be found on base self."

I have tried to move the enum outside (into he parent class space) but same thing happens.


Solution

  • The issue is that a GDScript enum is a collection of constants (and a dictionary to access them, if the enum is named). So there are not really variables which type is the enum. Instead they are int. And yes, that also means Godot won't complain if set a value that is not from the enum you expect (you could add validation by defining a custom setter with setget).