Game Development Community

Trigger - onEnter() is called after objects exit (FIXED)

by Josiah Hartzell · in Torque Game Builder · 09/22/2011 (6:04 pm) · 4 replies

Hello. This problem is very angering.

I can create any new trigger object and give it my simple behavior, create a new physics object and apply a force to send it toward the trigger, which should call this function when an object enters it:

function ForceFieldBehavior::onEnter(%this, %object)
{
	%object.setConstantForce(0, 1000, false);
}

It's just a simple test for a force field.

The angering problem is that it doesn't get called when the objects enter it, but rather when they *exit* the trigger. It makes no sense and it shouldn't happen. What could I possibly be doing wrong? If I change the function name to onExit() instead, it works in exactly the same way as it does with onEnter().

Moreover, onStay() also has the same effect as onExit().

So, simply put, all three functions actually work the same: they work as onExit().


- Josiah

#1
09/22/2011 (7:07 pm)
Wow...I really don't know what to tell you here.
#2
09/22/2011 (7:31 pm)
I've found something strange now. If I put this instead of applying forces to the object, it works as soon as the object enters the trigger, as desired:

function ForceFieldBehavior::onEnter(%this, %object)
{
	setRandomSeed(getSimTime()+%object.getPositionX()+%object.getPositionY());
	%object.setPositionX(%object.getPositionX+(getRandom(%this.MinMoveAmount, %this.MinMoveAmount+200)));
	%object.setPositionY(getRandom(%this.MinHeight, %this.MaxHeight));
}


Now I'm wondering, why can't I apply forces to an object when it's inside a trigger until it exits the trigger?
#3
09/22/2011 (7:52 pm)
You might wish to place echo statements at the beginning of each callback to see exactly what the object is doing and when. Also, could we see the declaration/fields of your trigger and your object?
#4
09/22/2011 (8:01 pm)
Hi Justin.

I tried echo statements (I'm a fan of print-debugging), but I can't find any console that gets printed to besides the GUI console widget, and that's too huge to be useful and is usually very buggy.

It's really nothing specific to the object or the trigger. I can create a new trigger and a new object from scratch and add that behavior to the trigger and it has the same effect.

Here's the code to the trigger behavior:

if(!isObject(ForceFieldBehavior))
{
	%template = new BehaviorTemplate(ForceFieldBehavior);

	%template.friendlyName = "Force Field Behavior";
	%template.behaviorType = "Physics Behaviors";
	%template.description  = "Applies a force to each object within the field";
}

function ForceFieldBehavior::onEnter(%this, %object)
{
	%object.setConstantForce(0, 1000, false);
}


EDIT:

I fixed it with this code below:

function ForceFieldBehavior::onStay(%this, %object)
{
	if(%object.class $= "PlayerShip")
		return;
		
	%object.setImpulseForcePolar(%this.owner.getMountedParent().getRotation(), 1);
}

I still don't know why setConstantForce() doesn't work inside the trigger functions, but setImpulseForcePolar() does, and that's what I really wanted anyway. The first was just a test but still frustrating. I do believe my problem was a Torque bug though.

Thanks for your replies.

- Josiah