Book Image

Torque 3D Game Development Cookbook

By : DAVID WYAND
Book Image

Torque 3D Game Development Cookbook

By: DAVID WYAND

Overview of this book

Torque 3D is a popular game engine that supports you in every step along the way to making your game a reality. Even with all of the power and tools that Torque 3D provides, finishing a high quality 3D game requires time and knowledge."Torque 3D Game Development Cookbook" is a practical guide that takes you through each of the major steps on the journey to creating your game, while learning a few tricks along the way.The recipes in this book start off with learning some of the finer points about TorqueScript. The book then moves on to each of Torque 3D's subsystems and ends with a variety of game play recipes.The various topics covered include activating level-specific game code and scheduling game events, dragging and dropping items between windows to work with an in-game inventory system, and covering the seams between objects with well placed decals. Some of the advanced topics include writing custom shaders and postFX, using zones to improve rendering performance, and enhancing your game's ambience through sound.Once you are done with Torque 3D Game Development Cookbook you'll be on your way to creating amazing 3D games and gain expert knowledge of Torque 3D.
Table of Contents (17 chapters)
Torque 3D Game Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Iterating on objects in a SimSet or SimGroup collection


The SimSet and SimGroup classes hold collections of game objects, also known as SimObject (the base class of nearly every object in Torque 3D). In this recipe we will iterate through a collection of objects in order to perform some operation on them.

Getting ready

We will be adding a new TorqueScript function to a project based on the Torque 3D Full template and try it out using the Empty Terrain level. If you haven't already, use the Torque Project Manager (Project Manager.exe) to create a new project from the Full template. It will be found under the My Projects directory. Then start up your favorite script editor, such as Torsion, and let's get going!

How to do it...

We are going to write a TorqueScript function that will iterate through the objects of a SimSet or SimGroup collection as follows:

  1. Open the game/scripts/server/game.cs script file and add the following code to the bottom:

    function iterateSimGroup1()
    {
       // Iterate through the MissionGroup SimGroup to retrieve
       // each SimObject.  This holds the top level objects from
       // the loaded level.
       foreach (%obj in MissionGroup)
       {
          // Print some information about the object in the group
          echo(%obj.getId() SPC %obj.getClassName());
       }
    }
  2. Start up our game under the My Projects directory and load the Empty Terrain level. Open the console using the tilde (~) key and enter the following at the bottom of the screen:

    iterateSimGroup1();
    

    In the console we will see the following output:

    ==>iterateSimGroup1();
    4275 LevelInfo
    4276 ScatterSky
    4277 TerrainBlock
    4278 SimGroup
    

How it works...

The foreach() function (which is different than the foreach$() function that is used to parse space-delimited strings) is used to iterate through a SimSet or SimGroup collection in order to retrieve one SimObject instance at a time and perform some operation on it. In this example, the ID and class name of object are output to the console.

The foreach() function is different than most of the looping TorqueScript functions (such as for()), in that it takes two parameters that are separated by the word in rather than a semicolon. It is also unusual in that it creates a new variable to hold the current SimObject instance (the %obj variable in our previous example). The foreach() function has the following form:

foreach( object in simgroup)
{
   ... Do something with object ...
}

Here, the simgroup parameter is the collection of objects to be processed (could also be a SimSet parameter), and the object parameter is a new variable that is created to hold the current SimObject instance. It is the object variable that we do work on.

There's more...

An alternative method to access a object collection of a SimSet or SimGroup parameter is to use a standard for() loop and getObject() method of the collection. For example, put the following function at the end of the game/scripts/server/game.cs script file, following the code we entered in the foreach() example:

function iterateSimGroup2()
{
   // Get the number of objects in the SimGroup
   %count = MissionGroup.getCount();
   
   // Iterate through the MissionGroup
   for (%i=0; %i<%count; %i++)
   {
      // Retrieve the object
      %obj = MissionGroup.getObject(%i);
      
      // Print some information about the object in the group
      echo(%obj.getId() SPC %obj.getClassName());
   }
}

Start up our game under the My Projects directory and load the Empty Terrain level. Open the console using the tilde (~) key and enter the following at the bottom of the screen:

iterateSimGroup2();

In the console we will see the following output:

==>iterateSimGroup2();
4275 LevelInfo
4276 ScatterSky
4277 TerrainBlock
4278 SimGroup

The results end up being the same as when we used the foreach() function. There is no real advantage of using this method over the foreach() method. We do often see this pattern in a number of stock Torque 3D scripts that were written prior to foreach() being added to the TorqueScript language. We will also see this pattern in a lot of game developers' script code just because they are not aware of the newer foreach() function (now you are one of the special ones that do know!).

See also

  • Getting a random object from a SimSet or SimGroup collection

  • Finding an object in a SimSet or SimGroup collection using its internal name

  • Executing a method on a SimSet or SimGroup collection