onReachNode callback for TGB t2dPath
by Tim Newell · 07/11/2006 (6:57 pm) · 3 comments
This is a code resource on how to add script callbacks when an object reaches a node following a t2dPath in Torque Game Builder Pro 1.1.0. I started using the t2dPath in TGB and realized that I needed to change the direction my enemy was facing when he reached the end of the path and goes the oposite direction. So this resource will walk you through how to add a callback so that you can make some script magic happen when the object reaches a node.
Note: This does require the C++ code to add.
The only two files you will need to modify are t2dPath.h and t2dPath.cc and both are in T2D folder in the source directory.
First we will start with the t2dPath.h modifications.
Around line 268 after the function declaration for isValid Node you want to add a function declaration for our function to turn the callbacks on and off.
Next we need to define mNodeCallback. Go around line 305 and after Vector mNodes; add:
Now for the modifications to t2dPath.cc
Around line 111 inside the t2dPath constructor after mMountOffset = 0; add:
Note: This is set to true so that callbacks are on by default. then you can use the script function defined below to turn off callbacks for a given path. If you would like it to be off be default then it should be set to false.
Around line 726 inside of the if (object.mDestinationNode == end) code block add:
The above allows us to execute a callback and pass the "yes its a end node" boolean value. Next we need to call the "no its not a end node" callback so around line 737 inside of the else code block add:
Then finally after the consolemethod for getLoops we are going to add our consolemethod for turning the callback on and off so around line 966 add:
And that is all thats required in the C++ code.
Now in script you have two new functions at your access.
Adding the following code to anywhere in your project script files (I just created a new file inside of gameScripts dir but you can also put it inside game.cs)
%this points to the t2dPath Object, %node is the number of the node your at, %object is the object that arrived at the node, and endNode is true if its the end node and false otherwise.
Also t2dPath objects can now call enableCallback(boolean) So if you have a t2dPath object called myPath you would simply just do the following to turn callbacks on and off:
That is all there is to the tutorials. If you want to make sure its working correctly just add to your onReachNode in script an echo statement that prints the %node and %endNode to see how it works.
-Tim Newell
MaxGaming Technologies
Note: This does require the C++ code to add.
The only two files you will need to modify are t2dPath.h and t2dPath.cc and both are in T2D folder in the source directory.
First we will start with the t2dPath.h modifications.
Around line 268 after the function declaration for isValid Node you want to add a function declaration for our function to turn the callbacks on and off.
inline void t2dPath::setNodeCallback(bool value) {
mNodeCallback = value;
};Next we need to define mNodeCallback. Go around line 305 and after Vector
//boolean variable to turn callbacks on and off bool mNodeCallback;
Now for the modifications to t2dPath.cc
Around line 111 inside the t2dPath constructor after mMountOffset = 0; add:
Note: This is set to true so that callbacks are on by default. then you can use the script function defined below to turn off callbacks for a given path. If you would like it to be off be default then it should be set to false.
mNodeCallback = true;
Around line 726 inside of the if (object.mDestinationNode == end) code block add:
if (mNodeCallback) {
//make callback for end node
Con::executef( this, 4, "onReachNode", Con::getIntArg(object.mSourceNode), Con::getIntArg(object.mObject->getId()), "1");
}The above allows us to execute a callback and pass the "yes its a end node" boolean value. Next we need to call the "no its not a end node" callback so around line 737 inside of the else code block add:
if (mNodeCallback) {
//make callback for regular node
Con::executef( this, 4, "onReachNode", Con::getIntArg(object.mSourceNode) , Con::getIntArg(object.mObject->getId()), "0");
}Then finally after the consolemethod for getLoops we are going to add our consolemethod for turning the callback on and off so around line 966 add:
ConsoleMethod(t2dPath, enableCallback, void, 3, 3, "(boolean)")
{
object->setNodeCallback(dAtob(argv[2]));
}And that is all thats required in the C++ code.
Now in script you have two new functions at your access.
Adding the following code to anywhere in your project script files (I just created a new file inside of gameScripts dir but you can also put it inside game.cs)
function t2dPath::onReachNode(%this, %node, %object, %endNode) {
}%this points to the t2dPath Object, %node is the number of the node your at, %object is the object that arrived at the node, and endNode is true if its the end node and false otherwise.
Also t2dPath objects can now call enableCallback(boolean) So if you have a t2dPath object called myPath you would simply just do the following to turn callbacks on and off:
//This would turn callbacks on myPath.enableCallback(true); //This would turn callbacks off myPath.enableCallback(false);
That is all there is to the tutorials. If you want to make sure its working correctly just add to your onReachNode in script an echo statement that prints the %node and %endNode to see how it works.
-Tim Newell
MaxGaming Technologies
About the author
#2
The only other thing I see needed is being able to access the nodes from script, for when you want to use a path object to have a nice system for linked path nodes, but don't want to mount your object directly to the path.
03/07/2007 (7:42 pm)
Looks great! Thanks a lot for posting this resource! It's a well-formatted walkthrough.The only other thing I see needed is being able to access the nodes from script, for when you want to use a path object to have a nice system for linked path nodes, but don't want to mount your object directly to the path.
#3
03/05/2010 (4:02 pm)
These modifications wont work for me. The enableCallback $myPath.enableCallback(true);fails as an unknown command.
function bgClass::onMouseUp(%this, %modifier, %worldPosition, %mouseClicks)
{
echo("Number of nodes: " , $myPath.getNodeCount());
//echo($myPath);
$myPath.enableCallback(true);
$myPath.attachObject($goody, 5, 1, 0, $myPath.getNodeCount(), "WRAP", 0, false);
$myPath.setPathType("linear");
$myPath.setMoveForward($goody, true);
}
function t2dPath::onReachNode(%this, %node, %object, %endNode) {
echo("reached a node!", %node, %endnode);
}
Torque Owner Oliver Rendelmann - DerR