Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: Janik on Fri 17/09/2004 18:01:52

Title: [ANSWERED] Scripting a function with optional arguments/ arglist?
Post by: Janik on Fri 17/09/2004 18:01:52
Hello,

Is it possible to define a function with variable number of arguments? I want to do:

RandomSpeak(string Speech1, string Speech2, ...)
{
int ran = Random(NumberOfArguments)
//etc... display a random message from the list.
}

Thanks!

Edit: [ANSWERED] in the subject
Title: Re: Scripting a function with optional arguments/ arglist?
Post by: Radiant on Fri 17/09/2004 18:14:09
It's probably easiest if you define a global array that stores the arguments. That's not quite the same but it'd work.
Title: Re: Scripting a function with optional arguments/ arglist?
Post by: Pumaman on Fri 17/09/2004 19:58:13
Yeah, variable-argument lists are not supported in custom function definitions. Using some sort of global array (or the GlobalStrings) as Radiant suggests is probably the best solution, albeit messy.
Title: Re: Scripting a function with optional arguments/ arglist?
Post by: Janik on Fri 17/09/2004 21:05:05
Thanks for your answer. I think what I'll do is define a few functions like:

RandomSpeak2(sp1, sp2);
RandomSpeak3(sp1, sp2, sp3);
..
RandomSpeak6(sp1, sp2, sp3, etc.);


So that way I won't have to setup an array each time. Odds are I won't need more than a few (< 10) options anyway.
Title: Re: [ANSWERED] Scripting a function with optional arguments/ arglist?
Post by: Radiant on Mon 20/09/2004 12:32:16
Maybe adding default values for function parameters would be a solution.
(like when you declare a function,   int My_func (int x, int y = 20);   )
Then again that may be annoying to code and I doubt it'd be used all that often.
Title: Re: [ANSWERED] Scripting a function with optional arguments/ arglist?
Post by: Janik on Mon 20/09/2004 22:15:48
I don't think the script engine supports optional arguments either, which would be needed in order for that to work.
Title: Re: [ANSWERED] Scripting a function with optional arguments/ arglist?
Post by: Gilbert on Tue 21/09/2004 02:12:45
Well it's just user-custom functions can't use optional argument lists.
Built-in functions like Display() can accept various amount of arguments.
Title: Re: [ANSWERED] Scripting a function with optional arguments/ arglist?
Post by: Radiant on Wed 22/09/2004 11:35:42
Janik -> it doesn't, I was suggesting that to CJ actually. But it's not something most people would use, I think.