Multiple Return Values in GDScript

GDScript has built-in support for multiple return values. This feature is often used in idiomatic GDScript, for example, to return both result and error values from a function.

# The `-> Array` in this function signature shows that
# the function returns an Array containing multiple values.
func vals() -> Array:
    return [3, 7]

func _ready():
    # Here we use the 2 different return values from the
    # call with multiple assignment.
    var a: int
    var b: int
    a = vals()[0]
    b = vals()[1]
    print(a)
    print(b)

    # If you only want a subset of the returned values,
    # you can ignore the unwanted values.
    var c: int
    c = vals()[1]
    print(c)

When you run this script, you’ll see the following output:

3
7
7

In GDScript, functions return a single value, but you can simulate multiple return values by returning an Array or Dictionary. When calling such functions, you can use array indexing to access individual values.

GDScript doesn’t have a built-in way to ignore certain return values (like Go’s blank identifier _), but you can simply not assign those values if you don’t need them.

This approach allows for flexibility in returning and handling multiple values from functions in GDScript.