Game Development Community

Rendering the contents of an array

by Darren Stuart · in Torque Game Builder · 10/22/2005 (8:41 am) · 2 replies

I want to create a puzzle game with dropping blocks.

I would normally create an array and each pass I would render the contents.

any ideas where to start with it?

#1
10/24/2005 (9:30 am)
Hello, this is how I would try it...
Note: I'm not a very good programmer, and new to T2D, so it might not be the best solution.

for(%X=0;%X<%MaxX;%X++)
		for(%Y=0;%Y<%MaxY;%Y++)
			echo($Array[%X, %Y]);

So that code here would only print the values stored in your array.
I think that something similar to that could work. Of course you need to know know MaxX and MaxY here. I dont know if there's a command in T2D to know how much item an Array has?

Hope this helps, or make sense :)
#2
10/24/2005 (12:03 pm)
If I understand you correctly Darren, you're interested in how to manage a set of objects that you want to address using an array?

First, Nicholas is essentially correct in the notation itself.

As your question infers, T2D doesn't require you to compile a list of objects per frame that need rendering, you simply create them and add them to the scene and T2D will faithfully render them each frame for you.

What you need to do therefore is create your objects, let's say some static sprites and assign their id's into your array. Let's use something similar to what Nicholas posted for continuity...

*switches into v1.0.2 mode*

for ( %x = 0; %x < %maxX; %x++ )
{
	for ( %y = 0; %y < %maxY; %y++ )
	{
		$array[%x,%y] = fxSprite2D() { scene = myScene; };
	}
}

This is great now that we've got a bunch of objects all referenced in our $array variable but all our objects are all the same but more importantly, they're all at the same position which isn't too useful. We could position them within the same loop but let's do it in another loop for clarity...

for ( %x = 0; %x < %maxX; %x++ )
{
	for ( %y = 0; %y < %maxY; %y++ )
	{
		// Fetch an easier reference to type!
		%obj = $array[%x,%y];
		
		// Position/Size our object.
		%obj.setSize( 5 );
		%obj.setPosition( (%x * 5) SPC (%y * 5) );
	}
}

Note how we can pass the object around from variable to variable. An object id is just a number so no problem there. I transferred it from our array into a easier to type variable called "%obj".

The rest is totally up to you!

Hope this helps,

- Melv.