script array
by Orion Elenzil · 03/12/2008 (9:56 am) · 16 comments
Arrays in TG* have some limitations, notably that you can't get a list of how many elements are in them, and that you can't pass them around directly. (you have to embed them inside an object, or some other such trick).
This resource is a trivial wrapper which does the bookkeeping of keeping an array in a script object.
It solves the two problems mentioned above.
The array is one-dimensional.
Note,
if you're interested in this, you might want to first check out this script-based array resource, which is a bit more full-featured.
This resource is a trivial wrapper which does the bookkeeping of keeping an array in a script object.
It solves the two problems mentioned above.
The array is one-dimensional.
Note,
if you're interested in this, you might want to first check out this script-based array resource, which is a bit more full-featured.
////////////////////////////////////////////////////
//
// scriptArray.cs
//
// a script-only implementation of one-dimensional arrays which keep track of their members
// this is a very primitive array: the keys must be contiguous integers, starting at 0.
// o. elenzil 20080218
//
// usage:
//
// creation:
// %array = new_ScriptArray("");
// new_ScriptArray(someName);
//
// adding elements:
// %array.append(something);
// %array.set(index, something);
//
// retrieving elements:
// %array.get(%index);
//
// retrieving number of elements in the array:
// %array.size();
//
// dumping everything in the array:
// %array.dumpValues();
function new_ScriptArray(%name)
{
%obj = new ScriptObject("ScriptArray");
%obj.numElements = 0;
%obj.setName(%name);
return %obj;
}
function ScriptArray::append(%this, %value)
{
%this.array[%this.numElements] = %value;
%this.numElements++;
}
function ScriptArray::get(%this, %index)
{
if (%index < 0 || %index >= %this.numElements)
{
error("ScriptArray::get()" SPC "- Subscript out of range:" SPC %index);
return "";
}
return %this.array[%index];
}
function ScriptArray::set(%this, %index, %value)
{
// note this allows you to go beyond the end of the array by one.
if (%index > %this.numElements)
{
error("ScriptArray::set()" SPC "- Subscript out of range:" SPC %index SPC "value:" SPC %value);
return;
}
if (%index == %this.numElements)
%this.append(%value);
else
%this.array[%index] = %value;
}
function ScriptArray::size(%this)
{
return %this.numElements;
}
function ScriptArray::dumpValues(%this)
{
for (%n = 0; %n < %this.numElements; %n++)
{
echo(%n SPC %this.get(%n));
}
}About the author
#2
03/13/2008 (1:19 pm)
Nice work Orion. I always see your nifty resources but haven't had a need to use most of them. This is one that I definitely have a use for. Thanks for sharing.
#3
03/13/2008 (2:04 pm)
w00t!
#4
Thanks again Orion
03/22/2008 (6:34 am)
@Orion: You are the man. Haven't tried it yet, but it is really needed. Arrays I missed them so!!!! I will definitely put this to task. I assume we can pass these in functions?Thanks again Orion
#5
yep !
i'm glad you're going to try it out, let me know if there's issues w/ this guy!
03/22/2008 (10:05 am)
> I assume we can pass these in functions?yep !
i'm glad you're going to try it out, let me know if there's issues w/ this guy!
#6
Compiling base/client/scripts/scriptArray.cs...
Loading compiled script base/client/scripts/scriptArray.cs.
Then after loading my mission I get the following from the console:
==>$x = new_ScriptArray("");
base/client/scripts/scriptArray.cs (51): Unknown command bindClassName.
Object (6552) ScriptObject -> SimObject
==>$x.append("How");
(0): Unknown command append.
Object (6552) ScriptObject -> SimObject
All right what did I do wrong.
03/23/2008 (10:11 am)
@Orion: I load the file as follows:Compiling base/client/scripts/scriptArray.cs...
Loading compiled script base/client/scripts/scriptArray.cs.
Then after loading my mission I get the following from the console:
==>$x = new_ScriptArray("");
base/client/scripts/scriptArray.cs (51): Unknown command bindClassName.
Object (6552) ScriptObject -> SimObject
==>$x.append("How");
(0): Unknown command append.
Object (6552) ScriptObject -> SimObject
All right what did I do wrong.
#7
my bad. i went and used a routine which i guess is custom and not in the stock codebase.
i've changed the resource above and actually tested it out (gasp!) with the stock game engine demo and it seems to work.
sorry!
03/23/2008 (10:33 am)
doh!my bad. i went and used a routine which i guess is custom and not in the stock codebase.
i've changed the resource above and actually tested it out (gasp!) with the stock game engine demo and it seems to work.
sorry!
#8
03/24/2008 (7:10 am)
Thanks a lot Orion I will play with it today. I am trying to load it early with some other scripts that need to be available all the time. That seems the correct way. Anyhoo wull let you know.
#9
03/24/2008 (7:17 am)
Excellent!!! I will have fun with this. So I thanks you a lot.
#10
Thanks again!!!
03/24/2008 (3:40 pm)
@Orion: Hey bro! You don't happen to have heard of any code to allow us to create structures in TorqueScript. Structures would make it much better to encapsulate data. Just a thought. The Arrar functions work great and I am using them already.Thanks again!!!
#11
i'm not sure what you mean by structures, tho.
you mean like C's structs ?
03/24/2008 (5:26 pm)
hey Shon - glad it's working!i'm not sure what you mean by structures, tho.
you mean like C's structs ?
#12
03/24/2008 (5:42 pm)
@Orion: That's exactly what I mean. The good old struct statement.
#13
see the array resource here for example, the way it initializes numElements.
or something like this:
(dumpFields is another custom routine, here, but you could just use stock dump().
03/24/2008 (5:45 pm)
well, any ScriptObject can be assigned fields, which seems similar.see the array resource here for example, the way it initializes numElements.
or something like this:
[3/24/2008 17:43:30][Inf][General] ==>$a = new scriptObject() { foo = "hey"; bar = "there"; };
[3/24/2008 17:43:32][Inf][General] ==>$a.dumpFields();
[3/24/2008 17:43:32][Inf][General] 8200-ScriptObject---
[3/24/2008 17:43:32][Inf][General] Member Fields:
[3/24/2008 17:43:32][Inf][General] Tagged Fields:
[3/24/2008 17:43:32][Inf][General] bar = "there"
[3/24/2008 17:43:32][Inf][General] foo = "hey"(dumpFields is another custom routine, here, but you could just use stock dump().
#14
03/25/2008 (7:45 am)
I think I see, this resource could be modified to allow the name you supplied to be int, char, bool, etc. and then you pass the array/structure wherever you need it. I alway forget that all variables are strings in TorqueScript. I appreciate your thoughts on this subject. Nice example! I didn't realize it told us what the field types were, Member or Tagged, that can be very useful.
#15
07/15/2008 (6:04 pm)
folks interested in this might also want to check out [url]=http://www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=15077]a similar resource by James Ford[/url], which i think is a bit more full-featured.
#16
Instead of shuffling the array and setting it up in a new random order. I get random repeats throughout the array.
01/18/2009 (8:41 pm)
This has been very useful for me. I've been trying to add a shuffle function to this, but having trouble getting it to work. Can anyone help?Instead of shuffling the array and setting it up in a new random order. I get random repeats throughout the array.
function ScriptArray::shuffle(%this)
{
%this.numElements = 6;
for (%marker = %this.numElements - 1; %marker >= 0; %marker--)
{
%n = getRandom(0, %marker);
%tmp = %this.array[%n];
%array[%n] = %this.array[%marker];
%this.array[%marker] = %tmp;
}
} 
Associate Orion Elenzil
Real Life Plus
==>$x = new_ScriptArray(""); ==>$x.append("how"); ==>$x.append("do"); ==>$x.append("you"); ==>$x.append("do"); ==>$x.dumpvalues(); 0 how 1 do 2 you 3 do ==>$x.set(1, "might"); ==>$x.dumpvalues(); 0 how 1 might 2 you 3 do