Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

returning a table no global from lua to C

I have a method called GetParameter in C. And I want to use it from Lua. This method is going to return some values to Lua again.

The way that Im building the table in C is the most common way:

lua_newtable(L);
for (int i = 0; i < parameters; i++)
{
    lua_pushnumber(L,i);
    lua_pushstring(L,myParameter);
    lua_settable(L,-3);
}

In all examples that I've seen, after this, you have to set the result table with lua setGlobal:

 //set name for the result
 lua_setglobal(ptLuaState, "resultTable");

With this method, I can access to the result table in lua, like this:

GetParameter("V111","V111Parameter")
printTable(resultTable);

Doing this all things goes well, but, there is another way to do this without using setglobal? I've tried to do something like:

local mytable=GetParameter("V111","V111Parameter");

but doesnt work. Using global variables is better? how can I get the result table without make the setglobal?

thanks in advance!

like image 248
opernas Avatar asked Aug 26 '26 00:08

opernas


1 Answers

Don't call setglobal and return 1 from your C function. That tells lua there is 1 return value from your function, and your last example will work.

like image 188
Tom Whittock Avatar answered Aug 27 '26 18:08

Tom Whittock



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!