tags:

views:

61

answers:

1

Hey,

in my C file I call luaL_dostring like this:

luaL_dostring(L, "return 'somestring'");

How do I read this return value in C after this line?

Thanks.

Edit: Thanks for the help.

I'd like to add that to remove the element after retrieving it, you use:

lua_pop(L, 1);
+3  A: 

The value is left on the Lua stack. In order to retrieve the value, use one of the lua_toXXXX functions, with -1 as the index argument (-1 refers to the top of the stack). Alternatively, use lua_gettop() to get the size of the stack.

In your case, use this:

luaL_dostring(L, "return 'somestring'");
const char * str = lua_tostring(L, -1);
MiKy
"The value is left on the C stack." should read "The value is left on the Lua stack."
gwell
You are right, I meant the Lua stack used by the C API. Corrected...
MiKy