user1782677
user1782677

Reputation: 2007

How to load Lua Table from disk using Lua language?

I have a plain text lua table on disk. It looks something like this:

rootTable = {

     name="myTable",

     settings = {
         setting1="some stuff",
         setting2="some more stuff"
     },
     parameters = {
          { name="paramName",  values={x=1.0,y=1.0,z=0.0,w=0.0} },
          { name="paramName2", values={x=0.0,y=3.0,z=1.0,w=0.0} }
     }
}

I'm trying to open up this table in lua (within c++) using the following code:

const char *filename = "path/file.txt"

lua_State *state = luaL_newstate();
luaL_openlibs(state);
int error = luaL_loadfile(state, filename);
if (error == 0) {
    error = lua_pcall(_state, 0, 0, 0);
}
if(error != 0)
{
    std::string errorMessage = lua_tostring(_state, -1);
    fprintf(stderr, "%s\n", _errorMessage.c_str());
}

However whenever I try to run this, I get the following error message:

unexpected symbol near '{'

I don't know what this means or how I can fix it. Any help would be appreciated.

Upvotes: 2

Views: 177

Answers (1)

Paul Kulchenko
Paul Kulchenko

Reputation: 26744

I don't get any failure loading that table using load/loadstring/loadfile in Lua 5.1, 5.2, or 5.3 (and don't see anything wrong with it). You may be loading a different file than the one you are showing.

Upvotes: 2

Related Questions