#include <stdio.h>#defineMax_color 255extern "C"{#include"Lua-5.2.2/src/lauxlib.h"#include"Lua-5.2.2/src/lualib.h"#include"lua-5.2.2/src/lstate.h"}intGetFieldConst Char* Key, lua_state*L) { intresult =0; Lua_pushstring (L, key); Lua_gettable (L,-2);//use the value of the top of the stack as key to access the table on the 2 position. and put its value on the top of the stack if(!lua_isnumber (L,-1))//converts the top element of the stack to a double type{} result= (int) Lua_tonumber (L,-1) *Max_color;//lua_tonumber only the top element of the conversion stack is not out of the stack of Lua_pop (L,1);//Pop 1 elements out of the stack returnresult;}intMainintargcChar*argv[]) { Const Char* BUF ="background = {r=30, g=10, b=0}"; Lua_state) O =lual_newstate (); Lual_dostring (L, buf); Lua_getglobal (L,"background");//gets the value of the background of the global variable and puts it on top of the stack if(!lua_istable (L,-1))//determine if it is a table { } intRed = GetField ("R", L); intGreen = GetField ("g", L); intBlack = GetField ("b", L); GetChar (); return 0;}
Lua Example Getglobal ()