Game Development Community

Creating triggers in script

by Brett Fattori · in Torque Game Engine · 07/16/2003 (10:36 pm) · 7 replies

I'm trying to address an issue with creating triggers within script. But when I go to "view" the trigger in the world editor, I get a crash because the don't seem to "really exist". Here's my code snip, anyone with an idea let me know:

datablock TriggerData(targetRingTrigger)
{
   category = "Misc";
   tickPeriodMS = 100;
};

// When targets are added, they will begin the rotation
// animation thread
function targetRing::onAdd(%this,%obj)
{
   // %obj is the object being added to the world
   // with %this datablock.  Start up a thread on the
   // new object.  Since the wave sequence is cyclic
   // this animation will run continously.
   %obj.playThread(0,"rotate");
   
   // Create a trigger
   %ringTrigger = new Trigger(ringTrig) {
      dataBlock = targetRingTrigger;
   };
   
   MissionGroup.cleanup(%ringTrigger);
   
   // Orient at the center of the object
   %ringTrigger.setTransform(%this.getTransform());
   
   // Scale to cover the ring (this is a guess)
   %ringTrigger.setScale("3 3 3");

   // Create a SimGroup
   %grp = new SimGroup(ringTriggerGroup) {
   };

   MissionGroup.cleanup(%grp);

   // Add the trigger and ring to the SimGroup
   %grp.add(%this);
   %grp.add(%ringTrigger);

   // Tell the ring what the group is
   %this.ringGroup = %grp;
}

PS: targetRing is a StaticShape object, if that helps at all.

- Brett

#1
07/16/2003 (11:53 pm)
I don't remember how I did it, but you got to add your trigger to the missionGroup after you create it. If you don't I remember it crashed when opening the editor.
Hope that helps a bit at least...
#2
07/17/2003 (12:14 am)
MissionGroup.cleanup(%ringTrigger);

I think should be:

MissionGroup.[b]add[/b](%ringTrigger);

Similarly for MissionGroup.cleanup(%grp).
#3
07/17/2003 (6:52 am)
I'll give those things a shot when I get home tonight.. This is one of those things that just stumps me. It seems so simple, but the script docos aren't too hot. And the tutes are a little lacking. I wonder if Ron Yacketta is still working on the script docos?

- Brett
#4
07/17/2003 (8:25 am)
Well, there's the sample fps demo app or RW ... or even Tribes2 ... reading other's code is the best way to figure things out.
#5
07/17/2003 (7:15 pm)
Another thing to note, you have to define the polyhedron (there isn't a default and maybe there should be) otherwise it crashes when you try to inspect it.

// Notice this is in the trigger and not
   // the datablock
   %ringTrigger = new Trigger(ringTrig) {
      dataBlock = "targetRingTrigger";
      polyhedron = "0 0 0 1 0 0 0 -1 0 0 0 1";
   };

The polyhedron is a point and 3 vectors that specify the length of each side.

- Brett
#6
07/18/2003 (10:01 am)
Ahhh that's true too... sorry I didn't remember that one... it's been sometime... but I guess you got it to work eh?
#7
07/21/2003 (6:17 pm)
I should update what I had, because it makes more sense when performed properly. There are basically two things that must happen for you to spawn an object and trigger, and have them function as one (both receive triggering events).

First off, you have to create the objects you want to add to a group, then create the group and add the objects:

// Create a visible object
   %object = new StaticShape(myObject) {
      dataBlock = "theDatablock";
   };
   
   // Add to the missiongroup
   // for proper cleanup
   MissionGroup.add(%object);

   // Create a trigger
   %trigger = new Trigger(myTrigger) {
      dataBlock = "triggerDatablock";
      polyhedron = "0 0 0 1 0 0 0 -1 0 0 0 1";
   };

   // Again, for cleanup
   MissionGroup.add(%trigger);

  // Orient at the center of the object
  %trigger.setTransform(%object.getTransform());

  // Scale to cover the ring (this is a guess)
  %trigger.setScale("5 5 5");

  // Create a SimGroup
  %grp = new SimGroup(triggerGroup) {
  };

  // Add the group to be cleaned up
  MissionGroup.add(%grp);

  // Add the trigger and object to the SimGroup
  %grp.add(%object);
  %grp.add(%trigger);

  // Tell the object what the group is
  // This makes it easier to schedule a delete()
  // after triggering the object.  You can
  // Then make the objects all-inclusive, in that
  // they can get rid of themselves.
  %object.triggerGroup = %grp;

That's pretty much it... You could of course use the name of the SimGroup to eliminate it, but you'd have to build a list for it. There's still the issue that the polyhedron for the trigger is defined as a point, and the length of the 3 sides. It can't really be placed at the center transform of the object, otherwise it doesn't bound it properly.

- Brett