Author Topic: Arrays  (Read 713 times)

so, how exactly do they work? it seems like the keybinds is an array list, is it possible to make a function be part of an array?

Arrays are literally just variable names.


%this["doggy"]


is the same as

%thisdoggy

and

%this["doggy", "notkitty"]

is the same as

%thisdoggy_notkitty


They are extremely useful for a lot of things. ie storing mass amounts of different kinds of data

%this.data["ottosparks", %dataType] = %dataValue; (which of course could be accessed as %this.dataottosparks_(whateveryouhadasdatatype).)

edit:

you can also technically do multi-dimensional arrays like this:

%blah["boo_baah"]

and you could access it as %blah["boo", "baah"] too

To clarify, arrays in Torque aren't actually arrays, they're substitutions to insert variables into variable names.

When you assign %array[1] you are really making %array1. %array[1,1] is really %array1_1. The variable %array itself is unchanged, and passing it to another function passes none of the data included in the arrayed part (this is a really huge shortcoming of Torquescript).

A solution to this would be using a ScriptObject with values assigned, like
Code: [Select]
function array(%str)
{
   %obj = new ScriptObject();
   %str = strReplace(expandEscape(%str),",","\t");
   for(%i=0;%i<getFieldCount(%str);%i++)
   {
      %obj.v[%i] = getField(%str,%i);
   }
   %obj.length = getFieldCount(%str);
   return %obj;
}
echo(array("rooster,balls,3,giraffe").length);
But this drives up object pointers (not too big of an issue), is really weird to work with (%array.v[0] and enclosing the whole value of the array as a string - it's syntactically sugar-free), potentially results in bad memory use (the object will still exist until explicitly deleted, instead of being discarded with the scope it exists in). It's a feasible solution, just takes some different habits to deal with. But you can pass all of that data all at once as a potentially iterable set of values to another function as only one argument, which is really, really handy. So if that's what you're doing with it, send it on and remember to delete it when you're done.

To clarify, arrays in Torque aren't actually arrays, they're substitutions to insert variables into variable names.
forget you i wanted to say that

forget you i wanted to say that

I was wondering why they looked nothing like actual arrays ...

of course since they're just variable names that saves a ton of memory doesnt it

the object will still exist until explicitly deleted
That's why you make a garbage collect function.
Gratned by the time you've added a system to index and keep track off all your array values, you've practically made an entire element of a programming language, inside a programming language.

of course since they're just variable names that saves a ton of memory doesnt it
Not really (well, I guess like one extra pointer, but that's not that much of a big deal).

That's why you make a garbage collect function.
Gratned by the time you've added a system to index and keep track off all your array values, you've practically made an entire element of a programming language, inside a programming language.
And how would you check to see if there's no references left for the array?

Have fun literally parsing and interpreting all the script using string manipulation to detect that accurately. Just delete it yourself.