$blah = new SimObject(asd); ==>$blah.myMethod(10, 20, 30); argv[0] "> Error using array-syntax | Torque Game Builder | Forums | Community | GarageGames.com

Game Development Community

Error using array-syntax

by BlueRaja · in Torque Game Builder · 07/27/2008 (10:47 pm) · 9 replies

function asd::myMethod(%this, %argv0, %argv1, %argv2)
{
   for(%i = 0; %i < 3; %i++)
      echo("argv[" @ %i @ "] is " @ %argv[%i]);
}
Running this code gives the following output:
==>$blah = new SimObject(asd);
==>$blah.myMethod(10, 20, 30);
argv[0] is 
argv[1] is 
argv[2] is

Now I edit myMethod to read as follows:
function asd::myMethod(%this, %argv0, %argv1, %argv2)
{
   echo("Going to echo %argv0 first: " @ %argv0);

   //Same as before
   for(%i = 0; %i < 3; %i++)
      echo("argv[" @ %i @ "] is " @ %argv[%i]);
}
this produces the following output:
==>$blah = new SimObject(asd);
==>$blah.myMethod(10, 20, 30);
Going to echo %argv0 first: 10
argv[0] is 10
argv[1] is 
argv[2] is

Finally (and this is the really weird part), I change myMethod to the way it was before and add another method, as so:
function asd::myMethod(%this, %argv0, %argv1, %argv2)
{
   for(%i = 0; %i < 3; %i++)
      echo("argv[" @ %i @ "] is " @ %argv[%i]);
}

function asd::myMethod2(%this, %argv0, %argv1, %argv2)
{
   echo("Going to echo %argv0 first: " @ %argv0);

   //Same as asd::myMethod
   for(%i = 0; %i < 3; %i++)
      echo("argv[" @ %i @ "] is " @ %argv[%i]);
}
Now I get the following output:
==>$blah = new SimObject(asd);
==>$blah.myMethod(10, 20, 30);
argv[0] is 10
argv[1] is 
argv[2] is 
==>$blah.myMethod2(10, 20, 30);
Going to echo %argv0 first: 10
argv[0] is 10
argv[1] is 
argv[2] is

#1
07/27/2008 (11:48 pm)
Yeah there is something very strange going on... heres an even more clear example

function testarguments( %argv0, %argv1, %argv2 )
{
   echo( %argv0 );
   echo( %argv1 );
   echo( %argv2 );
   
   for ( %i = 0; %i < 3; %i++ )   
      echo( %argv[%i] ); 
}

Type in testarguments(1,2,3) and you get 1,2,3 echoed twice. Now comment out the three echo lines. You get empty strings echoed... if you set a breakpoint in torsion you can see the arguments actually ARE empty strings...

Why? I tried stepping through the code for executing a codeblock but gah, I have no idea.
#2
07/28/2008 (12:11 am)
Something similiar happens with %var.X/.Y/.Z with an uninitialised variable. By uninitialised, I mean unused.

www.garagegames.com/mg/forums/result.thread.php?qt=75001
#3
07/28/2008 (3:56 am)
There's a bug with TGB, where it isn't able to use a function parameter in a call to another function unless the variable has been used.

You can work around it, by assigning your parameters to themselves, or putting them into local variables and using those.
#4
07/28/2008 (9:07 am)
Gary: That's not the problem here. Take a look at these examples (which must be compiled separately, see the third example in my first post):
function asd::myMethod(%this, %argv0)
{
   echo(%argv0);
}
function asd::myMethod2(%this, %argv0)
{
   echo(%argv[0]);
}
The first example works as expected. The second does not. However, if you put both in the same file, then both will work as expected.
I have a feeling there's some optimization done by the compiler that's not working correctly here.
#5
07/28/2008 (9:28 am)
Quote:
function asd::myMethod2(%this, %argv0)
{
echo(%argv[0]);
}

I believe this may still be related to the same bug, just a different way of seeing it.

If you modify your second example to:

function asd::myMethod2(%this, %argv0)
{
   %argv0 = %argv0;
   echo(%argv[0]);
}

It will work.

There's clearly a bug somewhere in the scripting engine though I've not looked into what it may be. Andreas noticed a similar version of this which occurs when you pass a variable into an eval call. The variable is treated as blank for the eval call unless it's previously been accessed within the function (such as by assigning it to itself). Although in the case of the eval bug, whether you use array syntax or not doesn't matter.
#6
07/28/2008 (9:47 am)
Gary: I don't think it has anything to do with the fact that the variable is unused before being passed to a method, though. For instance, the following code also does not work as expected:
function asd::myMethod(%this, %argv0)
{
   %otherVar = "hello!";
   %otherVar = %argv[0];
   echo(%otherVar);
}
Output:
==>$blah = new SimObject(asd);
==>$blah.myMethod(10);

==>
However, once again, if the variable %argv0 is used in either this method or in any other method in the file (including later on in the same method), it works correctly.

function asd::myMethod(%this, %argv0)
{
   echo(%argv[0]);
   echo(%argv0);
}
This correctly displays "10" twice. Remove the last line of the method and it displays nothing.
#7
07/28/2008 (10:55 am)
Quote:Gary: I don't think it has anything to do with the fact that the variable is unused before being passed to a method, though. For instance, the following code also does not work as expected:

Looks like there's a couple of bugs then :(
#8
07/28/2008 (11:00 am)
I think the "unused" explanation is the clearest of whats going on. I bet that this works...

function asd::myMethod( %this, %argv0, %argv1, %argv2 )
{
   %argv0 = %argv0;
   %argv1 = %argv1;
   %argv2 = %argv2;

   for ( %i = 0; %i < 3; %i++ )   
      echo( %argv[%i] );
}

Basically, using the array notation to "use" a variable does not count as a "use" as far as this bug is concerned, you must actually have the variable TYPED letter for letter somewhere in the function/method or it will equal "".
#9
07/28/2008 (11:33 am)
Well, I realize this may not be fixed for some time (if ever), so the temporary solution to this (for you future forum-searchers) that I used is to create a method that looks like this:

///Necessary for the method with arbitrary arguments to work correctly.  See
/// http://www.garagegames.com/mg/forums/result.thread.php?qt=77534
/// for more details
function asd::_dontCallMe(%this, %argv0, %argv1, %argv2,
         %argv3, %argv4, %argv5, %argv6, %argv7, %argv8,
         %argv9, %argv10, %argv11, %argv12, %argv13, 
         %argv14, %argv15)
{
   echo(%this @ %argv0 @ %argv1 @ %argv2 @
        %argv3 @ %argv4 @ %argv5 @ %argv6 @ %argv7 @ %argv8 @
        %argv9 @ %argv10 @ %argv11 @ %argv12 @ %argv13 @ 
        %argv14 @ %argv15);
}

Note that because of the strange behaviour of this bug, this does not need to be the same method that's having problems with passing arbitrary parameters - it only needs to be (anywhere) in the same file. Hope this saves someone from the same frustration I had to go through!
------------------------------------
keywords for this thread (for easy searching): pass passed passing arbitrary argument arguments parameter parameters method methods function functions array arrays argc argv bug empty string