Game Development Community

Access argument in script

by gamer · in Torque Game Engine · 02/12/2008 (2:13 pm) · 2 replies

In torque script is it possible to reference the arguments of a function (argc, argv) ? and further, to pass them to another function dynamically?

function a(arg1, arg2,...){ //number of arguments is unknown at compile time
// is it possible to pass all arguments of a to b?

}
function b(arg 1, arg2,...){
}

#1
02/13/2008 (9:23 pm)
Absolutely. Just as you did.

I have a feeling I haven't answered your question, but it's pretty straightforward.
#2
02/14/2008 (8:03 am)
I will be interested to see if there are any slick methods available.
If not, you can still do it "manually" similarly to how the C++ code handles arguments:

function testArgReceiver(%argc, %argv0, %argv1, %argv2, %argv3, %argv4, %argv5)
{
   //   echo(%argc SPC %argv0 SPC %argv1 SPC %argv2 SPC %argv3 SPC %argv4 SPC %argv5);

   // Trick to allow array addressing later.
   // Seems they have to be used once inside the function first.
   // (Can't seem to use array addressing when only used as calling arguments.)
   %argv0 = %argv0;
   %argv1 = %argv1;
   %argv2 = %argv2;
   %argv3 = %argv3;
   %argv4 = %argv4;
   %argv5 = %argv5;
   
   for (%i=0;%i<%argc;%i++) {
      if (%argc>6) {
         break;
      }
      echo("testArgReceiver: arg#" @ %i @ " is = " @ %argv[%i]);
   }
}

//test calling script methods with varying numbers of args
function test1()
{
   echo("--- testArgReceiver(1,0,1,2,3,4)");
   testArgReceiver(1,0,1,2,3,4);
   echo("");
   
   echo("--- testArgReceiver(3,0,1,2)");
   testArgReceiver(3,0,1,2);
   echo("");

   echo("--- testArgReceiver(4,0,1,2,3,4)");
   testArgReceiver(4,0,1,2,3,4);
   echo("");

   echo("--- testArgReceiver(10,0,1,2,3,4,5,6,7,8,9)");
   testArgReceiver(7,0,1,2,3,4,5,6);
   echo("");
   
   echo("--- testArgReceiver(6,0,1,2,3,4,5)");
   testArgReceiver(6,0,1,2,3,4,5);
   echo("");
}