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 ?