Game Development Community

Resolved: Arrays and Element Count

by Deozaan · in Torque Game Builder · 10/13/2007 (5:52 pm) · 11 replies

I did some searches on the forums for the answer to this, but they were all from 2005 or earlier, so I'm hoping things are updated since then.

Does TGB use Arrays properly now?

Basically I want to be able to pass an array to a function, and do something simple like %array.count() to get how many elements are in the array. Is this built in or do I still need to resort to a ScriptObject?

EDIT: I'm getting my info from here and here.

#1
10/13/2007 (6:50 pm)
No, there are no Array objects in TorqueScript. For most cases, you should be using a SimSet, but in those cases where you must absolutely be using an array, a ScriptObject wrapping the data, or possibly a global array are your options.
#2
10/13/2007 (6:57 pm)
Yay! SimSet is what I needed to know.

Thanks!
#3
10/15/2007 (12:19 pm)
On a slightly related note, anyone know how to use a SimSet to hold strings? I've got a dictionary of about 100,000 words that I'm using for a word game, and using a global variable for each ($dictionary[word] = true) makes me feel like a rookie.
#4
10/15/2007 (1:27 pm)
Well, you could wrap the strings in SimObjects, to use in a SimSet, but that's probably adding more overhead than necessary.

Actually, array indexes may be the most efficient way to hold a dictionary. If you're worried about cluttering the globals, you could stick them all in an object. i.e:
new ScriptObject(dictionary);
dictionary.words["word"] = true;
#5
10/15/2007 (7:20 pm)
That's interesting.

And thanks for the feedback. It's nice to have validation.

I'm going to keep my current approach with the globals.
#6
10/20/2007 (7:48 am)
@Mike, I have 92000 words inmy game and I'm using your approach also:

$Word[83746] = "ripped";
#7
10/23/2007 (9:44 pm)
Isaac or Mike or anyone:

What would be a good method of iterating over each entry in these kinds of arrays?

For instance, if I dynamically created string arrays in a variable $strArray[%str] where I might not know all the possible values of %str being entered in, how would I retrieve them or iterate through all of them?

More specifically: I've written some code that will iterate through objects in the scene based on the class name of the object, and then make a SimSet list of all those objects. If I want to make a list of more than one class I have to run the code again for each different class. As long as I don't clear the simSet list it will add to the list. The problem is, new objects of these classes may be constantly being created, so I need to keep the list updated. But once I change the code to look for another class, it won't remember the first class.

For example: I need to keep track of classes "team1" and "team2". So I run a pass and get all objects on team1, then another pass to get all objects on team2. But it needs to be run every so often to keep the list updated. So the next time it runs it will only remember team2 and while it will keep the team1 objects it already got on the list, it will only add team2 objects to the list. So I need an array of "team1" and "team2" that I can iterate through (where the string can be any value and may not be sequential or even related) easily to update the list with all objects it should be watching.

Does that make sense? If so, how would I go about doing that?
#8
10/23/2007 (10:15 pm)
Oy! Nevermind, I figured it out.

I'll store the strings in array[0] = "string", array[1] = "string2" etc and then store the count in array[count] and I can iterate using array[%i] just like standard arrays.
#9
10/24/2007 (10:36 am)
Deozaan:

that's the right way ;)
#10
10/25/2007 (11:54 am)
Deozaan,

I understand what you have described, but I do not know how you are using what you described, so this maybe off the mark.

If you use a SimSet, you already have getCount() and don't need to manually keep track of the count yourself. Using SimSet will also mean you can remove objects and you won't have to worry about "reindexing" to avoid indexes that aren't valid.
#11
10/25/2007 (12:48 pm)
Chaim,

From my understanding, a SimSet will only hold objects. I needed an array of strings, and the solution I came up with appears to me to be the best way to handle that.

To clear up any confusion: My initial post was about storing objects in an array. That's what the SimSet was for. After that I was wondering about how to handle arrays of strings. Since the topic of this thread is Arrays and Element Count and Isaac and Mike already mentioned string arrays, I figured it was as good as any place to ask specifically about strings.

Thanks for the help everyone!