Game Development Community

Is this how ArrayObject is supposed to work?

by Daniel Buckmaster · in Torque 3D Beginner · 11/16/2012 (5:40 pm) · 2 replies

I really hadn't used ArrayObject before now, preferring to stay out of TorqueScript. But I've just started to get my feet wet, and realised that ArrayObject does not work the way I thought it did. I'm sure I've seen code snippets like this:
new ArrayObject(MyArray);
MyArray["key1"] = "value1";
MyArray["key2"] = "value2";

echo(MyArray["key1"]);
which, as you would expect, printed "value1". But I have discovered that this doesn't actually work. Is there a reason for this? Also, there's no way to get a value straight from a key. So, for example, doing this:
echo(MyArray.getValue("key2"));
prints "value1" because "key2" is magically cast to the integer value 0 code-side. So if I want to get the value based on a key, I need to write:
%val = MyArray.getValue(MyArray.getIndexFromKey("key2"));
What?

About the author

Studying mechatronic engineering and computer science at the University of Sydney. Game development is probably my most time-consuming hobby!


#1
11/16/2012 (6:47 pm)
ArrayObject works in an odd way, there is a index value and a key value.

function ArrayObject::getValueKey(%this,%key){
 	return %this.getValue(%this.getIndexFromKey(%key));
}

MyArray.add(%key,%value); // how to add data to array
MyArray.add("1,1","fish");

echo(MyArray.getValueKey("1,1")); should return fish

MyArray.echo(); you should get something like this
example:
edit: for some reason it removed the spaces, so i replaced with dashes
Index--Key----Value
1------"1,1"--"fish"
2------"1,2"--"cow"
3------"2,1"--"rabbit"
4------"2,2"--"a"
5------"3,1"--"b"
6------"3,2"--"c"

also this -> http://docs.garagegames.com/torque-3d/reference/classArrayObject.html
#2
11/16/2012 (7:20 pm)
Thanks for the confirmation, Casey. I was just surprised, both because I thought I'd seen the class working differently before, and because it's very different to how associative arrays work in any other language.

Oh, and you can format spaces using [code ]code tags[/code ] (without the spaces):
first line never has any leading spaces
     but after that,     all     spaces are preserved!