This is probably a simple question, but I'm at a standstill. This is for Lua 5.1.
I have a script that works in my environment. In this environment, I have a variable called a βpluginβ that I installed from C ++, for example:
lua_getfield(L, LUA_REGISTRYINDEX, getScriptId()); // Put script env table onto the stack -- env_table lua_pushstring(L, "plugin"); // -- env_table, "plugin" luaW_push(L, this); // -- env_table, "plugin", *this lua_rawset(L, -3); // env_table["plugin"] = *this -- env_table lua_pop(L, -1); // Cleanup -- <<empty stack>>
Before running my Lua script, I installed the following functional environment:
lua_getfield(L, LUA_REGISTRYINDEX, getScriptId()); // Push REGISTRY[scriptId] onto stack -- function, table lua_setfenv(L, -2); // Set that table to be the env for function -- function
When my script works, it can see and interact with the plugin variable, as expected. So far, so good.
At some point, the Lua script calls the C ++ function, and in this function I want to find out if the plugin variable is set.
I have tried many things and I can not see the plugin variable. Here are just 4 things I've tried:
lua_getfield(L, LUA_ENVIRONINDEX, "plugin"); bool isPlugin = !lua_isnil(L, -1); lua_pop(L, 1);
Unfortunately, all isPlugin variables return false. It is as if the C ++ function called from Lua cannot see the variable set in the Lua environment.
Any idea how I can see the plugin variable from C ++?
Thanks!