Cocos2d-x  v3.17
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Groups Pages
LuaEngine Class Reference

The Lua engine integrated into the cocos2d-x to process the interactive operation between lua and c++. More...

Inherits ScriptEngineProtocol.

Public Member Functions

virtual ~LuaEngine (void)
 Destructor of LuaEngine.
 
virtual ccScriptType getScriptType () override
 Get ccScriptType of LuaEngine used, it is always kScriptTypeLua. More...
 
LuaStackgetLuaStack (void)
 Get LuaStack of the LuaEngine. More...
 
virtual void addSearchPath (const char *path)
 Add a path to find lua files in. More...
 
virtual void addLuaLoader (lua_CFunction func)
 Add lua loader. More...
 
virtual int reload (const char *moduleFileName)
 Reload script code corresponding to moduleFileName. More...
 
virtual void removeScriptObjectByObject (Ref *object) override
 Remove the related reference about the Ref object stored in the Lua table by set the value of corresponding key nil: The related Lua tables are toluafix_refid_ptr_mapping,toluafix_refid_type_mapping,tolua_value_root and object_Metatable["tolua_ubox"] or tolua_ubox. More...
 
virtual void removeScriptHandler (int nHandler) override
 Remove Lua function reference by nHandler by setting toluafix_refid_function_mapping[nHandle] nil. More...
 
virtual int reallocateScriptHandler (int nHandler) override
 Reallocate Lua function reference index to the Lua function pointer to add reference. More...
 
virtual int executeString (const char *codes) override
 Execute script code contained in the given string. More...
 
virtual int executeScriptFile (const char *filename) override
 Execute a script file. More...
 
virtual int executeGlobalFunction (const char *functionName) override
 Execute a scripted global function. More...
 
virtual bool handleAssert (const char *msg) override
 Handle the assert message. More...
 
virtual bool parseConfig (ConfigType type, const std::string &str) override
 Parse the config information data. More...
 
virtual int sendEvent (ScriptEvent *message) override
 When some events triggered in the c++ also needs to pass on to lua to handle, we could call this function to send events. More...
 
virtual int handleEvent (ScriptHandlerMgr::HandlerType type, void *data)
 Pass on the events related with ScrollView,TableCell,AssertManager, Armature, Accelerometer, Keyboard, Touch, Touches ,Mouse and Custom event to lua to handle. More...
 
virtual int handleEvent (ScriptHandlerMgr::HandlerType type, void *data, int numResults, const std::function< void(lua_State *, int)> &func)
 Pass on the events related with TableCell and TableView to lua to handle. More...
 
- Public Member Functions inherited from ScriptEngineProtocol
 ScriptEngineProtocol ()
 Constructor of ScriptEngineProtocol.
 
virtual ~ScriptEngineProtocol ()
 Destructor of ScriptEngineProtocol.
 
virtual void retainScriptObject (Ref *, Ref *)
 Reflect the retain relationship to script scope.
 
local retainScriptObject ()
 Reflect the retain relationship to script scope.
 
virtual void rootScriptObject (Ref *)
 Add the script object to root object.
 
virtual void releaseScriptObject (Ref *, Ref *)
 Reflect the release relationship to script scope.
 
local releaseScriptObject ()
 Reflect the release relationship to script scope.
 
virtual void unrootScriptObject (Ref *)
 Remove the script object from root object.
 
virtual void releaseAllChildrenRecursive (Node *)
 Release all children native refs for the given node in script scope.
 
local releaseAllChildrenRecursive ()
 Release all children native refs for the given node in script scope.
 
virtual void releaseAllNativeRefs (cocos2d::Ref *)
 Release all native refs for the given owner in script scope.
 
local releaseAllNativeRefs ()
 Release all native refs for the given owner in script scope.
 
virtual void setCalledFromScript (bool)
 Useless for Lua.
 
virtual bool isCalledFromScript ()
 Useless for Lua.
 
virtual void rootObject (Ref *)
 Root a Reference. More...
 
virtual void unrootObject (Ref *)
 Unroot a Reference. More...
 
virtual void removeObjectProxy (Ref *obj)
 Remove proxy for a native object.
 
virtual void garbageCollect ()
 Triggers the garbage collector.
 
local garbageCollect ()
 Triggers the garbage collector.
 

Static Public Member Functions

static LuaEnginegetInstance (void)
 Get instance of LuaEngine. More...
 
static LuaEnginedefaultEngine (void)
 Get defaultEngine of LuaEngine, it was deprecated. More...
 

Additional Inherited Members

- Public Types inherited from ScriptEngineProtocol
enum  ConfigType
 ConfigType enum. More...
 

Detailed Description

The Lua engine integrated into the cocos2d-x to process the interactive operation between lua and c++.

Member Function Documentation

static LuaEngine* getInstance ( void  )
static

Get instance of LuaEngine.

Returns
the instance of LuaEngine.
static LuaEngine* defaultEngine ( void  )
inlinestatic

Get defaultEngine of LuaEngine, it was deprecated.

Returns
the instance of LuaEngine.
virtual ccScriptType getScriptType ( )
inlineoverridevirtual

Get ccScriptType of LuaEngine used, it is always kScriptTypeLua.

Returns
kScriptTypeLua.

Reimplemented from ScriptEngineProtocol.

LuaStack* getLuaStack ( void  )
inline

Get LuaStack of the LuaEngine.

All the interactive operation are all base on the LuaStack.

Returns
LuaStack object.
virtual void addSearchPath ( const char *  path)
virtual

Add a path to find lua files in.

Parameters
pathto be added to the Lua path.
virtual void addLuaLoader ( lua_CFunction  func)
virtual

Add lua loader.

Parameters
funca function pointer point to the loader function.
virtual int reload ( const char *  moduleFileName)
virtual

Reload script code corresponding to moduleFileName.

If value of package["loaded"][moduleFileName] is existed, it would set the value nil.Then,it calls executeString function.

Parameters
moduleFileNameString object holding the filename of the script file that is to be executed.
Returns
0 if the string is executed correctly or other if the string is executed wrongly.
virtual void
removeScriptObjectByObject
( Ref object)
overridevirtual

Remove the related reference about the Ref object stored in the Lua table by set the value of corresponding key nil: The related Lua tables are toluafix_refid_ptr_mapping,toluafix_refid_type_mapping,tolua_value_root and object_Metatable["tolua_ubox"] or tolua_ubox.

Meanwhile set the corresponding userdata nullptr and remove the all the lua function reference corresponding to this object.

In current mechanism, this function is called in the destructor of Ref object, developer don't call this functions.

Parameters
objectthe key object to remove script object.

Reimplemented from ScriptEngineProtocol.

virtual void removeScriptHandler ( int  nHandler)
overridevirtual

Remove Lua function reference by nHandler by setting toluafix_refid_function_mapping[nHandle] nil.

Parameters
nHandlerthe function reference index to find the corresponding Lua function pointer.

Reimplemented from ScriptEngineProtocol.

virtual int reallocateScriptHandler ( int  nHandler)
overridevirtual

Reallocate Lua function reference index to the Lua function pointer to add reference.

Parameters
nHandlerthe function reference index to find the corresponding Lua function pointer.

Reimplemented from ScriptEngineProtocol.

virtual int executeString ( const char *  codes)
overridevirtual

Execute script code contained in the given string.

Parameters
codesholding the valid script code that should be executed.
Returns
0 if the string is executed correctly, other if the string is executed wrongly.

Implements ScriptEngineProtocol.

virtual int executeScriptFile ( const char *  filename)
overridevirtual

Execute a script file.

Parameters
filenameString object holding the filename of the script file that is to be executed.
Returns
the return values by calling executeFunction.

Implements ScriptEngineProtocol.

virtual int executeGlobalFunction ( const char *  functionName)
overridevirtual

Execute a scripted global function.

The function should not take any parameters and should return an integer.

Parameters
functionNameString object holding the name of the function, in the global script environment, that is to be executed.
Returns
The integer value returned from the script function.

Implements ScriptEngineProtocol.

virtual bool handleAssert ( const char *  msg)
overridevirtual

Handle the assert message.

Returns
return true if current _callFromLua of LuaStack is not equal to 0 otherwise return false.

Implements ScriptEngineProtocol.

virtual bool parseConfig ( ConfigType  type,
const std::string &  str 
)
overridevirtual

Parse the config information data.

Parameters
typein current mechanism,it always ConfigType::COCOSTUDIO.
strthe information data.
Returns
if __onParseConfig function exist in the Lua, it return the value that _stack->executeFunction returns otherwise return false.

Implements ScriptEngineProtocol.

virtual int sendEvent ( ScriptEvent message)
overridevirtual

When some events triggered in the c++ also needs to pass on to lua to handle, we could call this function to send events.

Parameters
messagethe ScriptEvent object that has ScriptEventType and the pointer to information data.
Returns
default return 0 otherwise return values the same as handleNodeEvent, handleMenuClickedEvent or handleCallFuncActionEvent,etc.

Implements ScriptEngineProtocol.

virtual int handleEvent ( ScriptHandlerMgr::HandlerType  type,
void *  data 
)
virtual

Pass on the events related with ScrollView,TableCell,AssertManager, Armature, Accelerometer, Keyboard, Touch, Touches ,Mouse and Custom event to lua to handle.

Parameters
typeDifferent ScriptHandlerMgr::HandlerType means different processing for the data.
dataThe pointer point to the information which should be pass on to lua, it would be parsed in the function to convert to the specific data according to the ScriptHandlerMgr::HandlerType,then pass to lua as function parameters.
Returns
default return 0 otherwise return values according different ScriptHandlerMgr::HandlerType.
virtual int handleEvent ( ScriptHandlerMgr::HandlerType  type,
void *  data,
int  numResults,
const std::function< void(lua_State *, int)> &  func 
)
virtual

Pass on the events related with TableCell and TableView to lua to handle.

Parameters
typeDifferent ScriptHandlerMgr::HandlerType means different processing for the data.
dataThe pointer point to the information which should be pass on to lua, it would be parsed in the function to convert to the specific data according to the ScriptHandlerMgr::HandlerType,then pass to lua as function parameters.
numResultsThe number of the return values.
funcThe callback would be called when numResults is > 0.
Returns
default return 0 otherwise return values according different ScriptHandlerMgr::HandlerType.

The documentation for this class was generated from the following file: