Lua Example Getglobal ()

Source: Internet
Author: User

#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 ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.