Game Development Community

My problem boils down to arrays, I'm sure

by Matt Troup · in Torque Game Builder · 08/18/2005 (2:35 am) · 6 replies

I apologize this has been asked in different ways twice in the past couple weeks, but I can't seem to figure out my specific problem from the answers given. I've read Tut's tutorial that shows how to handle arrays, but I'm just not grasping it. I appreciate anyone who's willing to put a little help in this thread so hopefully I can better understand what I want to accomplish.

Here's a simple scenario of what I'm trying to do:

Say I have 4 tetris blocks - for the sake of simplicity, they'll all be square. Use "tilemap" cells 0-3 in the images folder for easy reference to represent those squares. At point (0 0) I'd like one of those blocks to be randomly generated to show the "next" square to be used after the current square in play. After I'm done doing whatever with the current block in play, I want to move the block currently seen in the "next" window from the origin to coordinate (-25 -35), and spawn a random new block to the now vacant "next" window at the origin.

So, I gather I have to create an array to handle the multiple instances of the same sprite. I see how Matt showed us to create an array for a single sprite - but what about when there are 4 sprites involved? I can't seem to grasp it. I believe the array will allow me to handle individual instances of the same sprite so I can independently manipulate the current block in the playfield and the block in the next window.

The following tutorial Matthew Langley wrote and discusses arrays amongst other things:

http://www.garagegames.com/uploaded/code/7381.torque2dtutorial.html#step1

#1
08/18/2005 (3:02 am)
You could always try using SimSet or SimGroup to achieve this:

%myStuff = new SimSet(); // or, %myStuff = new SimGroup();

%myStuff.add(%sprite);

and then

for(%i = 0; %i < %myStuff.getCount(); %i++)
{
%aSprite = %myStuff.getObject(%i);
}

You could maybe have a variable called %nextBlock which hold a reference to the upcoming tetris block, and when it comes in to play you set another variable, %currentBlock to hold the same reference and then generate a new block for %nextBlock. When %currentBlock goes out of play you could add it to the SimSet/SimGroup as above to keep track of all blocks on the field....

Hope I managed to _not_ confuse you more by this...
#2
08/18/2005 (9:10 am)
Here's another option:

%blockObjects = new SimGroup();

for (%iii = 0; %iii < 10; %iii++)
{
    %blockObjects.blockSprite[%iii] = new fxStaticSprite2D() {scenegraph = t2dSceneGraph;};
}

%blockObjects.blockSpriteCount = 10;

This gives you a parent object (blockObjects) that you can stick any variables to (here I've got the variable blockSprite, an array of new sprite instances). Since there are almost no array support functions like .size(), it is good to keep track of the count of array objects nearby (blockSpriteCount in this case).

However there are many ways to do this too. When you create any new object, the variable you assign it to is just the object ID integer (which exists whether you actually assign it to a variable or not upon creation) so you can do anything you want with it such as append them to a string with spaces in between:

%blockSprites = "";

for (%iii = 0; %iii < 10; %iii++)
{
    %newBlockSprite = new fxStaticSprite2D() {scenegraph = t2dSceneGraph;};

    %blockSprites = %blockSprites SPC %newBlockSprite;
}

%blockSprites = trim(%blockSprites);

With the above method, you can pass one variable around easily (you can't pass direct arrays, they have to be hanging off an object to do so) and get the count of objects with getWordCount(%blockSprites) which checks for single spaces to define 'words'. There are a bunch of other word-oriented functions built-in, and of course you can easily expand on these with a few of your own functions to insert items, etc.
#3
08/18/2005 (7:40 pm)
Well, I've got something that works. It's actually the method Matthew Langley provided in his tutorial, but the way I implemented it is far from optimized I'm sure. I thank you guys for your help (Luke, thanks again) for helping me. I'll have to read more about these simSets and groups because I think it'll make my code more compact and fool-proof than what I've done so far.
#4
08/18/2005 (9:16 pm)
So I said to myself, "Self, what is the difference between a SimSet and SimGroup?"

Here's what I learned, after looking through the source code (engine/console/simBase.*):

SimGroup is derived from SimSet or said another way SimSet is the parent class of SimGroup.

There two important things that SimGroup does different from SimSet:
1. An object can only belong to ONE SimGroup
2. When SimGroup is deleted it deletes all the objects in it!

So you could say the SimGroup is a hoarder compared to SimSet or as the source code comment says, "A SimGroup is a stricter form of SimSet."

Maybe ya'll find that useful.
#5
08/19/2005 (8:47 am)
SimGroups can be very useful, great for cleaning up resources, in fact in Torque 3D theres a simgroup with each mission called "MissionCleanUp"... so when you create an object you do

MissionlCleanUp.add(object);

and at the end of the mission that SimGroup is deleted which then clears out those resources :)
#6
08/19/2005 (10:14 am)
Ah! So that's a simGroup. I was reading about that in Kenneth Finney's "3D Game Programming all in One". It makes quite a bit more sense now... doesn't mean I'll be quick to change what's not broken, though. I'm just glad I got what I wanted working. Heheh.