Question about distance and calling a function with schedule
by Andrea Farid Marsili · in Torque Game Builder · 06/05/2011 (4:23 am) · 3 replies
How can I get the distance between two point and how can I call a behavior function (behaviorName::function) using schedule?
#2
Note that the function name is in quotes without the parentheses. you can also add arguments after the function like so:
Now, if you are trying to schedule the function from outside the methods of the behavior, you'd first have to assign the the behavior object to some sort of global variable, which doesn't work too well if you have the behavior assigned to multiple game objects. Anyhow, here's how you'd do that:
I'm not sure what the difference between the two are, to be honest, I'm still a bit new myself, but one of the two should work for ya.
Hope this helps!
-Adam
06/05/2011 (3:15 pm)
For the schedule, it kind of depends on where you are calling it. If you are calling it from inside a method, you should have passed the object in a %this variable. In that case it would look like:%this.schedule(%time, "function");
Note that the function name is in quotes without the parentheses. you can also add arguments after the function like so:
%this.schedule(%time, "function", %arg1, %arg2);
Now, if you are trying to schedule the function from outside the methods of the behavior, you'd first have to assign the the behavior object to some sort of global variable, which doesn't work too well if you have the behavior assigned to multiple game objects. Anyhow, here's how you'd do that:
//Somewhere in the onBehaviorAdd() method $behaviorName = %this; //Wherever you schedule it use one or the other: schedule(%time, $behaviorName, "function"); $behaviorName.schedule(%time, "function");
I'm not sure what the difference between the two are, to be honest, I'm still a bit new myself, but one of the two should work for ya.
Hope this helps!
-Adam
#3
Here my code, it work but only without schedule.
06/06/2011 (9:36 am)
@Adam: I tried but cannot manage to call a behavior function with schedule.Here my code, it work but only without schedule.
if(!isObject(PuzzleLockBehavior)){
%template = new BehaviorTemplate(PuzzleLockBehavior);
%template.friendlyName = "PuzzleLockBehavior";
%template.behaviorType = "Puzzle Behavior";
%template.description = "Set the where you want to lock your object";
%template.addBehaviorField(targetX, "X position you want to lock the object", float, 0);
%template.addBehaviorField(targetY, "Y position you want to lock the object", float, 0);
%template.addBehaviorField(distanceToCheck, "The distance to your point", float, 0);
%template.addBehaviorField(speed, "Speed for moveTo point function", float, 0);
}
function PuzzleLockBehavior::onBehaviorAdd(%this){
}
function PuzzleLockBehavior::onMouseUp(%this, %modifier, %worldPosition, %clicks){
%this.distance = VectorDist(%this.owner.getPositionX() - %this.targetX, %this.owner.getPositionY() - %this.targetY);
PuzzleLockBehavior::checkDistance(%this);
echo("Distance: " @%this.distance);
}
function PuzzleLockBehavior::checkDistance(%this){
echo("Distance in check: " @%this.distance);
if(%this.distance < %this.distanceToCheck){
%this.owner.moveTo(%this.targetX, %this.targetY, %this.speed);
%this.owner.setUseMouseEvents(false);
echo("Moved&Locked");
}
}
Torque 3D Owner Matt Huston
Atomic Banzai Games