How can I get the data from the Lua script in C ++ code? As I understand the exchange of data from the script goes to the pros through a special LUA-Стек . Help me figure it out, let's say there is such a very useful feature on Lua :

 function add( x, y ) return x + y end 

In C ++, the following code:

 #include "stdafx.h" extern "C" { #include "Lua\lua.h" #include "Lua\lauxlib.h" #include "Lua\lualib.h" } lua_State* L = NULL; int from_lua_add(int x, int y) { int sum; lua_getglobal(L, "add"); lua_pushnumber(L, x); lua_pushnumber(L, y); lua_call(L, 2, 1); /* Ошибка */ sum = (int)lua_tointeger(L, -1); lua_pop(L, 1); return sum; } int main() { L = lua_open(); luaL_openlibs(L); luaL_dofile(L, "script.lua"); int summ; summ = from_lua_add(1, 1); printf("the summ from lua is %d\n", summ); lua_close(L); getchar(); return 0; } 

a lua_call occurs on the lua_call call, an error of this content:

call to lua api (attempt to call a nil value)

What have I done wrong ?

    2 answers 2

    Code examples can be viewed here: http://lua-users.org/wiki/SampleCode There is a long example of working with Lua from C ++ here: http://lua-users.org/wiki/CallingLuaFromCpp

    I will give a short example of getting data from Lua in C. The difference from C ++ is small.

    http://lua-users.org/wiki/GettingValuesFromLua

    Example code for Lua 5.1.1:

    returnone.c:

     #include <lua.h> #include <lualib.h> #include <lauxlib.h> int main() { lua_State *L = luaL_newstate(); char buff[] = "return 1,'a'"; int error; printf( "%d\n", lua_gettop(L) ); error = luaL_loadbuffer(L, buff, strlen(buff), "my test") || lua_pcall(L, 0, LUA_MULTRET, 0); if (error) { fprintf(stderr, "%s", lua_tostring(L, -1)); lua_pop(L, 1); /* pop error message from the stack */ } printf( "%d\n", lua_gettop(L) ); printf( "%s\n", lua_tostring(L,-2) ); printf( "%s\n", lua_tostring(L,-1) ); return 0; } 
    • What script does this code work with? - Sergey
    • and the principle of interaction between Lua and C + is not very clear. - Sergey
    • one
      The principle of interaction between C and Lua is described in the manual ( lua.org.ru/manual_ru.html ). This mechanism is described in more detail in the book “Programming in the Lua language” to Robert Jerusalem of Jerusalem. In short, the entire interaction between C and Lua goes through the Lua stack. For example, if we call a function in Lua from C, then we must put on the Lua stack the name of the function, and successively each of its parameters. Then call the lua_call function in C, which will start the function in Lua. The result of the function in Lua also falls into this stack. These values ​​can be retrieved by functions lua_tolstring, lua_tonumber, etc. - Ruslan Rakhmanin
    • As I understand it, if I get an error unprotected error in call to lua api (attempt to call a nil value) this indicates that I did not declare the name of the function or one of its parameters on the stack? - Sergey
    • Updated the question. - Sergey

    What have I done wrong ?

    You completely ignored error handling in your code.

    The luaL_dofile function returns the response code to be analyzed (surprise!) And not just ignored. The same applies to lua_getglobal and so on.

    If you add this processing to compile and run the script:

     int err = luaL_dofile(L, "script.lua"); if (err) { printf("Cannot dofile: %s", lua_tostring(L, -1)); lua_pop(L, 1); return 1; } 

    then if the script file is suddenly not found, a message will be displayed:

     Cannot dofile: cannot open script.lua: No such file or directory 

    Also, any syntax errors in the script itself will be processed if it is suddenly written with errors.

    If this processing is removed, then if the script.lua file is script.lua , a message will be displayed as in your question.