aa.lua
teacher = "guilinwang";
org = "nzhsoft";
object = "lua";
age = 28;
tab = {
teacher = "guilinwang_tab",
org = "nzhsoft_tab",
object = "lua_tab",
age = 38,
func_tab = function (num,string)
print("number=",num);
print("string=",string);
return 1,2,3,4,5;
end
}
function func()
print("hello lua!!!");
end
function func2(num,string)
print("number=",num);
print("string=",string);
end
function func3(num,string)
print("number=",num);
print("string=",string);
return 1,2,3,4,5;
end
c/c++访问lua全局变量
1、int lua_getglobal (lua_State *L, const char *name); 使用lua_getglobal() 把name成员压入到栈,位置是在-1
2、使用lua_tonumber(L,-1) 从栈中得到lua中name变量的值
#include
#include
#include
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
int main(){
lua_State* L = luaL_newstate(); //创建一个lua虚拟机(lua上下文),类似于指针
luaL_openlibs(L);
luaL_dofile(L, "aa.lua"); //把lua文件加载到虚拟机,并编译运行
lua_getglobal(L, "teacher"); //idx=1
lua_getglobal(L, "org"); //idx=2
lua_getglobal(L, "object"); //idx=3
lua_getglobal(L, "age"); //idx=4
栈内存储的数据为
age,object,org,teacher
if (lua_isnumber(L, -1)){
int num = lua_tonumber(L, -1);
printf("num = %d\n", num);
}
lua_pop(L, 1); //弹出栈顶元素,这样第二个object就是-1索引
if (lua_isstring(L, -1)){
char* str = lua_tostring(L, -1);
printf("str = %s\n", str);
}
lua_close(L);
system("pause");
return 0;
}
###############################访问lua全局table##################################
lua_getglobal(L, "tab"); //idx = -2 || 1
lua_pushstring(L, "teacher"); //idx = -1 || 2 //先把key push到栈上去
lua_gettable(L, -2); //这里会把索引2的数据"teacher"弹出,把数据value放在索引2,原来'teacher'的位置
/*
1、先push key 2、再gettable(L,-2) tab在栈中的索引
*/
if (lua_isstring(L, -1))
{
char* str = (char*)lua_tostring(L, -1);
printf("str = %s\n", str);
}
lua_pop(L,1); //弹出栈顶1个元素,就是弹出栈顶
lua_pushstring(L, "age");
lua_gettable(L, -2);
if (lua_isnumber(L, -1))
{
int num = lua_isnumber(L, -1);
printf("num = %d\n", num);
}
#include
#include
#include
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
int main(){
lua_State* L = luaL_newstate(); //创建一个lua虚拟机(lua上下文),类似于指针
luaL_openlibs(L);
luaL_dofile(L, "aa.lua"); //把lua文件加载到虚拟机,并编译运行
//有参无返回
lua_getglobal(L, "func");
lua_pcall(L, 0, 0, 0); //执行完成后会清理栈
//有参无返回
lua_getglobal(L, "func2");
lua_pushnumber(L, 101);
lua_pushstring(L, "fangfang"); //栈 1:func,2:101,3:"fangfang"
lua_pcall(L, 2, 0, 0); //参数二:n个参数, 参数三: n个返回值 ,参数四:通常为0
//有参有返回
lua_getglobal(L, "func3");
lua_pushnumber(L, 101);
lua_pushstring(L, "fangfang"); //栈 1:func,2:101,3:"fangfang"
lua_pcall(L, 2, 5, 0); //函数有2个参数,5个返回值
printf("1->%g\n", lua_tonumber(L, -1)); //%g 用tonumber 5
printf("2->%d\n", lua_tointeger(L, -2)); //%d 用tointerger 4
printf("3->%d\n", lua_tointeger(L, -3)); // 3
lua_close(L);
system("pause");
return 0;
}
#include
#include
#include
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
int main(){
lua_State* L = luaL_newstate(); //创建一个lua虚拟机(lua上下文),类似于指针
luaL_openlibs(L);
luaL_dofile(L, "aa.lua"); //把lua文件加载到虚拟机,并编译运行
lua_getglobal(L, "tab"); //tab idx = -1
lua_getfield(L, -1, "func_tab"); //func_tab idx = -1 ,tab idx = -2;
lua_remove(L, -2); //把tab移除栈,函数只有在栈底(1)才可以执行
lua_pushnumber(L, 101);
lua_pushstring(L, "fangfang"); //栈 1:func_tab,2:101,3:"fangfang"
//stackDump(L);
lua_pcall(L, 2, 5, 0); //函数有2个参数,5个返回值
//lua返回了 1,2,3,4,5
printf("1->%g\n", lua_tonumber(L, -1)); //%g 用tonumber 5
printf("2->%d\n", lua_tointeger(L, -2)); //%d 用tointerger 4
printf("3->%d\n", lua_tointeger(L, -3)); // 3
lua_close(L);
system("pause");
return 0;
}
#include
#include
#include
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
int main(){
lua_State* L = luaL_newstate(); //创建一个lua虚拟机(lua上下文),类似于指针
luaL_openlibs(L);
luaL_dofile(L, "aa.lua"); //把lua文件加载到虚拟机,并编译运行
lua_getglobal(L, "tab");
lua_getfield(L, -1, "teacher");
if (lua_isstring(L, -1))
{
char* str = (char*)lua_tostring(L, -1);
printf("str = %s\n", str);
}
lua_pop(L, 1);
lua_getfield(L, -1, "age");
if (lua_isnumber(L, -1))
{
int num = lua_isnumber(L, -1);
printf("num = %d\n", num);
}
lua_close(L);
system("pause");
return 0;
}