Introduction

Kanzi Engine Lua API is a platform-independent interface to the Kanzi Engine using the Lua programming language. It allows you to create application and user interface logic in Kanzi Studio.

When you export a kzb file, Kanzi Studio embeds Lua scripts in the kzb file.

Kanzi uses Lua 5.4. See https://www.lua.org/manual/5.4/.

Executing Lua scripts

To execute a Lua script, in a node, create a trigger with which you want to execute the script, and in the trigger create a Run Lua Script action. In the Run Lua Script action, set the script file that you want to execute when the trigger is set off.

In Kanzi documentation, see:

Kanzi Engine Lua API features

Accessing the current node

When you set off a trigger that executes the Run Lua Script action, the action assigns to the contextNode variable the reference to the node that contains the trigger.

When accessing nodes from a Lua script, Kanzi automatically deduces their type and provides the instance of the correct type. For example, if your trigger is attached to a Text Block 2D node, contextNode is an instance of the TextBlock2D class.

To navigate the node tree, start with the contextNode variable, then use the lookupNode function with either relative path or an alias in the nearest resource dictionary.

Acquiring resources

To acquire resources, use the Node.tryAcquireResource() function.

Setting and retrieving property values

To set and get property values of Kanzi objects, use the KanziObject.getProperty() and KanziObject.setProperty() functions.

Modifying the node tree

To enumerate and modify nodes in the node tree, use the functions in the Node2D and Node3D classes.

Instantiating prefabs

To instantiate prefabs and attach instantiated nodes to the node tree, use the functions in the PrefabTemplate class.

Using the Kanzi Math library

You can use Kanzi math types in your Lua scripts. See ColorRGBA, Vector2, Vector3, Vector4, Matrix3x3, Matrix4x4, Quaternion, SRTValue2D and SRTValue3D.

Dispatching Kanzi messages

To dispatch Kanzi messages, use the Node.dispatchMessage() function. See MessageArguments and MessageType.

Handling Kanzi messages

You can define message handlers in a Lua script and add the message handlers to nodes. See Node.addMessageHandler().