Class PrefabTemplate

Class Hierarchy

A prefab template contains information that is required to create a tree of Node instances.

The typical workflow for creating nodes in code is to:

  1. Define a prefab template in Kanzi Studio.
  2. Load the exported kzb to the application.
  3. Acquire a prefab template with Node.tryAcquireResource().
  4. Instantiate a tree of nodes with PrefabTemplate.instantiate().
  5. Add the instantiated nodes to the existing node tree with Node2D.addChild().

Inherits properties and message types from PrefabTemplateMetadata.

Synopsis

Methods
instantiate()

Creates a tree of nodes by instantiating a prefab template node

PrefabTemplate:instantiate(rootName)

Creates a tree of nodes by instantiating a prefab template node. Automatically determines the type of the KanziObject of the root node of the instantiated node tree and returns the ObjectRef which contains the reference to it.

Creates a tree of nodes according to the instructions that are stored in the prefab template data. The returned nodes are initially detached and for them to become active you need to add them to the existing node tree.

Example
-- Acquire a PrefabTemplate resource.
local prefab = contextNode:tryAcquireResource("kzb://project/Prefabs/Caption")
-- Instantiate the resource.
local instance = prefab:instantiate("CaptionNode")
-- Prefab root type is automatically determined.
instance:setText("Caption")
-- Add instantiated node to the node tree.
local parentNode = contextNode:lookupNode("..")
parentNode:addChild(instance)
Parameters
rootName (string)

The name of the instantiated root node. Overrides the name that is defined in the prefab template data. The child names are not overridden.

Return Values
(ObjectRef)

An instance of an ObjectRef, containing the reference to the root of the instantiated node tree.