Search code examples
godotgdscript

Where are operators for built-in Godot types defined?


Many of the built-in Godot types (Vector2, Rect2, etc.) support operators (+/-/*, etc.) that are not documented along with the type. Where can I find information about the supported operators and their semantics for all types?

If there is no documentation available, where in the Godot source code can the bindings for these operators be found?

The specific case I am interested in right now is converting a Rect2 to bool as part of an if statement, i.e.,

func my_func(area: Rect2):
    if area:
        # When do I get here?
        # Is this equivalent to if !area.has_no_area()?

Solution

  • Where can I find information about the supported operators and their semantics for all types?

    If there is no documentation available, where in the Godot source code can the bindings for these operators be found?

    For Godot 3.x, the only complete canonical source is the source code for Variant. The file variant_op.cpp to be more precise.

    However, the documentation for Godot 4.0 already includes the operators on the class pages.


    The specific case I am interested in right now is converting a Rect2 to bool as part of an if statement, i.e.,

    func my_func(area: Rect2):
        if area:
            # When do I get here?
            # Is this equivalent to if !area.has_no_area()?
    

    Note: this is not an operator.

    Conversion to bool used here is handled by the booleanize method, which delegate to is_zero (as in zeroed memory). In general, an uninitialized variable converts to false.

    For Rect2 that means that when position is zero ((0,0)) and size is zero ((0,0)), the Rect2 will convert to false, anything else converts to true.