Game Development Community

Unhide simgroup function

by JeffH · in Torque 3D Professional · 05/17/2013 (12:53 pm) · 6 replies

Figured I'd share a quick script method that I made.

It un-hides all gamebase and shapebase objects within a simgroup. If there are simgroups, it will iterate through those too. Say if you wanted all of your items to re-appear within a mission instantly. Just call:

ServerGroup.unHideAllObjects();

Anyways, here's the code:

// Jeff: allows a sim group to set all 
// gamebase and shapebase objects to be visible
function SimGroup::unHideAllObjects(%this)
{
   %mask = $TypeMasks::GameBaseObjectType | $TypeMasks::ShapeBaseObjectType;
   for (%i = %this.getCount() - 1; %i > -1; %i --)
   {
      %obj = %this.getObject(%i);
      if (%obj.getClassName() $= "SimGroup")
         %obj.unHideAllObjects();
      else if (%obj.getType() & %mask)
      {
         if (%obj.isCloaked())
            %obj.setCloaked(false);
         %obj.startFade(0, 0, false);
         %obj.setHidden(false);
      }
   }
}

#1
05/18/2013 (1:37 am)
"if (%obj.getType() & %mask)"

confused.
what is "&" symbol?
and how and when %mask be true?


what will happen if i use only this:
"if(%mask)" ?
#2
05/18/2013 (8:32 am)
"&" is a bitwise "and" - the opposite of "|" ("or").
%mask = $TypeMasks::GameBaseObjectType | $TypeMasks::ShapeBaseObjectType;
uses the | symbol. This would be the opposite:
%mask = $TypeMasks::GameBaseObjectType & $TypeMasks::ShapeBaseObjectType;
#3
05/18/2013 (8:41 am)
& is an AND operation.

The AND operation is a bitwise operation. It checks the bit of one object to see if those same bits are active in the other Object.

All Mask in the engine are bitwise.

Several objects are marked as many types of object types.

For example:
StaticObjectType = BIT( 0 )
EnvironmentObjectType = BIT( 1 )
TerrainObjectType = BIT( 2 )
InteriorObjectType = BIT( 3 )
WaterObjectType = BIT( 4 )

These are the bits that mark an object as a given type.

If you have many different objects in a SIM Group and you want to only do something for a given type you can use the &, or the AND operator, to do a test to see if the object type is what we are looking for.

In the example above he defines the mask because he is looking for GameBase and ShapeBase objects. So when you check the mask with the AND operator if the type matches GameBase or a ShapeBase type, the if statement will be true.

#4
05/18/2013 (8:42 am)
I used a bitmask because I figured it would be slightly faster than using a string comparison for all the types.

@ashan %mask would always be true then, so it would do that to every object (and remember there are other objects such as interiorInstances and missionAreas that do not have those methods because they are not shapebase, thus causing console warnings.)

Edit: Ninja'D ;)

Question: should I even be checking gameBase? I am not sure if gamebase even has those methods of cloaked and hidden. I better check the gamebase class again...
#5
05/19/2013 (5:25 am)
thanks for those explanations.
mow clear.
#6
05/19/2013 (8:17 am)
A note on bit masks just in case anybody reads up on this later trying to figure them out. When you use the following statement you are only checking to see if getType() matches one, not all, of the bits or object types set in the mask:
%mask = $TypeMasks::GameBaseObjectType | $TypeMasks::ShapeBaseObjectType;
if(%obj.getType() & %mask)

Meaning if %obj.getType() has just $TypeMasks::GameBaseObjectType or $TypeMasks::ShapeBaseObjectType value then the statement above in the code block would still evaluate to true. It will of course still evaluate to true if both object types are set/returned from %obj.getType() call. The point is just one matching bit in the %mask will cause the if() statement to be true.

To make it so that %obj.getType() is required to have all the bits or object types set in %mask then do the following instead of which is just a minor change:

%mask = $TypeMasks::GameBaseObjectType | $TypeMasks::ShapeBaseObjectType;
if((%obj.getType() & %mask) == %mask)

Now that if() statement will only evaluate to true when %obj.getType() is has both $TypeMasks::GameBaseObjectType and $TypeMasks::ShapeBaseObjectType bits set.