Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Torque 3D Game Development Cookbook

You're reading from   Torque 3D Game Development Cookbook Over 80 practical recipes and hidden gems for getting the most out of the Torque 3D game engine

Arrow left icon
Product type Paperback
Published in Jan 2013
Publisher Packt
ISBN-13 9781849693547
Length 380 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
DAVID WYAND DAVID WYAND
Author Profile Icon DAVID WYAND
DAVID WYAND
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Torque 3D Game Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. TorqueScript: The Only Script You Need to Know 2. Working with Your Editors FREE CHAPTER 3. Graphical User Interface 4. Camera and Mouse Controls 5. Your Graphics Evolved 6. Make That Sound Happen 7. Game Objects 8. Multiplayer Servers 9. Importance of Networking 10. Miscellaneous Gameplay Features 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

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image