ObjectRef
¶
The ObjectRef class stores an owning reference to a KanziObject instance.
The ObjectRef instance holds the reference to the KanziObject derived class. The instance inherits its type and you can use it directly in Kanzi Lua API calls.
-- Create an ObjectRef instance that keeps a reference to a TextBlock2D.
local textRef = TextBlock2D:create("Text")
-- This instance inherits TextBlock2D and you can use it directly in Kanzi Lua API calls.
textRef:setText("Hello Lua world!")
-- You can pass it as a parameter to functions that accept KanziObject instances.
contextNode:addChild(textRef)
The lifetime of the referred object is at least as long as the ObjectRef instance.
ObjectRef instance, inaccessible from a Lua script, is eventually collected by the Lua garbage collector.
You can control the Lua automatic memory management using the collectgarbage()
function.
-- Create an ObjectRef instance.
local nodeRef = EmptyNode2D:create("Child node")
-- Add node to the node tree.
contextNode:addChild(nodeRef)
-- Force Lua garbage collector run to ensure that new node is not referenced by Lua anymore.
nodeRef = nil
collectgarbage()
An ObjectRef instance cannot be directly compared with a KanziObject instance. While you can use ObjectRef instances directly in Kanzi Lua API calls, the ObjectRef and KanziObject object types differ and are not comparable. Use the ObjectRef.get() API to get the instance of the KanziObject derived type that is owned by this ObjectRef.
Get the KanziObject derived type that is owned by this ObjectRef.
-- Create an ObjectRef instance.
local nodeRef = TextBlock2D:create("Child node")
-- Add node to the node tree.
contextNode:addChild(nodeRef)
-- Get the instance of KanziObject.
local node = nodeRef:get()
-- Force Lua garbage collector run to ensure that new node is not referenced by Lua anymore.
nodeRef = nil
collectgarbage()
-- Although Lua garbage collection collected ObjectRef instance, you can still use the instance of KanziObject
-- that is added to the node tree and kept alive by its parent.
node:setText("Hello Lua world!")
(KanziObject) | An instance of the KanziObject derived type that is owned by this ObjectRef. |