[RESOLVED] Do AGS scripts support a variable number of parameters

Started by Joacim Andersson, Thu 09/01/2025 09:06:28

Previous topic - Next topic

Joacim Andersson

Some of the built-in API functions in AGS allows for a variable number of arguments to be passed to them. Does the script language also support that?

For example in Java you can write:
Code: ags
private int addUp(int ... nums) {
  int sum = 0;
  for (int num : nums)
    sum += num;
  return sum;
}
Or in C#:
Code: ags
private int AddUp(params int[] nums)
{
  // Before anyone tells me, yes I'm aware that you can use 
  // the Sum extension method from System.Linq:
  // return nums.Sum();
  int sum = 0;
  foreach(int num in nums)
    sum += num;
  return sum;
}

Crimson Wizard

#1
Looking at your examples, I cannot tell which kind of functionality exactly are you referring to.

AGS currently supports variadic functions in C style, where the function does not restrict parameter types. In other words, not only the number, but also types of parameters is variable.
If that, then the short answer will be "No". You can declare a function with "...", but AGS does not have a syntax for retrieving these parameters inside the function, nor any mechanism to recognize their types. In fact, even the engine cannot always recognize their types because of how ags script works. That's why "..." is only used for text formatting, where the passed values are treated either as a number (int, float) or a pointer value, but never tried to be accessed as an object.

If you are referring to some sort of a syntax sugar over generating a dynamic array of T and passing it to function, then the answer is also "no".
But at least that's something that is possible to do by hand.

Joacim Andersson

Quote from: Crimson Wizard on Thu 09/01/2025 10:35:24Looking at your examples, I cannot tell which kind of functionality exactly are you referring to.
I just wondered if AGS scripts allow an unspecified number of arguments, in my (stupid) example above you could in C# pass any number of int arguments to the AddUp() method. The AGS API has that in for example in the String.Format() method and in the AbortGame() function.

In the String.Format() method (which is also supported by the Display() function), the first string you pass in tells the type of the arguments that follow. However I was looking for something like my example above where you say that all arguments will be of the type int, you just don't specify how many the function accepts.

But, OK. Your reply answered the question that AGS scripts don't support that functionality, so thank you for that.

SMF spam blocked by CleanTalk