This page has been translated automatically.
视频教程
界面
要领
高级
实用建议
专业(SIM)
UnigineEditor
界面概述
资源工作流程
版本控制
设置和首选项
项目开发
调整节点参数
Setting Up Materials
设置属性
照明
Sandworm
使用编辑器工具执行特定任务
如何擴展編輯器功能
嵌入式节点类型
Nodes
Objects
Effects
Decals
光源
Geodetics
World Nodes
Sound Objects
Pathfinding Objects
Players
编程
基本原理
搭建开发环境
使用范例
C++
C#
UnigineScript
UUSL (Unified UNIGINE Shader Language)
Plugins
File Formats
材质和着色器
Rebuilding the Engine Tools
GUI
双精度坐标
应用程序接口
Containers
Common Functionality
Controls-Related Classes
Engine-Related Classes
Filesystem Functionality
GUI-Related Classes
Math Functionality
Node-Related Classes
Objects-Related Classes
Networking Functionality
Pathfinding-Related Classes
Physics-Related Classes
Plugins-Related Classes
IG Plugin
CIGIConnector Plugin
Rendering-Related Classes
创建内容
内容优化
材质
Material Nodes Library
Miscellaneous
Input
Math
Matrix
Textures
Art Samples
Tutorials
注意! 这个版本的文档是过时的,因为它描述了一个较老的SDK版本!请切换到最新SDK版本的文档。
注意! 这个版本的文档描述了一个不再受支持的旧SDK版本!请升级到最新的SDK版本。

Unigine::NodeTrigger Class

Header: #include <UnigineNodes.h>
Inherits from: 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).

The trigger node is usually added as a child node to another node, so that the callbacks will be fired 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 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.

AppWorldLogic.h (C++)
#include <UnigineLogic.h>

using namespace Unigine;

class AppWorldLogic : public Unigine::WorldLogic {

public:
	
	virtual int init();
	
	virtual int update();
	virtual int postUpdate();
	virtual int updatePhysics();
	
	virtual int shutdown();
	
	virtual int save(const Unigine::StreamPtr &stream);
	virtual int restore(const Unigine::StreamPtr &stream);

private:

	ObjectMeshStaticPtr object;
	NodeTriggerPtr trigger;
};
AppWorldLogic.cpp (C++)
#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 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 are set by callback pointers: AddEnabledCallback() and AddPositionCallback() receive pointers to the callback functions.

AppWorldLogic.h (C++)
// AppWorldLogic.h

#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_callback(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_callback(NodeTriggerPtr trigger)
	{
		Log::message("The enabled flag is %d\n", trigger->isEnabled());
	}
};
AppWorldLogic.cpp (C++)
// AppWorldLogic.cpp

#include "AppWorldLogic.h"

using namespace Math;

int AppWorldLogic::init() {

	// create a mesh
	MeshPtr mesh = Mesh::create();
	mesh->addBoxSurface("box_0", vec3(1.0f));
	// create a node (e.g. an instance of the ObjectMeshStatic class)
	object = ObjectMeshStatic::create(mesh);

	// change material albedo color
	object->setMaterialParameterFloat4("albedo_color", vec4(1.0f, 0.0f, 0.0f, 1.0f), 0);

	// create node trigger
	trigger = NodeTrigger::create();

	// add the trigger node to the static mesh as a child node
	object->addWorldChild(trigger);

	// add the enabled and position callbacks
	trigger->addEnabledCallback(MakeCallback(this, &AppWorldLogic::enabled_callback));
	trigger->addPositionCallback(MakeCallback(this, &AppWorldLogic::position_callback));

	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#

NodeTrigger Class

Members


static NodeTriggerPtr create ( ) #

Constructor. Creates a new trigger node.

void * addEnabledCallback ( Unigine::CallbackBase1< Ptr<NodeTrigger> > * func ) #

Sets a pointer to a callback to be fired when the trigger node is enabled or disabled. The callback function must receive a NodeTrigger as its first argument. In addition, it can also take 2 arguments of any type.

Source code (C++)
// implement the enabled callback
void AppWorldLogic::enabled_callback(NodeTriggerPtr trigger){
	Log::message("The enabled flag is %d\n", trigger->isEnabled());
}

NodeTriggerPtr trigger;

int AppWorldLogic::init() {

	// create a trigger node
	trigger = NodeTrigger::create();
	
	// add the enabled callback to be fired when the node is enabled/disabled
	trigger->addEnabledCallback(MakeCallback(this, &AppWorldLogic::enabled_callback));
	
	return 1;
}

Arguments

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 ( void * id ) #

Removes the specified callback from the list of enabled callbacks.

Arguments

  • void * 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 ( 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 Node or a NodeTrigger.
Notice
The method allows for setting a callback with 0 or 1 argument only. To set the callback with additional arguments, use setEnabledCallback().
On UnigineScript side:
Source code (UnigineScript)
// implement the enabled callback
void enabled_callback(Node node) {
	log.message("The enabled flag is %d\n", node.isEnabled());
}
On C++ side:
Source code (C++)
int AppWorldLogic::init() {

	// create a trigger node
	trigger = NodeTrigger::create();
	
	// set the enabled callback to be fired when the node is enabled/disabled
	trigger->setEnabledCallbackName("enabled_callback");
	
	return 1;
}

Arguments

  • const char * name - Name of the callback function implemented in the world script (UnigineScript side).

const char * 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 * addPositionCallback ( Unigine::CallbackBase1< Ptr<NodeTrigger> > * 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.
Source code (C++)
// implement the position callback
void AppWorldLogic::position_callback(NodeTriggerPtr trigger){
	Log::message("A new position of the node is (%f %f %f)\n", trigger->getWorldPosition().x, trigger->getWorldPosition().y, trigger->getWorldPosition().z);
}

NodeTriggerPtr trigger;

int AppWorldLogic::init() {

	// create a trigger node
	trigger = NodeTrigger::create();
	
	// add the position callback to be fired when the node position is changed
	trigger->addPositionCallback(MakeCallback(this, &AppWorldLogic::position_callback));
	
	return 1;
}

Arguments

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 ( void * id ) #

Removes the specified callback from the list of position callbacks.

Arguments

  • void * 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 ( 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 Node or a NodeTrigger.
Notice
The method allows for setting a callback with 0 or 1 argument only. To set the callback with additional arguments, use setPositionCallback().
On UnigineScript side:
Source code (UnigineScript)
// implement the position callback
void position_changed(NodeTriggerPtr trigger) {
	log.message("A new position of the node is %s\n", typeinfo(node.getWorldPosition()));
}
On C++ side:
Source code (C++)
int AppWorldLogic::init() {

	// create a trigger node
	trigger = NodeTrigger::create();
	
	// set the position callback to be fired when the node position is changed
	trigger->setPositionCallbackName("position_callback");
	
	return 1;
}

Arguments

  • const char * name - Name of the callback function implemented in the world script (UnigineScript side).

const char * 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).

static int type ( ) #

Returns the type of the node.

Return value

NodeTrigger type identifier.
Last update: 2023-08-02
Build: ()