Nanfeng

Notes on software development, code, and curious ideas

tolua.cast, tolua.type, and tolua.isnull in Cocos2d-x Lua

tolua.cast(object, typeName)

tolua.cast treats a bound object as the specified native type:

1
local node = tolua.cast(layer, "cc.Node")

This can be useful when an object retrieved through a generic API has lost the more specific Lua-facing methods expected by the caller. The requested type must match the actual C++ inheritance relationship; a cast does not transform an unrelated object.

tolua.type(object)

tolua.type returns the native type description for a bound C++ object:

1
2
local node = display.newNode()
print(tolua.type(node))

The exact returned name depends on the engine and binding version.

tolua.isnull(object)

A Lua value may remain reachable after its underlying C++ object has been deleted. Calling native methods through that stale binding can fail. Check it first:

1
2
3
if object and not tolua.isnull(object) then
object:setVisible(true)
end

This check is useful at asynchronous or lifecycle boundaries, but ownership should still be managed carefully so stale native references are not retained longer than necessary.

Original reference

+