Sign in to follow this  

Getting modified parameters back to C after calling a lua function

This topic is 2846 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

Hi, I would like to pass a table to a lua function from C, add or modify the table in Lua and retrieve the modified version in C. So something like this:
lua_getglobal(L, "magic_function");

// Create new table
lua_newtable(L);

// Push 1st key value pair
lua_pushstring(L, "a");
lua_pushinteger(L, value1);
lua_rawset(L, -3);

// Push 2nd key value pair
lua_pushstring(L, "b");
lua_pushinteger(L, value2);
lua_rawset(L, -3);

// ETC!

// Call function, passing table as 1 parameter
lua_pcall(pState, 1, 0, 0);

Where magic_function might look like this:
 
function magic_function(arguments)
    -- Display argument a
    print("a:" .. argument.a)

    -- Modify argument b
    argument.b = 0
end
As doing a lua_pcall pops the arguments off the stack, I don't know where to read the modified table from. Cheers Jamie

Share this post


Link to post
Share on other sites
Just use lua_pushvalue to copy the value on the top of the stack; one reference to the table gets popped off, the other remains.

Share this post


Link to post
Share on other sites
I've got this working by returning the table, but I'd like to get my original plan to work and Sneftel's suggestion makes perfect sense, BUT when I duplicate the table before attempting to call the function (using lua_pushvalue(L, -1);) the stack looks right:

3 = Duplicate table reference
2 = Table to pass as argument
1 = Function

But the call returns 2 with an 'attempting to call table value' error which presumably means it's confused by the extra table on top of it's expected number of arguments? Any clues?

Share this post


Link to post
Share on other sites
GrayFace is correct the function will pop the arguments and the function off the stack, so you either need to store the table below the function or take a reference from it.


lua_newtable(lua);

std::string magic_function("magic_function = function(t) t.a=1 end");
luaL_loadbuffer(lua,magic_function.c_str(),magic_function.size(),"chunk");
lua_pcall(lua,0,0,0);

lua_getfield(lua, LUA_GLOBALSINDEX, "magic_function");
lua_pushvalue(lua,1);
lua_pcall(lua,1,0,0);

lua_pushlstring(lua,"a",1);
lua_gettable(lua, 1);

int storedValue =lua_tointeger( lua, -1);
lua_pop(lua,2);

assert(1, storedValue );




lua_newtable(lua);
int tableRef = luaL_ref(lua, LUA_REGISTRYINDEX);

std::string magic_function("magic_function = function(t) t.a=1 end");
luaL_loadbuffer(lua,magic_function.c_str(),magic_function.size(),"chunk");
lua_pcall(lua,0,0,0);

lua_getfield(lua, LUA_GLOBALSINDEX, "magic_function");
lua_rawgeti(lua, LUA_REGISTRYINDEX, tableRef );
lua_pcall(lua,1,0,0);

lua_rawgeti(lua, LUA_REGISTRYINDEX, tableRef );
lua_pushlstring(lua,"a",1);
lua_gettable(lua, -2);

int storedValue =lua_tointeger( lua, -1);
lua_pop(lua,2);
luaL_unref(lua,LUA_REGISTRYINDEX,tableRef);

assert(1, storedValue );

Share this post


Link to post
Share on other sites

This topic is 2846 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this