Game Development Community

Problem with Triggers - Mounted Trigger not used but onEnter callback is triggered. [Solved]

by Renee Reyneke · in Torque Game Builder · 10/08/2011 (11:06 am) · 1 replies

I create a trigger and mount it to a tree. When it is triggered the tree will be made translucent.

The problem is the trigger mounted to the tree is not used, although the onEnter callback is triggered, and I have no idea why. It looks as if 2 triggers are created but the second one is not mounted to the tree. I have TGB 1.7.5

Here is the code where I create the tree and trigger:

echo(" ****    Create tree and trigger");   

   %treeSprite2 = new t2dStaticSprite(tree2) {
      imageMap = "stuff_tree1ImageMap";
      frame = "0";
      useSourceRect = "0";
      sourceRect = "0 0 0 0";
      canSaveDynamicFields = "1";
      useMouseEvents = "1";
      CollisionActiveSend = "1";
      Position = "-20.854 -7.000";
      size = "11.000 15.000";
      Layer = "1";
      CollisionLayers = "4";  // only layer 2
         mountID = "7";
   }; 
   %sceneGraph = SceneWindow2D.getSceneGraph();
   %sceneGraph.addToScene(%treeSprite2);
   
 //--------  add trigger to make objects on layer 1 translucent when passing behind  
  %addTrigger = new t2dTrigger(transTrig) {
  //    canSaveDynamicFields = "1";
      Position = "-20.854 -7.000";
      size = "5.000 5.000";
      CollisionActiveSend = "0";
      CollisionActiveReceive = "1";
      CollisionPhysicsSend = "0";
      CollisionPhysicsReceive = "0";
      CollisionLayers = "-7";  // ignore layers 1 and 2 collision
      Layer = "1";
      MountOffset = "0.000 0.000";
      MountOwned = "1";
      MountInheritAttributes = "0";
         mountID = "6";
         mountToID = "7";
      _behavior0 = "TranslucentBehavior";
   };
   
   %sceneGraph = SceneWindow2D.getSceneGraph();
   %sceneGraph.addToScene(%addTrigger);
   
echo("       %treeSprite2 = " @ %treeSprite2);
echo("       %addTrigger = " @ %addTrigger);
     
echo(" ****    Mount trigger to tree");   
    %addTrigger.mount(%treeSprite2,0,0,0,true,true,false);

    %parent = %addTrigger.getMountedParent();
echo("     %parent of %addTrigger  = " @ %parent );

    %linkQty = %parent.getLinkCount();
echo("    links for %parent  = " @ %linkQty );

    %linkQtyT = %addTrigger.getLinkCount();
echo("    links  for %addTrigger  = " @ %linkQtyT );

    %EnterCallback = %addTrigger.getEnterCallback();
echo("    getEnterCallback for %addTrigger  = " @ %EnterCallback  );
       
echo(" ****    END Create tree and trigger ");


The following code is the onEnter callback, but the mounted trigger is not used:

function TranslucentBehavior::onEnter(%this, %object)
{ 
    echo(" * * * * TranslucentBehavior::onEnter - %this = " @ %this @ "/ %object = " @ %object);
      
    echo(" * * * *   %this.getname() = " @ %this.getname());  	
    echo(" * * * *   %object.getname() = " @ %object.getname()); 
 
   	
    %parent = %this.getMountedParent();
    %parent.setBlendAlpha(0.3);
    echo("       %parent  = " @ %parent );
    
//========== Added to check is original trigger  still exist ============
    echo(" ! ! ! ! !  Check if original trigger still exist - set %chqtrig = 1362"); 
    %chqtrig = "1362";
    echo("       %chqtrig.getname() = " @ %chqtrig.getname()); 
    
    %chqparent = %chqtrig.getMountedParent();
    echo("       %chqparent  = " @ %chqparent );
    echo("       %chqparent.getname() = " @ %chqparent.getname()); 

    %linkQty = %chqparent.getLinkCount();
    echo("    links for %chqparent  = " @ %linkQty );  

    %linkQtyT = %chqtrig.getLinkCount();
    echo("    links  for %chqtrig  = " @ %linkQtyT );
    
    echo("  ! ! ! ! !  end   %chqtrig ");
//=======================================================================
    
}

---------------------------------------------------------
Here is the console.log output:

**** Create tree and trigger
%treeSprite2 = 1361
%addTrigger = 1362
**** Mount trigger to tree
%parent of %addTrigger = 1361
links for %parent = 1
links for %addTrigger = 0
getEnterCallback for %addTrigger = 1
**** END Create tree and trigger
%addPlayer = 1364
%addSled = 1366
* * * * TranslucentBehavior::onEnter - %this = 1363/ %object = 1364
* * * * %this.getname() =
* * * * %object.getname() = player
game/behaviors/translucentBehavior.cs (18): Unknown command getMountedParent.
Object (1363) TranslucentBehavior -> BehaviorTemplate -> SimObject
game/behaviors/translucentBehavior.cs (19): Unable to find object: '' attempting to call function 'setBlendAlpha'
%parent =

! ! ! ! ! Check if original trigger still exist - set %chqtrig = 1362
%chqtrig.getname() = transTrig
%chqparent = 1361
%chqparent.getname() = tree2
links for %chqparent = 1
links for %chqtrig = 0
! ! ! ! ! end %chqtrig
------------------------------------------------------------

I have no idea where object 1363 comes from! It triggers the TranslucentBehavior::onEnter when the player runs behind the tree but it is not mounted to the tree and I can get no information about is ( did a %this.dump() ). I do not create any other triggers in the game either.

I have been looking at this code for 2 days and know I am missing something, but I cannot see it.


#1
10/09/2011 (10:36 am)
Ok I figured out what the problem is. This is to maybe help other newbies like me.

If you use a behaviour with a trigger and you need the parent of the trigger you have to use %this.owner. %this is the Behaviour Id. Once the penny dropped the solution was easy.