Unigine.NodeTrigger Class
Inherits: | Node |
A trigger node is a zero-sized node that has no visual representation and fires callbacks when:
- It is enabled/disabled (the enabled callback function is called).
- Its transformation is changed (the position callback function is called).
For example, to detect if some node has been enabled (for example, a world clutter node that renders nodes only around the camera has enabled it), the trigger node is added as a child to this node and fires the corresponding callback.
Creating a Trigger Node
To create a trigger node, simply call the NodeTrigger constructor and then add the node as a child to another node, for which callbacks should be fired.
using Unigine;
namespace UnigineApp
{
class AppWorldLogic : WorldLogic
{
private NodeTrigger trigger;
private ObjectMeshStatic obj;
public override int init()
{
// create a mesh
Mesh mesh = new Mesh();
mesh.addBoxSurface("box_0", new vec3(1.0f));
// create a node (e.g. an instance of the ObjectMeshStatic class)
obj = new ObjectMeshStatic(mesh);
obj.setMaterial("mesh_base", "*");
obj.setMaterialParameter("albedo_color", new vec4(1.0f, 0.0f, 0.0f, 1.0f), 0);
obj.release();
// create a trigger node
trigger = new NodeTrigger();
trigger.release();
// add it as a child to the static mesh
obj.addWorldChild(trigger.getNode());
// add nodes to UnigineEditor
Editor.get().addNode(obj.getNode());
return 1;
}
public override int shutdown()
{
// clear pointers
trigger.clearPtr();
obj.clearPtr();
return 1;
}
}
}
Editing a Trigger Node
Editing a trigger node includes implementing and specifying the enabled and postion callbacks that are fired on enabling or positioning the trigger node correspondingly.
The callback function must receive at least 1 argument of the NodeTrigger type. In addition, it can also take another 2 arguments of any type.
The callback functions can be set by callback pointers or names:
- setEnabledCallback() and setPositionCallback() receive pointers to the callback functions.
- setEnabledCallbackName() and setPositionCallbackName() receive names of the callback functions implemented in the world script (on the UnigineScript side). In this case, the callback functions can receive no arguments or 1 argument of the Node or NodeTrigger type.
These methods allow for setting callbacks with 0 or 1 argument only. To set callbacks with additional arguments, use setEnabledCallback()/setPositionCallback().
Setting the callback functions by pointers:
// AppWorldLogic.cs
using Unigine;
#if UNIGINE_DOUBLE
using Vec3 = Unigine.dvec3;
using Vec4 = Unigine.dvec4;
using Mat4 = Unigine.dmat4;
#else
using Vec3 = Unigine.vec3;
using Vec4 = Unigine.vec4;
using Mat4 = Unigine.mat4;
#endif
namespace UnigineApp
{
class AppWorldLogic : WorldLogic
{
private NodeTrigger trigger;
private ObjectMeshStatic obj;
// the position callback
private static void position_callback(NodeTrigger trigger)
{
Log.message("Object position has been changed. New position is: {0}\n", trigger.getWorldPosition().ToString());
}
// the enabled callback
private static void enabled_callback(NodeTrigger trigger)
{
Log.message("The enabled flag is {0}\n", trigger.isEnabled());
}
public override int init()
{
// create a mesh
Mesh mesh = new Mesh();
mesh.addBoxSurface("box_0", new vec3(1.0f));
// create a node (e.g. an instance of the ObjectMeshStatic class)
obj = new ObjectMeshStatic(mesh);
obj.setMaterial("mesh_base", "*");
obj.setMaterialParameter("albedo_color", new vec4(1.0f, 0.0f, 0.0f, 1.0f), 0);
obj.release();
// create a trigger node
trigger = new NodeTrigger();
trigger.release();
// add it as a child to the static mesh
obj.addWorldChild(trigger.getNode());
// add nodes to UnigineEditor
Editor.get().addNode(obj.getNode());
// set the enabled and position callbacks
trigger.setEnabledCallback(enabled_callback);
trigger.setPositionCallback(position_callback);
return 1;
}
public override int update()
{
float time = Game.get().getTime();
Vec3 pos = new Vec3(MathLib.sin(time) * 2.0f, MathLib.cos(time) * 2.0f, 0.0f);
// change the enabled flag of the node
obj.setEnabled(pos.x > 0.0f || pos.y > 0.0f ? 1 : 0);
// change the node position
obj.setWorldPosition(pos);
return 1;
}
public override int shutdown()
{
// clear the pointers
trigger.clearPtr();
obj.clearPtr();
return 1;
}
}
}
Setting the callback functions by their names:
- Implement the callback functions in the world script (on the UnigineScript side).
- Set the callback functions for the trigger node by using their names on the C# side (AppWorldLogic.cs).
// unigine_project.cpp
#include <core/unigine.h>
// the enabled callback
void enabled_callback(Node trigger) {
log.message("Enabled flag is %d\n", trigger.isEnabled());
}
// the position callback
void position_callback(Node trigger) {
log.message("The node position is %s\n", typeinfo(trigger.getWorldPosition()));
}
int init() {
// some logic if required
return 1;
}
using Unigine;
#if UNIGINE_DOUBLE
using Vec3 = Unigine.dvec3;
using Vec4 = Unigine.dvec4;
using Mat4 = Unigine.dmat4;
#else
using Vec3 = Unigine.vec3;
using Vec4 = Unigine.vec4;
using Mat4 = Unigine.mat4;
#endif
namespace UnigineApp
{
class AppWorldLogic : WorldLogic
{
private NodeTrigger trigger;
public override int init()
{
// create a trigger node
trigger = new NodeTrigger();
// release script ownership
trigger.release();
// add nodes to UnigineEditor
Editor.get().addNode(trigger.getNode());
// set the enabled and position callbacks
trigger.setEnabledCallbackName("enabled_callback");
trigger.setPositionCallbackName("position_callback");
return 1;
}
public override int update()
{
// change the enabled flag and the trigger position to fire callbacks
float time = Game.get().getTime();
Vec3 pos = new Vec3(MathLib.sin(time) * 2.0f, MathLib.cos(time) * 2.0f, 0.0f);
trigger.setEnabled(pos.x > 0.0f || pos.y > 0.0f ? 1 : 0);
trigger.setWorldPosition(pos);
return 1;
}
public override int shutdown()
{
// clear the pointer
trigger.clearPtr();
return 1;
}
}
}
NodeTrigger Class
Members
static NodeTrigger()
Constructor. Creates a new trigger node.NodeTrigger cast(Node node)
Casts a NodeTrigger out of the Node instance.Arguments
- Node node - Node instance.
Return value
NodeTrigger instance.void setEnabledCallback(PositionCallback func)
Sets a pointer to a callback to be fired when the trigger node is enabled. The callback function must receive a NodeTrigger as its first argument. In addition, it can also take 2 arguments of any type.using Unigine;
namespace UnigineApp
{
class AppWorldLogic : WorldLogic
{
// implement the enabled callback
private static void enabled_callback(NodeTrigger trigger)
{
Log.message("The enabled flag is {0}\n", trigger.isEnabled());
}
public override int init()
{
// create a trigger node
NodeTrigger trigger = new NodeTrigger();
// set the enabled callback to be fired when the node is enabled/disabled
trigger.setEnabledCallback(enabled_callback);
return 1;
}
}
}
Arguments
- PositionCallback func - Pointer to the callback function.
void setEnabledCallbackName(string name)
Sets a callback function to be fired when the trigger node is enabled. The callback function must be implemented in the world script (on the UnigineScript side). The callback function can take no arguments, a Node or a NodeTrigger.// implement the enabled callback
void enabled_callback(Node node) {
log.message("The enabled flag is %d\n", node.isEnabled());
}
using Unigine;
namespace UnigineApp
{
class AppWorldLogic : WorldLogic
{
public override int init()
{
// create a trigger node
NodeTrigger trigger = new NodeTrigger();
// set the enabled callback to be fired when the node is enabled/disabled
trigger.setEnabledCallbackName("enabled_callback");
return 1;
}
}
}
Arguments
- string name - Name of the callback function implemented in the world script (UnigineScript side).
string getEnabledCallbackName()
Returns the name of callback function to be fired on enabling the trigger node. This callback function is set via setEnabledCallbackName().Return value
Name of the callback function implemented in the world script (UnigineScript side).void setPositionCallback(PositionCallback func)
Sets a pointer to a callback to be fired when the trigger node position has changed. The callback function must receive a NodeTrigger as its first argument. In addition, it can also take 2 arguments of any type.using Unigine;
namespace UnigineApp
{
class AppWorldLogic : WorldLogic
{
// implement the position callback
private static void position_callback(NodeTrigger trigger)
{
Log.message("A new position of the node is {0}\n", trigger.getWorldPosition().ToString());
}
public override int init()
{
// create a trigger node
NodeTrigger trigger = new NodeTrigger();
// set the position callback to be fired when the node position is changed
trigger.setPositionCallback(position_callback);
return 1;
}
}
}
Arguments
- PositionCallback func - Pointer to the callback function.
void setPositionCallbackName(string name)
Sets a callback function to be fired when the trigger node position has changed. The callback function must be implemented in the world script (on the UnigineScript side). The callback function can take no arguments, a Node or a NodeTrigger.// implement the position callback
void position_callback(Node node) {
log.message("A new position of the node is %s\n", typeinfo(node.getWorldPosition()));
}
using Unigine;
namespace UnigineApp
{
class AppWorldLogic : WorldLogic
{
public override int init()
{
// create a trigger node
NodeTrigger trigger = new NodeTrigger();
// set the position callback to be fired when the node position is changed
trigger.setPositionCallbackName("position_callback");
return 1;
}
}
}
Arguments
- string name - Name of the callback function implemented in the world script (UnigineScript side).