Accessing Lua global variables from c++

http://blog.csdn.net/cnjet/article/details/5909541

Calling Lua scripts from c++’s example was written in post How to embed Lua 5.1 in C++. Now, let us look at how to access Lua’s global variables from c++.

Value passing between c++ and Lua rely on Lua stack. Stack is a data structure based on the principle of Last In First Out (LIFO). This is very important keep in mind while coding with C API of Lua.

P.S: I am using Lua 5.1.3’s C API.

01 //aconf.cpp
02 extern "C" {
03 #include "lua.h"
04 #include "lualib.h"
05 #include "lauxlib.h"
06 }
07  
08 int main()
09 {
10     int var1=0,var2=0;
11  
12     lua_State *L = lua_open();
13     luaL_openlibs(L);
14     if (luaL_loadfile(L, "config.lua") || lua_pcall(L, 0, 0, 0))
15     {
16         printf("error: %s", lua_tostring(L, -1));
17         return -1;
18     }
19  
20     lua_getglobal(L, "var1");
21     lua_getglobal(L, "var2");
22     if (!lua_isnumber(L, -2)) {
23         printf ("`var1‘ should be a number/n");
24         return -1;
25     }
26     if (!lua_isnumber(L, -1))
27     {
28         printf("`var2 should be a number/n");
29         return -1;
30     }
31     var1 = (int)lua_tonumber(L, -2);
32     var2 = (int)lua_tonumber(L, -1);
33     printf("var1: %d/nvar2: %d/n",var1, var2);
34     lua_close(L);
35  
36     return 0;
37 }

The Lua script “config.lua”:

var1=12
var2=34

Compilation line:

g++ -o aconf{,.cpp} -llua -ldl

if (luaL_loadfile(L, "config.lua") || lua_pcall(L, 0, 0, 0)) can be written asluaL_dofile(L,"config.lua"), it does the same thing. By calling lua_getglobal(L, "var1");, Lua will push the value of var1 to the stack. When execute getglobal for “var2″, again it will push var2’s value to the stack, var2 will now stack on top of var1.

The most top of the stack will be assign as logical address -1. Stack address -2 will be at below the stack -1. Therefore to access var1, you have to point to -2. For more info regarding Lua stack, can readHERE.

To check the value type in the stack -2 (var1) is it number, we can use this:
lua_isnumber(L, -2)

To access the value to number, we do this:
lua_tonumber(L, -2)

As lua_tonumber(L, -2) will return as double, therefore we must add (int) in front of the function call. Well, we should use lua_tointeger(L, -2) in this case.

Again, I can access var1 and var2 one by one, therefore you access both variables from the top stack.

1 lua_getglobal(L, "var1");
2 var1=lua_tointeger(L,-1);
3 lua_getglobal(L, "var2");
4 var2=lua_tointeger(L,-1);

When accessing Lua script and trigger exception, the error message will always push to the stack, that why We can print out the message when errors detected like this:

printf("error: %s", lua_tostring(L, -1));

Accessing simple variables is simple, but for accessing table data type, you need more steps, the Lua online ebook does cover that.

时间: 2024-08-10 14:04:28

Accessing Lua global variables from c++的相关文章

Variable hoisting Global variables Constants

Another unusual thing about variables in JavaScript is that you can refer to a variable declared later, without getting an exception. This concept is known as hoisting; variables in JavaScript are in a sense "hoisted" or lifted to the top of the

mysql   show global variables

以下变量全部来自mysql5.5.40-log 包括该版本的所有变量, 绝大部分变量都是解释的,那些没有解释的变量不是很重要, 不过后期还是会补充上去的.以下变量只有部分经过本人验证, ,没有验证均来自大师的博客. 仅供参考. 1.自增值相关 auto_increment_increment 1   #   增量 auto_increment_offset 1      #  起始值/偏移量 http://chengxuyuan.naxieshir.com/fenlei/2/p/151.html

How to declare global variables in Android? --- Application Subclasses

I wrote this answer back in '09 when Android was relatively new, and there were many not well established areas in Android development. I have added a long addendum at the bottom of this post, addressing some criticism, and detailing a philosophical

CUDA[3] Samples for accessing shared/global memory

memory model: programming model:

Lua 架构 The Lua Architecture

转载自:http://magicpanda.net/2010/10/lua%E6%9E%B6%E6%9E%84%E6%96%87%E6%A1%A3/ Lua架构文档(翻译) 十 102010 前段时间翻译了lua官方关于lua5架构设计的一份文档,现在分享给大家. 注意:所有版权都归lua官方所有,本人仅将其翻译为中文,以方便中文阅读者.翻译中出现任何错误导致的结果,本人不负任何责任. 如果有任何翻译错误,以及意见与建议,请email本人.邮件地址:[email protected]. 转载请注

redis翻译_redis lua脚本

Available since 2.6.0.  加入版本2.6 Time complexity: Depends on the script that is executed. 时间复杂度: 取决于脚本的执行 出处:http://blog.csdn.net/column/details/redisbanli.html Introduction to EVAL  介绍EVAL EVAL and EVALSHA are used to evaluate scripts using the Lua i

lua 环境探索

什么是环境? http://www.lua.org/manual/5.1/manual.html#2.9 Besides metatables, objects of types thread, function, and userdata have another table associated with them, called their environment. Like metatables, environments are regular tables and multiple

使用Lua 局部变量来优化性能,同时比较局部变量和全局变量

在竞争激烈的游戏行业中,尤其页游,面对策划复杂和频繁的需求,使用脚本可以降低难度和成本.在使用Lua的过程中,会经常访问全局变量来作为配置文件. 在访问全局变量时,可以通过局部变量引用全局变量来优化.当然,这样的优化毫无意义. Locals Vs Globals  from  http://lua-users.org/wiki/LocalsVsGlobals Comparison between local and global variables: Implementation: Locals

使用Lua 局部变量来优化性能,同一时候比較局部变量和全局变量

在竞争激烈的游戏行业中,尤其页游,面对策划复杂和频繁的需求,使用脚本能够减少难度和成本.在使用Lua的过程中,会常常訪问全局变量来作为配置文件. 在訪问全局变量时,能够通过局部变量引用全局变量来优化.当然,这种优化毫无意义. Locals Vs Globals  from  http://lua-users.org/wiki/LocalsVsGlobals Comparison between local and global variables: Implementation: Locals