Game Development Community

Copies of Arrays?

by Rodney Rindels - Torqued · in Torque Game Builder · 06/12/2006 (11:50 am) · 7 replies

Maybe its just me .. but if I have an array.

$test[0]=1;
$test[1]=2;
$test[2]=3;

shouldn't I be able to make copies of $test?

Take the following example.
==>$test[0]=1;
==>$test[2]=2;
==>echo($test[2]);
2
==>%array=$test;
==>echo(%array[2]);

hum... is this expected behavior of TS? or is something afoot!

Because if you can't make copies of arrays, you can't pass them to functions...

#1
06/12/2006 (12:04 pm)
Expected behaviour I think. Torque Script doesn't really do arrays. I think I'm right in saying that when you define the following:
$test[0]=1;
$test[1]=2;
$test[2]=3;

What you actually get is:
$test_0=1;
$test_1=2;
$test_2=3;

I suspect the best way to emulate an array with a SimSet:
new SimSet(test);

new ScriptObject (element0)
{
	val = 1;
};

new ScriptObject (element1)
{
	val = 2;
};

new ScriptObject (element2)
{
	val = 3;
};

for (%i=0; %i<2; %i++)
{
	test.add (element @ %i);
}

for (%j=0; %j< test.getCount(); %j++)
{
	%foo = test.getObject(%j);
	echo (%foo.val);
}

So a bit more complicated, but much more powerful.
#2
06/12/2006 (12:07 pm)
I'm aware of that, but the same code that does the psuedo manipulation into array like vars, should be able to copy the pointers to assignments I would think?

I use simSets personally, this was really more of a "Shouldn't TS do this" question :-)
#3
06/12/2006 (12:07 pm)
Or you can use *real* arrays check the array add-on, use the search box I don't have the link at hand...
#4
06/12/2006 (12:13 pm)
My question wasn't really one of the best practice, but it seems to me that you should be able to make copies of any data structure.
#5
06/12/2006 (1:51 pm)
It constantly amazes me that Torque doesn't have real arrays. The SimSet approach gets clunky if you need to deal with ordered arrays that constantly change (perhaps someone will someday offer a comprehensive tutorial on the subject). Yes, you can add the array resource (the first thing I do when I get a new build); but that approach won't work for all the people who will be buying a license sans source code. All they're going to get is an executable; and will quickly be dumbstruck that it doesn't do real arrays!
#6
06/12/2006 (2:05 pm)
@Don, I agree, I dont know all the intimate details of the parser and lexer used to build TS, but I would think that if the array add-on works and is stable, they should consider packaging it natively, or spinning up their own soluton.

I know why I can't make copies of arrays, but I really think they should think about this, the problem is going to soon be, that they can't retrofit it in over the array thats there, because of backwards compatibility issues.

I dont think they need too, what they have now is a named variable representation of an array, so if they changed the caller, left the data type thats there alone and added a new type they could forego the risks of adding in a true array from a backwards compatibiltiy standpoint. Something like @varname is a true array, where $varname is the legacy, but then provide facilities to copy between them. @varname = $varname would iterate and populate @varname, etc.

I haven't really looked at the add on array code, because i'm working on stuff that I "can't" modify the source for at the moment, because I'm working on a utility resource for binary only users.

But for decent sorting algorithms , etc , I think they should consider adding it in the next gen of T2D (if they haven't already).

And if there are not any plans to add a true array, it would be nice to be able to pass the "array" thats there around like other TS data types.
#7
06/13/2006 (3:26 pm)
It is much simpler to do the following:

new ScriptObject (test);

test.val[1] = 1;
test.val[2] = 2;
test.val[3] = 3;


function printArray(%array)
{
      echo(%array.val[1]);
      echo(%array.val[2]);
      echo(%array.val[3]);
}

printArray(test);

NOTE: This does NOT copy the array, it will pass it by reference.