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

The LuaEngine 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 *owner, Ref *target)
 Reflect the retain relationship to script scope.
 
virtual void rootScriptObject (Ref *target)
 Add the script object to root object.
 
virtual void releaseScriptObject (Ref *owner, Ref *target)
 Reflect the release relationship to script scope.
 
virtual void unrootScriptObject (Ref *target)
 Remove the script object from root object.
 
virtual void releaseAllChildrenRecursive (Node *node)
 Release all children native refs for the given node in script scope.
 
virtual void releaseAllNativeRefs (cocos2d::Ref *owner)
 Release all native refs for the given owner in script scope.
 
virtual void setCalledFromScript (bool callFromScript)
 Set whether the call comes from the script or not, only use for the js. More...
 
virtual bool isCalledFromScript ()
 Get the flag that represents whether the call comes from the script or not,only use for the js. More...
 
virtual void rootObject (Ref *obj)
 Root a Reference. More...
 
virtual void unrootObject (Ref *obj)
 Unroot a Reference. More...
 
virtual void 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.
 

Detailed Description

The LuaEngine 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.

the instance of LuaEngine.

static LuaEngine* defaultEngine ( void  )
inlinestatic

Get defaultEngine of LuaEngine, it was deprecated.

the instance of LuaEngine.

virtual ccScriptType getScriptType ( )
inlineoverridevirtual

Get ccScriptType of LuaEngine used, it is always kScriptTypeLua.

kScriptTypeLua.

Reimplemented from ScriptEngineProtocol.

LuaStack* getLuaStack ( void  )
inline

Get LuaStack of the LuaEngine.

All the interactive operation are all base on the LuaStack.

LuaStack object.

virtual void addSearchPath ( const char *  path)
virtual

Add a path to find Lua files in.

to be added to the Lua path.

virtual void addLuaLoader ( lua_CFunction  func)
virtual

Add Lua loader.

a 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.

String object holding the filename of the script file that is to be executed. 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.

the 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.

the function refrence index to find the correspoinding 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.

the function refrence index to find the correspoinding Lua function pointer.

Reimplemented from ScriptEngineProtocol.

virtual int executeString ( const char *  codes)
overridevirtual

Execute script code contained in the given string.

holding the valid script code that should be executed. 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.

String object holding the filename of the script file that is to be executed. 0 if it happen error or it hasn't return value, otherwise return 1.

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.

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

Implements ScriptEngineProtocol.

virtual bool handleAssert ( const char *  msg)
overridevirtual

Handle the assert message.

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.

in current mechanism,it always ConfigType::COCOSTUDIO. the information data. 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.

the ScriptEvent object that has ScriptEventType and the pointer to information data. 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.

Different ScriptHandlerMgr::HandlerType means different processing for the data. The 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. 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.

Different ScriptHandlerMgr::HandlerType means different processing for the data. The 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. The number of the return values. The callback would be called when numResults is > 0. default return 0 otherwise return values according different ScriptHandlerMgr::HandlerType.


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