Greetings. I work with the LUA script and C ++. The essence of the problem is that the data from the Lua script does not fall into the stack, or C ++ does not work correctly with the stack. In more detail, here is the script running on the LUA machine:

 require("QluaConnector"); is_run=true; function main() while is_run == true do for i = 1, 3, 1 do -- print(tostring(i)); -- такой вывод на экран срабатывает верно QluaConnector.StartSendData(i); -- а этот уже нет sleep(1000); end; end end; 

The only thing I need to do is consistently push the stack and read three digits from it: 1, 2, 3.

C ++ code:

 static int forLua_StartDataGet(lua_State *L) { while (true) { int value = lua_tonumber(L, 1); lua_pop(L,-1); Sleep(1000); return(value ); } } 

For the first time, the method does return 1, but instead of 2 and 3 it is already zero. If instead of lua_tonumber you lua_tonumber see the number of items in the lua_gettop stack lua_gettop then 1 function returns 1 and the second and further ones 0. I suspect that there is an error in the script or in C ++, help me figure it out.

    2 answers 2

    In return you need to return not the value, but the number of values ​​returned by the function. In Lua function can return multiple values. The return value itself is pre-pushed onto the stack. Well, lua_pop is not needed here at all.

     static int forLua_StartDataGet(lua_State *L) { int value = lua_tonumber(L, 1); lua_pushinteger(L, value); // положить результат в стек return 1; // говорим lua, что вернули одно значение на стеке } 

    If the function should not return anything, then the code becomes even simpler:

     static int forLua_StartDataGet(lua_State *L) { int value = lua_tonumber(L, 1); ... // используем value return 0; // говорим lua, что наша функция ничего не возвращает } 
    • C ++ should not return a value in lua but only accept 3 different numbers, which it does not do, regardless of whether something needs to be returned. - Sergey
    • @ Sergey Well, I don’t know what your function should do. If you do not need to return anything, then remove the lua_pushinteger and make return 0; . Delov something! - zed
    • @ Sergey And read at least this tutorial , but it seems that you do not understand what is happening there and how. - zed
    • So initially I didn’t have a code other than lua_tonumber which selected 1 item in the stack, but when I call the function again, the script should put a completely different element there and the function should get 2 but not 0. - Sergey
    • Thanks for the manual. this is not read - Sergey

    The lua_pop function takes as input the number of elements that need to be removed from the stack. Accordingly, this number should be positive.

     lua_pop(L, 1); 
    • Unfortunately this did not solve the problem. I get 1 at the beginning as it should be but then 0.00 . - Sergey
    • If I try to get the last element in the stack, value = lua_tonumber(L, -1) it sometimes skips 3. but then again by zeros - Sergey