Game Development Community

Array of scene objects - how to set up and manipulate?

by Mihailo Šundić · in Torque Game Builder · 06/26/2010 (11:42 am) · 7 replies

What i am trying to achieve is:

To have for example 5 scene objects as static sprite, to be named in a way so script can access them
as array.
say i have:
each sprite should be use in script in a way:

for (%i=1...)
{
  if (...)
  {
     spritearray[i].setLayer(1);
  }
  else
  {
     spritearray[i].setLayer(4); 
  }
}

where spritearray[1]..[10] would for example be objects i placed in TGB scene.

From TGBuilder in Edit tab and in dropdown Scripting,
there is a field name, under that is class etc, so i should name them in a way that i can access them
from script like i showed up there... is there a way to do this?

And what is the right approach for this?

TDN seems not to work for me... it shows some error about database and {hidden} or smthng like that.

Thx in advance!

About the author

Recent Threads


#1
06/26/2010 (3:07 pm)
Instead of making the objects in the builder, make them in script:

for( %i = 0; %i < 10; %i++ )
{
  $SpriteArray[%i] = new t2dStaticSprite()
  {
    scenegraph = ...;
    ...
  };
}

Then you can easily manipulate them in script like you have above.

Sometimes to make the whole process easier, I'll create a sprite in the builder, look in the level file for the sprite, copy it out into my scripts, and then delete it from the level.
#2
06/27/2010 (2:06 am)
Thx, i will do that.

You mean to create them in builder to be able to find their correct positions and other characteristics in scene file to easily use it in script?
#3
06/27/2010 (6:27 pm)
I don't necessarily use it for the correct position, as I often am using the array to set those anyway (for example, objects placed in a regular pattern), but your concept is right. I may use it to get the blending, physics, or other features that are less obvious.

If I need a few objects (say, less than 10) that have specific locations, I'll give them a Script Name, like "HUD0", "HUD1", "HUD2", etc. Then you can also use code like:

for( %i = 0; %i < 10; %i++ )
{
  eval( "%obj = HUD" @ %i @ ".getId();" );
  %obj.setVisible( false ); // or whatever else you need to do
}
#4
06/29/2010 (4:22 am)
Thx, very helpfull

I have one more, final question,
if i create those staticsprites in script, they will appear in all
levels but should actually appear for ex.not in main menu, but on
gameplay scene-level.

Can i do this?

if newgame button clicked
{
Schedule-load level file
all NEW t2dstaticsprites creation
}
ofcourse in script language :)

if quittomainmenu selected
{
Schedule-load main menu level
delete all the staticsprites
}

or do i have another option for this?
#5
06/29/2010 (11:00 am)
A really good place to put this code is in the scene callbacks for the specific scene you are creating.

First, in TGB, click on the t2dSceneGraph under the "Project" tab and then under the "Edit" tab -> "Scene Graph Scripting" -> "Name", give the scene a name (for example, "GameScene").

Then, in your scripts you can write the following:
function GameScene::onLevelLoaded( %this )
{
  // Code for creating the objects would go here.
  // Or even better, call a function here to
  // create the objects.
}

function GameScene::onLevelEnded( %this )
{
  // If you added the objects to the scene, the scene
  // will take care of deleting the objects, so you
  // don't *HAVE* to do anything here.
  //
  // If you new'd up other objects (like ScriptObjects)
  // in the "onLevelLoaded", then this is a good place
  // to delete those objects.
}
#6
06/29/2010 (11:20 am)
Well, just what i needed.
Perfect, thank you very much!
#7
06/29/2010 (12:48 pm)
No problem!