Blockland Forums > Modification Help
Arrays
(1/2) > >>
phflack:
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?
otto-san:
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
M:
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: ---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);
--- End code ---
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.
Slicksilver:

--- Quote from: M on November 26, 2011, 04:39:17 AM ---To clarify, arrays in Torque aren't actually arrays, they're substitutions to insert variables into variable names.

--- End quote ---
forget you i wanted to say that
SpreadsPlague:

--- Quote from: Slicksilver on November 26, 2011, 05:11:30 PM ---forget you i wanted to say that

--- End quote ---

I was wondering why they looked nothing like actual arrays ...
Navigation
Message Index
Next page

Go to full version