Game Development Community

Back to Basics: SimGroup vs SimSet

by Dreamer · in Torque Game Engine · 11/05/2005 (1:55 am) · 28 replies

I'm going to start a series of threads on stuff I should know but can't remember anymore, called back to basics, this way folks can have a quickly searchable series of topics, for instances, when they have forgotten more about a topic then they remember, like I seem to be running into more and more these days.

My first topic (and yes I'm inviting full comments and opinions), is SimGroup vs SimSet.

Recently I purchased RTS and while going through the code I noticed something that at first I thought was quite odd.

// Create simset to track selection
   %this.selection = new SimSet();
   
// Create simset to track buildings
   %this.buildings = new SimGroup();

This seemed to me especially peculiar, since first of all, my instincts would have been to use an array for both types and secondly, shouldn't they both be the same?

Turns out after a bit of digging the answer is a resounding NO!

A SimGroup is a much more restrictive form of SimSet, in that you can only have a single object type in a group, for instance buildings belong in the buildings group. So in this case at least, since any number and type of objects can be selected at anyone time, we want to place all of our selected objects into a Simset.

This updated information is from Stephen Zepp an employee of GarageGames.
Quote:
There is no other reason to select a SimGroup over a SimSet, and in fact arbitrarily using a SimGroup over a SimSet is probably a bad idea until you understand the complexities. There is are no memory/performance improvements in a SimGroup over a SimSet (it's the same underlying code, just that SimGroup implements the restraint that a SimObject can be a member of one and only one SimGroup.


That would mean that since buildings all belong to one group at a time, we want to place them into a SimGroup. However Stephen further goes on to say, that when he made his mod to RTS the buildings in a SimGroup, were the first thing to go, replaced instead with a SimSet.

Eitherway, once created SimSets and SimGroups are handy ways around the inherent limitations of Array handling in Torque. Array limitations are the subject of my next back to basics discussion topic.

Feel free to leave input.
Page«First 1 2 Next»
#21
11/16/2006 (2:30 pm)
Gina, are these actual Object instances your dealing with?
#22
11/16/2006 (2:43 pm)
Err... I believe so. If I understand your question correctly.

Here's an example of how things work:

From groupTreasure.cs (The script that handles all my container creation)
// Define our Treasure Type Holder //
if (isObject(Treasure))
{
   Treasure.clear();
   Treasure.delete();   
}
new SimSet(Treasure){};


// Define the Gems treasure type holder //
if (isObject(Gems))
{
   Gems.clear();
   Gems.delete();   
}
new SimSet(Gems){};
...
// Add all treasure type SimGroups to the Treasure SimGroup //
Treasure.add(Gems);
...
// Define the Thief's pouch
 if (isObject(thiefPouch))
{
   thiefPouch.clear();
   thiefPouch.delete();   
}
new SimSet(thiefPouch){};

From treasure.cs (Where I take an item from the SimSet, copy it, and add it to the scenegraph)
error("Creating a random piece of treasure--------------------");
         if ($treasureCount < %maxTreasure)
         {
      
            //Grab a random treasure object out of the SimGroup and make it invisible
            %object = grabTreasure();
            %object.setVisible(0);
      
            // Create a container for the new object we'll make by copying the
            // original object
            %showObject = new t2dStaticSprite()
            {
               superclass = %object.superclass;
               myName = %object.getName();
            };
      
            // Add the object and the container to the scene, 
            // then copy the gem into the container
            %scenegraph.addToScene(%object);
            %scenegraph.addToScene(%showObject);
            %showObject.copy(%object,true);
      
            // Fire Creation Methods by object type

From enemies.cs (Where I create the thief and handle his collisions)
function EnemyObj::onCollision(%srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts)
// Enemies can interact with treasure in various ways
{
   %scenegraph = sceneWindow2D.getSceneGraph();
   echo(%srcObj SPC "is colliding with" SPC %dstObj SPC ".");
   // If the enemy is a thief, check to see if the collision occurred with a gem
   //
   // Did a gem fall on the thief?
   if(%srcObj.superclass $= "GemObj")
   {
      // Check the thief animation to see what grab we need to play
      %dstAnimation = %dstObj.getAnimation();
      if(%dstAnimation $= "eastThiefWalk")
      {
         %dstObj.playAnimation(eastThiefSteal, true);
      }else if(%dstAnimation $= "westThiefWalk")
      {
         %dstObj.playAnimation(westThiefSteal, true);
      }
      
      // Add the gem to the thief's pouch and check the contents
      thiefPouch.add(%srcObj);
      echo("Let's sort the Thief's Pouch!");
      thiefPouch.listObjects();
      
      // Remove the gem from the game screen
      %scenegraph.removeFromScene(%srcObj);
      
      // Add the gem to the thief's treasure count
      $treasureHeldByThief += 1;
   }


So, essentially what happens is I create the containers in groupTreasure.cs. Then I draw a random item out of a random container, copy it on the scenegraph, and set it loose in treasure.cs. Then, it hits the ground, the thief is spawned (not shown) and when he collides with the treasure he picks it up in enemies.cs. It should go into his "pouch" (SimSet(thiefPouch)) and be removed from the scenegraph at that point.

What happens is that he picks up the treasure, but as soon as the class of the treasure changes, it either doesn't list all the items, or it crashes TGB. It's doing this even though I've changed everything from SimGroup to SimSet now.
#23
11/16/2006 (2:49 pm)
I'm not sure but this looks wrong to me
new SimSet(Treasure){};

If I'm not mistaken the correct syntax here is
Treasure = new SimSet();

Same with all the others.
Are you sure you don't have any errors in your console.log?
#24
11/16/2006 (2:56 pm)
I'm positive. And everything has been working fine since I created the original groups. Now, understand, my original groups were SimGroups, so the syntax was :

new SimGroup(Treasure){};

and everything was working fine that way till I added the pouch in and started trying to save items to it. Once I read this thread, I thought the problem was "different object types" and I changed everything to SimSets to forestall this.

There are no errors in the console. It just gets to the point where it's listing the contents of the pouch:
thiefPouch.listObjects(); for the third or fourth time (when the object types switch) and it either drops different types or it crashes. For example, if the thief picks up two gems, when the list comes up it will say:

Let's sort the Thief's Pouch!
   3036: t2dStaticSprite 
   3038: t2dStaticSprite

but then if the thief picks up a household item, the list may say:
Let's sort the Thief's Pouch!
   3038: t2dStaticSprite 
   3041: t2dStaticSprite

It may erase both gems (3036 and 3038) or it may only erase one. (3041 is a household item.) Or it may crash entirely and this is what the last few lines of the log look like:

3039 is colliding with 3046 .
Let's sort the Thief's Pouch!
   3037: t2dStaticSprite

That's it. Nothing else. It's got to be an issue in the SimSets/Groups, because it's only happening when the object type switches from one superclass to another.
#25
11/16/2006 (3:37 pm)
Hey Gina, why don't you hop on irc.mydreamrpg.com and we can talk and try to figure this out, I still don't have enough info to be of much help.

Regards,
Dreamer
#26
11/16/2006 (3:54 pm)
Can't get onto the server for one reason or another. :(
#27
11/16/2006 (7:07 pm)
Okay, well Dreamer had an idea that I could fix my problem by fixing the source, but it didn't work. The crashes stopped happening, but everything else in the game (that was working previously) stopped working.

Does anyone have any other ideas? Please??
#28
11/16/2006 (7:27 pm)
@Gina, all that "fix" actually did was show us what line of source is causing the crashing problem.
It was meant as a test not a fix :)
Anyways now I know the issue, come into mydreamrpg IRC tommorow and we can fix it right.
Regards,
Dreamer
Page«First 1 2 Next»