Reading global variables

Reading a global variable is done in three steps. You have to use the name of the variable to push its value onto the stack. Once on the stack, the value can be read by C. After you have the value in C, clean up the stack by removing the old copy. 

You can push a global variable onto the stack by name using the lua_getglobal (lua_State*, const char *) function. The first argument of lua_getglobal is the Lua state. The second argument is the name of the variable to push it onto the stack. This function does not return anything.

The lua_getglobal function uses the name of a global variable to leave its value on the stack. To read the value in C, you have to use one of the lua_to functions such as lua_tonumber or lua_tostring. These functions were discussed in the Reading from the stack section of this chapter. Once you have the value of the variable in C, clean up the stack by calling lua_pop.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.188.201