Game Development Community

T2dTrigger onLeave problem..

by Backman · in Torque Game Builder · 12/09/2006 (11:34 am) · 4 replies

I've got this big circular trigger in my game which seems to not work properly. This has caused severe headache as I'm trying to fix it in other ways but the main problem is that the onLeave function seems to get called without any objects leaving?..

My code is set up like this, target = -1 means there's no target at the moment. It should not swap targets until the target is dead or outside the range.. :

function TurretTrigger::onEnter(%this, %object)
{
%parent = %this.getMountedParent();
if(%parent.target == -1)
{
%parent.target = %object;
%parent.attackTarget();
}
}

function TurretTrigger::onLeave(%this, %object)
{
%parent = %this.getMountedParent();

if(%object==%parent.target)
{
%parent.target = -1;
}
}

The parent is a turret. In the onLeave function it checks if the leaving object is same as current target, and if so, sets the target to -1 so it can register the next entering object as the target.

I'm echoing both %object and %parent.target in onLeave and it always returns the same in both variables?

I'm looking at this with the debugging collision boxes on and as soon as more than one object has entered the area the onLeave function gets called? Not even that seems to be consistent with the number of objects entering, almost as if it's not checking each frame.

I can't figure this one out and haven't found much regarding this.. kinda feels like I've been stupid somewhere but also need to see that there's not any 'hidden features' of the t2dtriggers, such as a need for two seperate triggers for this stuff, or it can only handle one object or something.

Really appreciate any help, thanks :)

#1
12/09/2006 (3:42 pm)
Is the problem that the parent object (I'm assuming it's the actual turret object) is being acquired as a target? That would explain why the echoes were the same.

So perhaps you just need to make sure onEnter that %object isn't the %parent before assigning the target.

Hope that helps a little!
#2
12/10/2006 (4:53 am)
Thanks for the input Tom, but I don't think that's the problem.. the turret has its group set like this:

%this.setGraphGroup($T1_WeaponsGroup);

and the trigger is set this way:

%this.setCollisionGroups($T2_PlayerGroup);

So unless I'm using the wrong script commands, that should work..

The echo reports different id's as more objects enter.. but it reports the same object leaving as entering (whenever an object enters), if that makes sense. I've also checked classes which say it's looking at the right class at least. :/

To make it clear, this is what happens:

- object enters (works ok)
- target is set to object (works ok)
- new object enters, but onLeave wrongly gets called and says the current target is the same as the onLeave %object and therefore sets target to -1.
- it then finds that new object that entered and says ok, target this since target is -1 (no current target) which I guess works fine.

I'm really confused by this, I'm going to leave this one to see if you guys have any more input, then I might just rewrite the whole thing... :(
#3
12/10/2006 (5:30 am)
I could just be reading this wrong, but it seems (based on the graph groups call) that your trigger is sending collision, rather than receiving. Each active sender can only register a collision with one receiver per scene update.

If your trigger is only sending, a second object entering the trigger will result in either:
a) no change, new object doesn't trigger an onEnter callback -or-
b) new object fires an onEnter callback and other object fires an onLeave

Switch them around so your trigger is receiving and see if that fixes your problem.
#4
12/12/2006 (5:59 am)
Hey that was it! Thanks a lot, saved me from going totally insane.. Hadn't heard about that at all actually.

Again, big thanks!