Author Topic: Trigger Help  (Read 1563 times)

So I'm new to TorqueScript and I'm trying to make a trigger for something to happen, however whenever I stand on the brick it never does anything.

I've found this from searching to see if someone else has had the same problem:

Code: [Select]
function FxDTSBrick::createTrigger(%this, %data)
{
//credits to Space Guy for showing how to create triggers

%t = new Trigger()
{
datablock = %data;
polyhedron = "0 0 0 1 0 0 0 -1 0 0 0 1"; //this determines the shape of the trigger
};

missionCleanup.add(%t);

%boxMax = getWords(%this.getWorldBox(), 3, 5);
%boxMin = getWords(%this.getWorldBox(), 0, 2);
%boxDiff = vectorSub(%boxMax,%boxMin);
%boxDiff = vectorAdd(%boxDiff, "0 0 0.2");
%t.setScale(%boxDiff);
%posA = %this.getWorldBoxCenter();
%posB = %t.getWorldBoxCenter();
%posDiff = vectorSub(%posA, %posB);
%posDiff = vectorAdd(%posDiff, "0 0 0.1");
%t.setTransform(%posDiff);

%this.trigger = %t;
%t.brick = %this;

return %t;
}

... and I'm using this with the brick:

Code: [Select]
datablock fxDTSBrickData(TestBrickData : brick2x4fData)
{
category = "Test";
subCategory = "Bricks";

uiName = "Test Brick";
};

datablock TriggerData(TestBrickTriggerData)
{
tickPeriodMS = 150;
};

function TestBrickData::onPlant(%this, %obj)
{
%this.createTrigger(TestBrickTriggerData);
}

function TestBrickTriggerData::onEnterTrigger(%this, %trigger, %obj)
{
echo("Player on brick");
}

function TestBrickTriggerData::onLeaveTrigger(%this, %trigger, %obj)
{
echo("Player off brick");
}

function TestBrickTriggerData::onTickTrigger(%this, %trigger, %obj)
{
Parent::onTickTrigger(%this, %trigger);
}

I don't really see what's going wrong. I'm not sure if I'm using %brick.createTrigger(%data); wrong or not... Help?

Code: [Select]
function TestBrickData::onPlant(%this, %obj)
{
%this.createTrigger(TestBrickTriggerData);
}
It's not working because you're creating the trigger wrong. %this is the brick's datablock. %obj is the actual brick.
Code: [Select]
function TestBrickTriggerData::onTickTrigger(%this, %trigger, %obj)
{
Parent::onTickTrigger(%this, %trigger);
}
Side note: You don't need to parent this function. And if you're not going to do anything with the function, don't create it at all.

It's not working because you're creating the trigger wrong. %this is the brick's datablock. %obj is the actual brick.

Oh okay, I get it now. Thanks for the help

I'd recommend renaming that ::createTrigger function to something unique. I've seen lots of people using it and modifying it since I posted it.