Unigine::NodeTrigger Class
Header: | #include <UnigineNodes.h> |
Inherits from: | Node |
A Trigger node is a zero-sized node that has no visual representation and triggers events when:
- It is enabled/disabled (the Enabled event is triggered).
- Its transformation is changed (the Position event is triggered).
The Trigger node is usually added as a child node to another node, so that the handler functions were executed on the parent node enabling/disabling or transforming.
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 executes the corresponding function.
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 handlers should be executed.
#include <UnigineLogic.h>
using namespace Unigine;
class AppWorldLogic : public Unigine::WorldLogic {
public:
virtual int init();
virtual int update();
/*...*/
private:
ObjectMeshStaticPtr object;
NodeTriggerPtr trigger;
};
#include "AppWorldLogic.h"
using namespace Math;
int AppWorldLogic::init() {
// create a mesh
object = ObjectMeshStatic::create("core/meshes/box.mesh");
// change material albedo color
object->setMaterialParameterFloat4("albedo_color", vec4(1.0f, 0.0f, 0.0f, 1.0f), 0);
// create a trigger node
trigger = NodeTrigger::create();
// add the trigger node to the static mesh as a child node
object->addWorldChild(trigger);
return 1;
}
Editing a Trigger Node#
Editing a trigger node includes implementing and specifying the Enabled and Position event handlers that are executed on enabling or positioning the Trigger node correspondingly.
The event handler must receive at least 1 argument of the NodeTrigger type. In addition, it can also take another 2 arguments of any type.
The event handlers are set via pointers specified when subscribing to the following events: EventEnabled and EventPosition.
#include <UnigineLogic.h>
#include <UnigineGame.h>
using namespace Unigine;
class AppWorldLogic : public Unigine::WorldLogic {
public:
virtual int init();
virtual int update();
/*...*/
private:
ObjectMeshStaticPtr object;
NodeTriggerPtr trigger;
void position_event_handler(const NodeTriggerPtr &trigger)
{
Log::message("Object position has been changed. New position is: (%f %f %f)\n", trigger->getWorldPosition().x, trigger->getWorldPosition().y, trigger->getWorldPosition().z);
}
void enabled_event_handler(const NodeTriggerPtr &trigger)
{
Log::message("The enabled flag is %d\n", trigger->isEnabled());
}
};
#include "AppWorldLogic.h"
using namespace Math;
int AppWorldLogic::init() {
// create a mesh
object = ObjectMeshStatic::create("core/meshes/box.mesh");
// change material albedo color
object->setMaterialParameterFloat4("albedo_color", vec4(1.0f, 0.0f, 0.0f, 1.0f), 0);
// create a trigger node
trigger = NodeTrigger::create();
// add the trigger node to the static mesh as a child node
object->addWorldChild(trigger);
// subscribe for the Enabled and Position events
trigger->getEventEnabled().connect(this, &AppWorldLogic::enabled_event_handler);
trigger->getEventPosition().connect(this, &AppWorldLogic::position_event_handler);
return 1;
}
int AppWorldLogic::update()
{
float time = Game::getTime();
Vec3 pos = Vec3(Math::sin(time) * 2.0f, Math::cos(time) * 2.0f, 0.0f);
object->setEnabled(pos.x > 0.0f || pos.y > 0.0f);
object->setWorldPosition(pos);
return 1;
}
See Also#
- A video tutorial on How To Use Node Triggers to Detect Changes in Node States
- An article on Event Handling
- 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
Event<const Ptr<NodeTrigger> &> getEventPosition() const#
Usage Example
// implement the Position event handler
void position_event_handler(const Ptr<NodeTrigger> & trigger)
{
Log::message("\Handling Position event\n");
}
//////////////////////////////////////////////////////////////////////////////
// 1. Multiple subscriptions can be linked to an instance of the EventConnections
// class that you can use later to remove all these subscriptions at once
//////////////////////////////////////////////////////////////////////////////
// create an instance of the EventConnections class
EventConnections position_event_connections;
// link to this instance when subscribing to an event (subscription to various events can be linked)
publisher->getEventPosition().connect(position_event_connections, position_event_handler);
// other subscriptions are also linked to this EventConnections instance
// (e.g. you can subscribe using lambdas)
publisher->getEventPosition().connect(position_event_connections, [](const Ptr<NodeTrigger> & trigger) {
Log::message("\Handling Position event (lambda).\n");
}
);
// ...
// later all of these linked subscriptions can be removed with a single line
position_event_connections.disconnectAll();
//////////////////////////////////////////////////////////////////////////////
// 2. You can subscribe and unsubscribe via an instance of the EventConnection
// class. And toggle this particular connection off and on, when necessary.
//////////////////////////////////////////////////////////////////////////////
// create an instance of the EventConnection class
EventConnection position_event_connection;
// subscribe to the Position event with a handler function keeping the connection
publisher->getEventPosition().connect(position_event_connection, position_event_handler);
// ...
// you can temporarily disable a particular event connection to perform certain actions
position_event_connection.setEnabled(false);
// ... actions to be performed
// and enable it back when necessary
position_event_connection.setEnabled(true);
// ...
// remove subscription to the Position event via the connection
position_event_connection.disconnect();
//////////////////////////////////////////////////////////////////////////////
// 3. You can add EventConnection/EventConnections instance as a member of the
// class that handles the event. In this case all linked subscriptions will be
// automatically removed when class destructor is called
//////////////////////////////////////////////////////////////////////////////
// Class handling the event
class SomeClass
{
public:
// instance of the EventConnections class as a class member
EventConnections e_connections;
// A Position event handler implemented as a class member
void event_handler(const Ptr<NodeTrigger> & trigger)
{
Log::message("\Handling Position event\n");
// ...
}
};
SomeClass *sc = new SomeClass();
// ...
// specify a class instance in case a handler method belongs to some class
publisher->getEventPosition().connect(sc->e_connections, sc, &SomeClass::event_handler);
// ...
// handler class instance is deleted with all its subscriptions removed automatically
delete sc;
//////////////////////////////////////////////////////////////////////////////
// 4. You can subscribe and unsubscribe via the handler function directly
//////////////////////////////////////////////////////////////////////////////
// subscribe to the Position event with a handler function
publisher->getEventPosition().connect(position_event_handler);
// remove subscription to the Position event later by the handler function
publisher->getEventPosition().disconnect(position_event_handler);
//////////////////////////////////////////////////////////////////////////////
// 5. Subscribe to an event saving an ID and unsubscribe later by this ID
//////////////////////////////////////////////////////////////////////////////
// define a connection ID to be used to unsubscribe later
EventConnectionId position_handler_id;
// subscribe to the Position event with a lambda handler function and keeping connection ID
position_handler_id = publisher->getEventPosition().connect([](const Ptr<NodeTrigger> & trigger) {
Log::message("\Handling Position event (lambda).\n");
}
);
// remove the subscription later using the ID
publisher->getEventPosition().disconnect(position_handler_id);
//////////////////////////////////////////////////////////////////////////////
// 6. Ignoring all Position events when necessary
//////////////////////////////////////////////////////////////////////////////
// you can temporarily disable the event to perform certain actions without triggering it
publisher->getEventPosition().setEnabled(false);
// ... actions to be performed
// and enable it back when necessary
publisher->getEventPosition().setEnabled(true);
Return value
Event reference.Event<const Ptr<NodeTrigger> &> getEventEnabled() const#
Usage Example
// implement the Enabled event handler
void enabled_event_handler(const Ptr<NodeTrigger> & trigger)
{
Log::message("\Handling Enabled event\n");
}
//////////////////////////////////////////////////////////////////////////////
// 1. Multiple subscriptions can be linked to an instance of the EventConnections
// class that you can use later to remove all these subscriptions at once
//////////////////////////////////////////////////////////////////////////////
// create an instance of the EventConnections class
EventConnections enabled_event_connections;
// link to this instance when subscribing to an event (subscription to various events can be linked)
publisher->getEventEnabled().connect(enabled_event_connections, enabled_event_handler);
// other subscriptions are also linked to this EventConnections instance
// (e.g. you can subscribe using lambdas)
publisher->getEventEnabled().connect(enabled_event_connections, [](const Ptr<NodeTrigger> & trigger) {
Log::message("\Handling Enabled event (lambda).\n");
}
);
// ...
// later all of these linked subscriptions can be removed with a single line
enabled_event_connections.disconnectAll();
//////////////////////////////////////////////////////////////////////////////
// 2. You can subscribe and unsubscribe via an instance of the EventConnection
// class. And toggle this particular connection off and on, when necessary.
//////////////////////////////////////////////////////////////////////////////
// create an instance of the EventConnection class
EventConnection enabled_event_connection;
// subscribe to the Enabled event with a handler function keeping the connection
publisher->getEventEnabled().connect(enabled_event_connection, enabled_event_handler);
// ...
// you can temporarily disable a particular event connection to perform certain actions
enabled_event_connection.setEnabled(false);
// ... actions to be performed
// and enable it back when necessary
enabled_event_connection.setEnabled(true);
// ...
// remove subscription to the Enabled event via the connection
enabled_event_connection.disconnect();
//////////////////////////////////////////////////////////////////////////////
// 3. You can add EventConnection/EventConnections instance as a member of the
// class that handles the event. In this case all linked subscriptions will be
// automatically removed when class destructor is called
//////////////////////////////////////////////////////////////////////////////
// Class handling the event
class SomeClass
{
public:
// instance of the EventConnections class as a class member
EventConnections e_connections;
// A Enabled event handler implemented as a class member
void event_handler(const Ptr<NodeTrigger> & trigger)
{
Log::message("\Handling Enabled event\n");
// ...
}
};
SomeClass *sc = new SomeClass();
// ...
// specify a class instance in case a handler method belongs to some class
publisher->getEventEnabled().connect(sc->e_connections, sc, &SomeClass::event_handler);
// ...
// handler class instance is deleted with all its subscriptions removed automatically
delete sc;
//////////////////////////////////////////////////////////////////////////////
// 4. You can subscribe and unsubscribe via the handler function directly
//////////////////////////////////////////////////////////////////////////////
// subscribe to the Enabled event with a handler function
publisher->getEventEnabled().connect(enabled_event_handler);
// remove subscription to the Enabled event later by the handler function
publisher->getEventEnabled().disconnect(enabled_event_handler);
//////////////////////////////////////////////////////////////////////////////
// 5. Subscribe to an event saving an ID and unsubscribe later by this ID
//////////////////////////////////////////////////////////////////////////////
// define a connection ID to be used to unsubscribe later
EventConnectionId enabled_handler_id;
// subscribe to the Enabled event with a lambda handler function and keeping connection ID
enabled_handler_id = publisher->getEventEnabled().connect([](const Ptr<NodeTrigger> & trigger) {
Log::message("\Handling Enabled event (lambda).\n");
}
);
// remove the subscription later using the ID
publisher->getEventEnabled().disconnect(enabled_handler_id);
//////////////////////////////////////////////////////////////////////////////
// 6. Ignoring all Enabled events when necessary
//////////////////////////////////////////////////////////////////////////////
// you can temporarily disable the event to perform certain actions without triggering it
publisher->getEventEnabled().setEnabled(false);
// ... actions to be performed
// and enable it back when necessary
publisher->getEventEnabled().setEnabled(true);
Return value
Event reference.static NodeTriggerPtr create ( ) #
Constructor. Creates a new trigger node.void setEnabledCallbackName ( const char * 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 Nodeor a NodeTrigger.// implement the enabled callback
void enabled_callback(Node node) {
log.message("The enabled flag is %d\n", node.isEnabled());
}
int AppWorldLogic::init() {
// create a trigger node
trigger = NodeTrigger::create();
// set the enabled event handler function to be executed when the node is enabled/disabled
trigger->setEnabledCallbackName("enabled_event_handler");
return 1;
}
Arguments
- const char * name - Name of the callback function implemented in the world script (UnigineScript side).
const char * getEnabledCallbackName ( ) const#
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 setPositionCallbackName ( const char * 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 Nodeor a NodeTrigger.// implement the position callback
void position_changed(NodeTriggerPtr trigger) {
log.message("A new position of the node is %s\n", typeinfo(node.getWorldPosition()));
}
int AppWorldLogic::init() {
// create a trigger node
trigger = NodeTrigger::create();
// set the position event handler function to be executed when the node position is changed
trigger->setPositionCallbackName("position_event_handler");
return 1;
}
Arguments
- const char * name - Name of the callback function implemented in the world script (UnigineScript side).