Variadic Functions in AngelScript

Here’s a function that will take an arbitrary number of ints as arguments.

void sum(array<int> nums) {
    for (uint i = 0; i < nums.length(); i++) {
        print(nums[i] + " ");
    }
    int total = 0;
    for (uint i = 0; i < nums.length(); i++) {
        total += nums[i];
    }
    print(total + "\n");
}

void main() {
    // Variadic functions can be called in the usual way with individual arguments.
    sum({1, 2});
    sum({1, 2, 3});

    // If you already have multiple args in an array, apply them to a variadic function using func(array).
    array<int> nums = {1, 2, 3, 4};
    sum(nums);
}

To run this AngelScript code:

  1. Ensure you have an AngelScript environment set up.
  2. Copy the code into a .as file.
  3. Execute the script using an AngelScript interpreter or integrate it into an application supporting AngelScript.

Now that you know how to use variadic functions in AngelScript, let’s delve into more advanced features of the language.