Author Topic: Functions with unlimited arguments  (Read 2305 times)

I was wondering how you create functions which can have an indefinite number of arguments in torquescript. The schedule() function is an example of one. You can give it as many arguments as you like. How can I declare a function like that?

Wtf are you talking about, schedule doesn't have unlimited arguments?

There's a maximum number of arguments hardcoded into the language. I think that number is 16.

If you need more, then you can use a tab or space seperated list as a string in one or more of your 16 default arguments, for example "blah 1234 stuff morestuff"

You can theoretically make as many arguments as you want that way.

There's a maximum number of arguments hardcoded into the language. I think that number is 16.
The number is 20.

The only real way is to make a lot of placeholder arguments and iterate backwards through them until you find one that isn't blank.

The schedule function is defined/handled by the engine and not through TorqueScript itself.

The only real way is to make a lot of placeholder arguments and iterate backwards through them until you find one that isn't blank.
Clever.

What I have done in the past is to store a large amount of data as a global array, and then have the function read through that array.

Unfortunately, functions with unlimited arguments are declared in C++ rather than Torquescript. Aside from using the bindings I made to declare C++ functions, it is impossible to declare a function with more than 20 arguments. Historically, mods have used a different keyword identifier so they can be used through chat ("!command arg1 arg2 arg3 ...") but for non-serverCmds your best bet is to either concatenate all of the variables into one string or use a global array.

the way i do it;

Code: [Select]
function blah(%a,%b,%c)
{
    %arg1 = %a;
    %arg2 = %b
    %arg3 = getField(%c,0);
    %arg4 = getField(%c,1);
}


----

%a = "bla";
%b = "bla";
%c = "useful" TAB "data" TAB "here";
function blah(%a,%b,%c);

separating the last field with tabs will let you make as many arguments as you'd like, just retrieve them with getfield

This and default argument values are features I wish torque script had

Or, a more dynamic way of doing it:

Code: [Select]
function NAME(%argset0, %argset1, %argset2, ...)
{
%argsetCount = 3; //Enter in the number of %argset 's you have
%currentArg = 0;

for(%a = 0; %a < %argsetCount; %a++)
{
%argscount = getFieldCount(%argset[%a]);
for(%b = 0; %b < %argscount; %b++)
{
%args[%currentArg] = getField(%argset[%a], %b);
%currentArg++;
}
}

//Do stuff here, and use %args[index] to access each argument
}

And to convert say, an array of variables into an argument set:

Code: [Select]
//Example array of variables
%arrayLength = 4;
%array[0] = 15;
%array[1] = 6;
%array[2] = 2;
%array[3] = 1;

if(%arrayLength > 0)
{
%argset = %array[0];

for(%a = 1; %a < %arrayLength; %a++)
{
%argset = %argset TAB %array[%a];
}
}

//Use %argset here

Actually... Why do you need infinite args, anyway? O.o