c++ lua error on setting global variable -


i have folowing code:

lua_state *lua; lua = lua_open(); lual_openlibs(lua);  std::string code = "print(gvar)\n"     "function test()\n"     "print(gvar)\n"     "end\n";  if(!lual_loadstring(lua, code.c_str())){     if (lua_pcall(lua, 0, 0, 0)){         const char* error = lua_tostring(lua, -1);          lua_pop(lua, 1);     } }  lua_pushstring(lua, "100"); lua_setglobal(lua, "gvar"); if (lua_pcall(lua, 0, 0, 0)){     const char* error = lua_tostring(lua, -1); // returns "attempt call nil value"     lua_pop(lua, 1); }  lua_close(lua); 

calling functions , getting global variables works fine, when try set global variable "attempt call nil value". , cant understand why that?

if(!lual_loadstring(lua, code.c_str())){     if (lua_pcall(lua, 0, 0, 0)){         const char* error = lua_tostring(lua, -1);          lua_pop(lua, 1);     } } 

this code loads string anonymous function using lual_loadstring(), puts on stack , executes function using lua_pcall(lua, 0, 0, 0).

lua_pushstring(lua, "100"); lua_setglobal(lua, "gvar"); if (lua_pcall(lua, 0, 0, 0)){     const char* error = lua_tostring(lua, -1); // returns "attempt call nil value"     lua_pop(lua, 1); } 

this piece of code pushes string onto stack sets global variable gvar. there should nothing on stack after call lua_setglobal(). var there.

now after try call function @ top of stack lua_pcall, stack empty - that's why attempt call nil value message.


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -