We will create Lua script
int K3d_GetSphere(const char* _strName)
which calling game engine function
int K3dSphereBuild::FindSphereId(const char* _strName)
The function returns sphere index from sphere name
_strName
class K3dVM{static K3dLua *ms_pLua;static K3dGameData *ms_pGameData; ///< Global game data, data centered game systemstatic K3dScene *ms_pScene;static K3dSphereBuild *ms_pSphereBuild; ///< Pointer to sphere build object for find sphere index from sphere namestaticvoid *ms_pObject; ///< Pointer to some voidstaticint K3d_GetSphere(lua_State *_pState);int GetSphere(constchar* _strName); public: K3dVM(K3dGameData *_pGameData, K3dScene *_pScene); K3dVM(){} ~K3dVM();void RegisterFunctions();void LoadScript(K3dString _strFilename);void LF_Init();void LF_Update();void LF_Delete();};
3. In this class constructor
K3dVM::K3dVM(K3dGameData *_pGameData, K3dScene *_pScene){ ms_pGameData = _pGameData; ms_pScene = _pScene; ms_pLua = new K3dLua(); ms_pSphereBuild = new K3dSphereBuild(_pGameData); RegisterFunctions();}
3.1 Open Lua script language ms_pLua = new K3dLua();
(is calling K3dLua constructor)
4.1 Simple Lua script save as
test_script.lua file
5. If our function is registered then automatically will be called VM static function with same name.
///\briefScriptfunctioncallingGetSpherefunctionint K3dVM::K3d_GetSphere(lua_State *_pState){// Cast the void pointer K3dVM *pSomeClass = (K3dVM*)ms_pObject;// Get function parameterconstchar* strParam = (constchar*) lua_tostring(_pState, 1); // Try call non static functionint iResult = pSomeClass->GetSphere(strParam);// Push result into script function lua_pushnumber(_pState, iResult);// Return number of resultsreturn1;}
5.1 Because we want call non-static function from static function we must cast void pointer to K3dVM class
K3dVM *pSomeClass = (K3dVM*)ms_pObject;
5.2 Get first parameter from script function. First parameter is sphere name.
5.3 Call non-static function GetSphere of casting class K3dVM with sphere name parameter.
int iResult = pSomeClass->GetSphere(strParam);
5.3.1 The function GetSphere finding sphere index and returns it. If sphere name doesn`t exists then function returns index -1
///\briefGetsphereindexfromgamedata///\param_strName Sphere name///\retvalint Sphere index. If return -1, then doesn`t exist current sphereint K3dVM::GetSphere(constchar* _strName){// Find sphere index by sphere nameint iId = ms_pSphereBuild->FindSphereId(_strName);return iId;}
int error = ms_pLua->LuaLoadFile( _strFilename.GetString().c_str());
///\briefLoadluascriptfile///\param_strFilename Filename string///\retvalint Positive value, if file correctly loaded int K3dLua::LuaLoadFile(constchar *_strFilename){return luaL_loadfile(m_pLua, _strFilename);}
6.2 If the file succeed open, run script by calling function
LuaPCall
error = ms_pLua->LuaPCall(0, 0, 0);
///\briefCallluascript///\param_iNumArgs Number of arguments///\param_iNumResults Number of results///\param_iErrFunc Error function indexint K3dLua::LuaPCall(constint _iNumArgs,constint _iNumResults,constint _iErrFunc){return lua_pcall(m_pLua, _iNumArgs, _iNumResults, _iErrFunc);}