This page has been translated automatically.
UnigineEditor
Interface Overview
Assets Workflow
Settings and Preferences
Working With Projects
Adjusting Node Parameters
Setting Up Materials
Setting Up Properties
Landscape Tool
Using Editor Tools for Specific Tasks
Extending Editor Functionality
Programming
Setting Up Development Environment
Usage Examples
UnigineScript
C++
C#
UUSL (Unified UNIGINE Shader Language)
File Formats
Rebuilding the Engine Tools
GUI
Double Precision Coordinates
API
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
Warning! This version of documentation is OUTDATED, as it describes an older SDK version! Please switch to the documentation for the latest SDK version.
Warning! This version of documentation describes an old SDK version which is no longer supported! Please upgrade to the latest SDK version.

Event Handling Callbacks

Callback is a function wrapper representing a pointer to static and member functions which are expected to be executed with specified parameters at a certain moment. Callback can be passed as an argument to other function.

Notice
Callbacks are guaranteed to be reentrant and provide safe multi-threaded execution.

Unigine C# API lets you specify function delegates as callbacks to handle events.

Source code (C#)
void node_property_added(Node n, Property property)
{
	/* .. */
}
node.AddCallback(Node.CALLBACK_PROPERTY_NODE_ADD, node_property_added);

You can also use lambda expressions for the same event handling purpose:

Source code (C#)
widget_button.AddCallback(Gui.CLICKED, () => Log.Message("Button pressed\n"));

Practical Use#

Callbacks are widely used in event handling. A number of Unigine API members have several predefined events which can be handled by using callbacks in certain cases.

Triggers#

Triggers are used to detect changes in nodes position or state. Unigine offers three types of built-in triggers:

Here is a simple WorldTrigger usage example:

Source code (C#)
using Unigine;

namespace UnigineApp
{
	class AppWorldLogic : WorldLogic
	{	
		// implement the enter callback
        void enter_callback(Node node)
        {
			Log.Message("\nA node named {0} has entered the trigger\n", node.Name);
        }
		// implement the leave callback
        void leave_callback(Node node)
        {
			Log.Message("\nA node named {0} has left the trigger\n", node.Name);
        }

		WorldTrigger trigger;
		int enter_callback_id;
		
        public override bool Init()
        {
            // create a world trigger
            trigger = new WorldTrigger(new vec3(3.0f));
            // add the enter callback to be fired when a node enters the world trigger
			//and keep its id to be used to remove the callback when necessary
            enter_callback_id = trigger.AddEnterCallback(enter_callback);
			// add the leave callback to be fired when a node leaves the world trigger
            trigger.AddLeaveCallback(leave_callback);

            return 1;
        }
	}
}

To remove the callbacks use the following code:

Source code (C#)
// remove the callback by using its id
trigger.RemoveEnterCallback(enter_callback_id);
// clear all leave callbacks
trigger.ClearLeaveCallbacks();
See Also
  • A C# API sample located in the <UnigineSDK>/source/csharp/samples/Api/Nodes/NodeTrigger folder.
  • A C# API sample located in the <UnigineSDK>/source/csharp/samples/Api/Nodes/WorldTrigger folder.
  • A C# API sample located in the <UnigineSDK>/source/csharp/samples/Api/Nodes/PhysicalTrigger folder.

Widgets#

The widgets base class Widget allows registering callbacks for events defined in the GUI class. The following example shows how to create a WidgetButton and register a callback function for the CLICKED event:

Source code (C#)
// getting the system GUI
Gui gui = Gui.Get();

// creating a button widget and setting its caption
WidgetButton widget_button = new WidgetButton(gui, "Press me");

// rearranging button size
widget_button.Arrange();

// setting button position
widget_button.SetPosition(10,10);

// setting a lambda function to handle CLICKED event
widget_button.AddCallback(Gui.CLICKED, () => Log.Message("Button pressed\n"));

// adding the created button widget to the system GUI
gui.AddChild(widget_button, Gui.ALIGN_OVERLAP | Gui.ALIGN_FIXED);
See Also

C# API samples located in the <UnigineSDK>/source/csharp/samples/Api/Widgets folder.

Physics#

You can track certain events of the physics-related Bodies and Joints:

The following sample shows the way of registering callbacks for a BodyRigid and change the color of a mesh depending on its state:

Source code (C#)
// create a box static mesh
Mesh mesh = new Mesh();
mesh.AddBoxSurface("box_0", new vec3(1.0f));
ObjectMeshStatic node = new ObjectMeshStatic(mesh);
node.SetMaterial("mesh_base", "*");
node.Position = new dvec3(0, 0, 5.0f);

// add a rigid body
BodyRigid body = new BodyRigid(node);

// register callbacks for events by using lambdas
body.AddFrozenCallback(b => b.GetObject().SetMaterialParameterFloat4("albedo_color", new vec4(1.0f, 0.0f, 0.0f, 1.0f), 0));
body.AddPositionCallback(b => b.GetObject().SetMaterialParameterFloat4("albedo_color", new vec4(0.0f, 0.0f, 1.0f, 1.0f), 0));
body.AddContactCallback((b, num) => b.GetObject().SetMaterialParameterFloat4("albedo_color", new vec4(1.0f, 1.0f, 0.0f, 1.0f), 0));

// add a shape to the body
ShapeBox shape = new ShapeBox(body, new vec3(1.0f));
Notice
Physics-based callbacks are executed in parallel with the main tread, so you should not modify nodes inside these functions. If you want to reposition, transform, create or delete nodes captured by your callback function, you can store them in the array and then perform all necessary operations in the update(). See the example for contact callbacks
See Also

A C# API sample located in the <UnigineSDK>/source/csharp/samples/Api/Physics/BodyCallbacks folder.

Properties#

Callback functions can be used to determine actions to be performed when adding or removing node and surface properties as well as when swapping node properties. Here is an example demonstrating how to track adding a node property via callbacks:

Source code (C#)
using Unigine;

namespace UnigineApp
{
	class AppWorldLogic : WorldLogic
	{	
		// implement the enabled callback
        void node_property_added(Node n, Property property)
        {
			Log.Message("Property \"{0}\" was added to the node named \"{1}\".\n", property.Name, n.Name);
			// ...
        }

		Node node;
		
        public override bool Init()
        {
			// somewhere in the code

			Properties properties = Engine.properties;

			// inheriting a new property named "my_prop" from the base property "node_base"
			properties.FindManualProperty("node_base").Inherit("my_prop");

			// setting our callback function on adding a node property
			node.AddCallback(Node.CALLBACK_PROPERTY_NODE_ADD, node_property_added);

			// adding the property named "my_prop" to the node
			node.AddProperty("my_prop");

            return true;
        }
	}
}

You can add callbacks to track any changes made to a property and its parameters and perform certain actions.

The example below shows how to add a callback to track changes of property parameters and report the name of the property and the changed parameter (suppose we have a manual property named my_prop with an integer parameter named my_int_param).

Source code (C#)
using Unigine;

namespace UnigineApp
{
	class AppWorldLogic : WorldLogic
	{	
		// implement the parameter changed callback
        void parameter_changed(Property property, int num)
        {
			Log.Message("Parameter \"{0}\" of the property \"{1}\" has changed its value.\n", property.GetParameterPtr(num).Name, property.Name);
			// ...
        }

        public override bool Init()
        {
			// somewhere in the code

			// getting a manual property named "my_prop" via the Property Manager
			Property property = Properties.FindManualProperty("my_prop");

			// setting our callback function on parameter change
			property.AddCallback(Property.CALLBACK_PARAMETER_CHANGED, parameter_changed);

			// changing the value of the "my_int_param" parameter
			property.SetParameterInt(property.FindParameter("my_int_param"), 3);

            return true;
        }
	}
}

You can also add callbacks to the Properties manager to track any changes made to any property and perform certain actions:

Source code (C#)
public void property_removed(Property property)
{
	Log.Message("Property \"{0}\" was removed.\n", property.getName());
    // ...
}

// somewhere in the code

Properties properties = Engine.properties;

// inheriting a new property named "my_prop" from the base property "surface_base"
Property property = properties.FindManualProperty("surface_base").Inherit("my_prop");

// setting our callback function on property removal
properties.AddCallback(Properties.CALLBACK_REMOVED, property_removed);

// removing the property named "my_prop"
properties.RemoveProperty(properties.FindProperty("my_prop").GetGUID());

See Also#

These are not all usage examples of event handling callbacks. The following list contains more API members supporting event handling:

  • UserInterface - for handling events from widgets created by loading a UI file.
  • Render - callback functions can be used to get access to buffers and matrices at intermediate stages of the rendering sequence.
  • Console supports adding a callback function that will be executed when a text is output to the console.
  • EditorLogic - a set of editor callback functions can be overridden for certain purposes.
  • ComponentBase - there may be performed certain actions on destroying a component.
  • AsyncQueue - сallback functions can be used to determine actions to be performed when certain resources are loaded.
  • WorldSplineGraph provides a set of callbacks for handling actions on editing points and segments.
  • Viewport - callback functions can be used to get access to buffers and matrices at intermediate stages of the rendering sequence.

Plugins:

Last update: 2020-06-01
Build: ()