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 bool 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.setMaterialParameterFloat4("albedo_color", new vec4(1.0f, 0.0f, 0.0f, 1.0f), 0);
obj.release();
// create a trigger node
trigger = new NodeTrigger();
// add it as a child to the static mesh
obj.addWorldChild(trigger);
return true;
}
}
}
Editing a Trigger Node#
Editing a trigger node includes implementing and specifying the enabled and position callbacks that are fired on enabling or positioning the trigger node correspondingly.
See Also#
- A C++ API sample <UnigineSDK>/source/samples/Api/Nodes/NodeTrigger
- A C# API sample located in the <UnigineSDK>/source/csharp/samples/Api/Nodes/NodeTrigger folder
NodeTrigger Class
Members
static NodeTrigger ( ) #
Constructor. Creates a new trigger node.IntPtr addEnabledCallback ( EnabledDelegate 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
void enabled_callback(NodeTrigger trigger)
{
Log.Message("The enabled flag is {0}\n", trigger.isEnabled());
}
NodeTrigger trigger;
public override bool Init()
{
// create a trigger node
trigger = new NodeTrigger();
// add the enabled callback to be fired when the node is enabled/disabled
trigger.addEnabledCallback(enabled_callback);
return 1;
}
}
}
Arguments
- EnabledDelegate func - Callback function with the following signature: void EnabledDelegate(NodeTrigger trigger)
Return value
ID of the last added enabled callback, if the callback was added successfully; otherwise, nullptr. This ID can be used to remove this callback when necessary.bool removeEnabledCallback ( IntPtr id ) #
Removes the specified callback from the list of enabled callbacks.Arguments
- IntPtr id - Enabled callback ID obtained when adding it.
Return value
True if the enabled callback with the given ID was removed successfully; otherwise false.void clearEnabledCallbacks ( ) #
Clears all added enabled callbacks.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.The method allows for setting a callback with 0 or 1 argument only. To set the callback with additional arguments, use setEnabledCallback().
// 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
{
NodeTrigger trigger;
public override bool Init()
{
// create a trigger node
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).IntPtr addPositionCallback ( PositionDelegate func ) #
Adds 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
void position_callback(NodeTrigger trigger)
{
Log.Message("A new position of the node is {0}\n", trigger.getWorldPosition().ToString());
}
NodeTrigger trigger;
public override bool Init()
{
// create a trigger node
trigger = new NodeTrigger();
// add the position callback to be fired when the node position is changed
trigger.addPositionCallback(position_callback);
return 1;
}
}
}
Arguments
- PositionDelegate func - Callback function with the following signature: void PositionDelegate(NodeTrigger trigger)
Return value
ID of the last added position callback, if the callback was added successfully; otherwise, nullptr. This ID can be used to remove this callback when necessary.bool removePositionCallback ( IntPtr id ) #
Removes the specified callback from the list of position callbacks.Arguments
- IntPtr id - Position callback ID obtained when adding it.
Return value
True if the position callback with the given ID was removed successfully; otherwise false.void clearPositionCallbacks ( ) #
Clears all added position callbacks.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.The method allows for setting a callback with 0 or 1 argument only. To set the callback with additional arguments, use setPositionCallback().
// 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
{
NodeTrigger trigger;
public override bool Init()
{
// create a trigger node
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).
string GetPositionCallbackName ( ) #
Returns the name of callback function to be fired on changing the trigger node position. This function is set by using the setPositionCallbackName() function.Return value
Name of the callback function implemented in the world script (UnigineScript side).Last update:
2020-06-01
Help improve this article
Was this article helpful?
(or select a word/phrase and press Ctrl+Enter)