This topic is now archived and is closed to further replies.

Loading Lua scripts from file

This topic is 5318 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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:
    // 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;, 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);, LoadData.ScriptSize);
    if (lua_load(LuaVM, LuaLoad, (void *)&LoadData, szScriptName) == 0)
        bRet = true;
        bRet = false;

    delete [] LoadData.Script;
    LoadData.Script = NULL;

    return (bRet);
Anyone knows what is wrong? Toolmaker
-Earth is 98% full. Please delete anybody you can.

Share this post

Link to post
Share on other sites