Multiple Return Values in AngelScript

AngelScript doesn’t have built-in support for multiple return values like some other languages. However, we can simulate this behavior using out parameters or by returning an array or a custom object. In this example, we’ll use out parameters to demonstrate a similar concept.

void vals(int &out a, int &out b)
{
    a = 3;
    b = 7;
}

void main()
{
    // Here we use out parameters to simulate multiple return values
    int a, b;
    vals(a, b);
    Print(a);
    Print(b);

    // If you only want a subset of the returned values,
    // you can ignore one of the out parameters
    int c;
    vals(c, c); // We're overwriting c with the second value
    Print(c);
}

In this AngelScript code:

  1. We define a function vals that takes two int parameters by reference with the out keyword. This is similar to returning multiple values.

  2. In the main function, we call vals and pass in two variables to receive the values.

  3. We then print these values using the Print function (assuming it’s defined elsewhere in your AngelScript environment).

  4. To demonstrate ignoring one of the “returned” values, we call vals again but use the same variable c for both parameters. This effectively ignores the first value and only keeps the second.

To run this script, you would typically use an AngelScript-compatible engine or interpreter. The exact method may vary depending on your setup, but it might look something like this:

$ angelscript multiple_return_values.as
3
7
7

Note that AngelScript doesn’t have a standard library or built-in print function, so the Print function used in this example would need to be provided by your scripting environment.

This approach using out parameters is a common way to simulate multiple return values in languages that don’t natively support the feature. It allows you to modify multiple variables within a single function call, achieving a similar effect to multiple return values.