[ANSWERED] Scripting a function with optional arguments/ arglist?

Started by Janik, Fri 17/09/2004 18:01:52

Previous topic - Next topic

Janik

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
Play pen and paper D&D? Then try DM Genie - software for Dungeons and Dragons!

Radiant

It's probably easiest if you define a global array that stores the arguments. That's not quite the same but it'd work.

Pumaman

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.

Janik

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.
Play pen and paper D&D? Then try DM Genie - software for Dungeons and Dragons!

Radiant

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.

Janik

I don't think the script engine supports optional arguments either, which would be needed in order for that to work.
Play pen and paper D&D? Then try DM Genie - software for Dungeons and Dragons!

Gilbert

Well it's just user-custom functions can't use optional argument lists.
Built-in functions like Display() can accept various amount of arguments.

Radiant

Janik -> it doesn't, I was suggesting that to CJ actually. But it's not something most people would use, I think.

SMF spam blocked by CleanTalk