I need to know because I supposedly need to know what it is to make a Lua global using lua_setglobal().
Lua (/ˈluːə/ LOO-ə; from Portuguese: lua [ˈlu. (w)ɐ] meaning moon) is a lightweight, high-level, multi-paradigm programming language designed primarily for embedded use in applications.
The expression lua_upvalueindex(1) refers to the index of the first upvalue of the function. So, the lua_tonumber in function counter retrieves the current value of the first (and only) upvalue as a number.
The type userdata is provided to allow arbitrary C data to be stored in Lua variables. A userdata value is a pointer to a block of raw memory. There are two kinds of userdata: full userdata, where the block of memory is managed by Lua, and light userdata, where the block of memory is managed by the host.
This means that all values can be stored in variables, passed as arguments to other functions, and returned as results. There are eight basic types in Lua: nil, boolean, number, string, function, userdata, thread, and table.
You'll want to check out this page in Programming in Lua: A first example To make an analogy, pretend that the C or C++ program is running in a little box and has access to its functions, variables, and so on. The lua_State is basically a way to access what's going on in the Lua "box" during execution of your program and allows you to glue the two languages together.
Brief example which may help...
lua_State* L=lua_open(); // create a Lua state
luaL_openlibs(L); // load standard libs
lua_pushstring(L, "nick"); // push a string on the stack
lua_setglobal(L, "name"); // set the string to the global 'name'
luaL_loadstring(L, "print(name)"); // load a script
lua_pcall(L, 0, 0, 0); // call the script
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With