// Load scripts in Init function
if (!LoadScript(CFG.GetVariable("Scripts", "Events")))
{
cout << "Failed to load scripts" << endl;
}
// Execute test script
lua_pushstring(LuaVM, "test");
lua_gettable(LuaVM, LUA_GLOBALSINDEX);
if (lua_pcall(LuaVM, 0, 0, 0) != 0)
cout << "Function call failed" << endl;
// Chunk loader function, passed to the lua_load function
const char *CBasicController::LuaLoad(lua_State *LuaVM, void *Data, size_t *Size)
{
LUALOAD *Load;
Load = (LUALOAD *)Data;
if (Load->bLoaded == true)
return (NULL);
if (Load->Script == NULL)
return (NULL);
if (Load->ScriptSize <= 0)
return (NULL);
*Size = Load->ScriptSize;
Load->ScriptSize = strlen(Load->Script);
Load->bLoaded = true;
return (Load->Script);
}
// Function to load a certain script
bool CBasicController::LoadScript(const char *szScriptName)
{
LUALOAD LoadData;
fstream Script;
bool bRet = false;
LoadData.bLoaded = false;
LoadData.ScriptSize = 0;
LoadData.Script = NULL;
Script.open(szScriptName, ios::in);
if (!Script.is_open())
{
return (false);
}
Script.seekg(0, ios::end);
LoadData.ScriptSize = Script.tellg();
Script.seekg(0, ios::beg);
LoadData.Script = new char[LoadData.ScriptSize];
memset(LoadData.Script, 0, LoadData.ScriptSize);
Script.read(LoadData.Script, LoadData.ScriptSize);
if (lua_load(LuaVM, LuaLoad, (void *)&LoadData, szScriptName) == 0)
bRet = true;
else
bRet = false;
delete [] LoadData.Script;
LoadData.Script = NULL;
return (bRet);
}
🎉 Celebrating 25 Years of GameDev.net! 🎉
Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!
Loading Lua scripts from file
I am trying to load a lua script into memory, in compiled form. However, when I try to load an uncompiled script, the loading fails. And if I load a compiled script, the function call always fails. This is my code:
Anyone knows what is wrong?
Toolmaker
-Earth is 98% full. Please delete anybody you can.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement