How to access LUA script variables through lua api in C ++

Source: Internet
Author: User

C ++PassedLUA APIAccessLUA scriptVariable learning is the content to be introduced in this article. It is mainly used to learn about stack operations and data type judgment.LUA APIYou can use these functions to obtain the variable values in the script.

1. Steps

Compile test01.LuaScript, create and correctly configure the console C ++ program in VS2003, execute the command to view the result, and modify test02.Lua scriptView the execution result

2. Test script

The following is the lua script for testing.

 
 
  1. function plustwo(x)      
  2.       local a = 2;      
  3.       return x+a;  
  4. end;  
  5. rows = 6;  
  6. cols = plustwo(rows); 

The above script defines a function and two global variables.LUA scriptThe variables are global by default ). In the subsequent C ++ program, we will obtain the two variables rows and cols through stack operations.

3. Console Program

 
 
  1. #include <iostream> 
  2.  
  3. extern "C"  
  4. {  
  5.     #include "lua.h"  
  6.     #include "lauxlib.h"  
  7.     #include "lualib.h"  
  8. }  
  9.  
  10. using namespace std;  
  11.  
  12. int main(int argc, char* argv[])  
  13. {  
  14.     cout << "01_Read_Stack" << endl;  
  15.  
  16.     /**//* Create a LUA VMachine */  
  17.     lua_State *L = lua_open();  
  18.     luaopen_base(L);  
  19.     luaopen_table(L);  
  20.     luaL_openlibs(L);  
  21.     luaopen_string(L);  
  22.     luaopen_math(L);  
  23.  
  24.     int iError;  
  25.     iError = luaL_loadfile(L, "../test01.lua");  
  26.     if (iError)  
  27.     {  
  28.         cout << "Load script FAILED!" << lua_tostring(L, -1)<< endl;  
  29.         lua_close(L);  
  30.         return 1;  
  31.     }  
  32.     iError = lua_pcall(L, 0, 0, 0);  
  33.     if (iError)  
  34.     {  
  35.         cout << "pcall FAILED"<< lua_tostring(L, -1)<< iError<< endl;  
  36.         lua_close(L);  
  37.         return 1;  
  38.     }  
  39.       
  40.     lua_getglobal(L, "rows");  
  41.     lua_getglobal(L, "cols");  
  42.  
  43.     if (!lua_isnumber(L, -2))  
  44.    {  
  45.         cout << "[rows] is not a number" << endl;  
  46.         lua_close(L);  
  47.         return 1;  
  48.     }  
  49.     if (!lua_isnumber(L, -1))  
  50.     {  
  51.         cout << "[cols] is not a number" << endl;  
  52.         lua_close(L);  
  53.         return 1;  
  54.     }  
  55.     cout << "[rows]"  
  56.          << static_cast<int> (lua_tonumber(L, -2))  
  57.          << "[cols]"  
  58.          << static_cast<int> (lua_tonumber(L, -1))  
  59.          << endl;  
  60.  
  61.     lua_pop(L,2);  
  62.     lua_close(L);  
  63.     return 0;  

Summary:C ++PassedLUA APIAccessLUA scriptThe variable learning tutorial is complete. I hope this article will help you!

Related Article

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.