Game Development Community

Running a script form the trigger object

by Orion the Hunter · in Torque Game Builder · 07/24/2012 (2:32 pm) · 16 replies

Hi,

I was wondering if I could run a script when the player enters a trigger. I hope this doesn't involve making a whole new object script for each one!


Thanks!

#1
07/24/2012 (3:56 pm)
If I understand what you are asking:
This is a mission trigger that I was fooling around with, it uses a Dynamic field on the specific trigger to complete the information loop. I believe you can do something similar with calling functions as well so that you don't have to make a separate trigger for each one.

--------------------------------
datablock TriggerData (Trigger2SwapMission)
{
tickPeriodMS = 100;
};

function Trigger2SwapMission::onEnterTrigger(%this,%trigger,%obj)
{

%client = %obj.client;
if(!%client)
{
// return if not a client
// we do not want any npc or other players walking into our trigger and // setting off the swap mission
return;
}
}

function Trigger2SwapMission::onTickTrigger(%this,%trigger,%obj)
{
// the enter key sets $interactive to true while down for my game, this is not necessary
if($interactive == true)
{
// Put in Dynamic Field called ZoneName on trigger object in-game such // as: ZoneName = "YourMissionName.MIS";
%ZoneName = %trigger.ZoneName;
//Another Dynamic field for the Spawn Point in mission
%SpawnPoint = %trigger.SpawnPoint;

echo("Zone client:" SPC %client SPC "to" SPC %ZoneName SPC "at" SPC %SpawnPoint);

schedule( 0, 0, loadMission, "levels/" @ %ZoneName,false,%SpawnPoint);
}
else
{
echo("No interactive objects?");
}
}
--------------------

I hope this leads you in the right direction.
#2
07/24/2012 (7:16 pm)
Thanks but what I was looking for was one trigger that allows you to edit the command from the scene builder so you could have multiple triggers, each with their own command.

Thanks for the reply and script which I am sure will be useful some time else.
#3
07/24/2012 (9:09 pm)
That would be great to implement. I was needing some pointers in this direction so as to start a more developer friendly version of Torque for my specific needs.
I will attempt to go through the core GUI functions, assuming this is where the mission editor's GUI's are and let you know if I find anything, but this is new territory for me. Hopefully someone else can offer some insight along the way.
#4
07/24/2012 (9:23 pm)
It's not very elegant, but you could use a single onEnter() callback that uses the name of the trigger to check a switch statement and execute a script function dependent on the trigger's name.

function genericTrigger::onEnter(%this, %obj)
{
   switch$(%this.getName())
   {
      case "triggerOne":
         functionOne();

      case "triggerTwo":
         functionTwo();
   }
}

Like I said, ugly but functional.
#5
07/27/2012 (7:52 am)
Thanks,

but again, what I was looking for was a single trigger that you edit the script line from the scene builder.
#6
07/27/2012 (5:56 pm)
Couldn't you add a behavior to store them?

So you'd add a "functionHolder" behavior to your trigger, which would ask for things:

enterFunction
leaveFunction

(parameters... whatever you need)...

Then you modify your trigger callback with:

if(%this.method !$= "")
call(%this.method, %this.methodparm);

(assuming the behavior sets these variables for its owner when the behavior is added)

Then you'd have a quick n' easy trigger-callback manipulator within the editor?

#7
07/30/2012 (8:30 am)
Yeah,
that kind of thing. :)
#8
08/24/2012 (2:35 pm)
Bumped

Anyone found anything?
#9
08/24/2012 (3:51 pm)
What you didn't like my solution? :)

Or you just need help in implementation? I can be more explicit if you need it, but likely not until next week or so when I get a minute.

#10
08/25/2012 (6:11 am)
No no, your solution is spot on. Do you think you could give me some tips and snippets on how to do so?
#11
08/27/2012 (9:58 am)
I'll write ye up some help on this later in the week, for sure.
#12
08/27/2012 (11:57 am)
Thanks!

Greatly appreciated.
#13
09/07/2012 (9:38 am)
Hi,

Don't mean to be impatient but have you worked up a script yet?

Thanks again
#14
09/07/2012 (6:04 pm)
Here ya go:

First make yourself up a new behavior.
Call it triggerExecute.cs or some such, and drop it in your behaviors folder.

if (!isObject(TriggerExecBehavior))
{
   %template = new BehaviorTemplate(TriggerExecBehavior);
   
   %template.friendlyName = "Trigger Function Execute";
   %template.behaviorType = "Triggers";
   %template.description  = "Executes a method when trigger is entered or left.";
   
   %template.addBehaviorField(entermethod, "The method to call on entering", string, "");
   %template.addBehaviorField(entermethodparm, "parms for method", string, "");
   %template.addBehaviorField(leavemethod, "The method to call on leaving", string, "");
   %template.addBehaviorField(leavemethodparm, "parms for method", string, "");
   
   %template.addBehaviorField(dontif, "methods won't be called if this is true", string, "");

}
function TriggerExecBehavior::onBehaviorAdd(%this)
{
	// set 'em up on the trigger itself for easy access
   %this.owner.entermethod = %this.entermethod;
   %this.owner.leavemethod = %this.leavemethod;
   %this.owner.entermethodparm = %this.entermethodparm;
   %this.owner.leavemethodparm = %this.leavemethodparm;
   %this.owner.dontif = %this.dontif;
      
}

Then you need to add some bits to your onEnter and onLeave callbacks, like so:

function t2dTrigger::onEnter(%this, %object)
{
	echo(%object.getname() @ " has entered the trigger.");
	
	if(%this.dontif !$= "" && eval("if(" @ %this.dontif @ ") { return 1; }else{ return 0;}"))
		return;
		
	if(%this.entermethod !$= "")
		call(%this.entermethod, %this.owner, %this.entermethodparm);
}
function t2dTrigger::onLeave(%this, %object)
{
	echo(%object.getname() @ " has left the trigger.");
	
	if(%this.dontif !$= "" && eval("if(" @ %this.dontif @ ") { return 1; }else{ return 0;}"))
		return;
		
	if(%this.leavemethod !$= "")
		call(%this.leavemethod, %this.owner, %this.leavemethodparm);
}

And you should be all set. In the editor you add the behavior to your trigger, and enter in your functions and parameters as you'd like 'em.

I use a "dontif" field for some extra useful final bit of logic... like dontif $paused || %this.dead || $playerState $= "invincible", etc.

When telling it what function to run in the editor, don't use ();, just drop the name and parameters in there like so:

enterMethod: damagePlayer
enterMethodparm: 5

Hope it helps!


#15
09/10/2012 (1:03 pm)
WOW! This is GREAT!

I really like this, thanks a lot!

I really, really hate to be picky, but there is one little error that happens. When I spawn, it runs the script as if I was in it for a second but then came out. Any suggestions on how to fix that?
#16
09/10/2012 (1:12 pm)
Never mind! I fixed it myself. I realized it was behaving badly because it was colliding with the ground. And so, I set it up so that it will only work with enemies like that if has the playerOnly thing checked off. I added this:

function TriggerExecBehavior::onAddToScene( %this )
{
    // Collision system
    if ( %this.PlayerOnly )
    {
        %this.Owner.setObjectType( "PlayerTrigger" );
        %this.Owner.setCollidesWith( "PlayerObject" );
    }
    else
    {
        %this.Owner.setObjectType( "ActorTrigger" );
        %this.Owner.setCollidesWith( "ActorObject" );
    }
}
to the bottom and I added this:

%template.addBehaviorField( PlayerOnly, "Damage the player, not other actors",       BOOL,  false );

to the top! Viola! Problem solved. Thanks for the awesome code!