1. 入栈操作
lua_pushstring(L, "xx");//index为1
lua_pushnumber(L,20);//index为2,number可以视为string
2. 出栈操作
lua_tostring(L,1)//取出index为1的值
lua_tostring(L,-1)//取出最后一个入栈的元素
3. table(实现原理为数组或者是hash表)
table有点像数组或者是字典。里面的每一项是key,value键值对。key可以是字符串也可以是序号。
local a = {["x"] = 12, [3] = "hello"}表示字符串key为x的value是12,index为3的value是hello。当key是字符串的时候可以去掉[],简写为local a = {x= 12, [3] = "hello"},注意去掉[]的时候不能加引号,即不能写为local a = {"x"= 12}。a["x"]或a.x表示取12。a[3]表示取hello。如果没有key,那么自动用数字key,且下标从1自动排序local a = {12}。
4. lua调用c++。
首先定义c++函数
static int average(lua_State *L)
{
/* 获取参数 */
//int num = lua_tonumber(pL, 1);//可能多个
/* 压入返回值 */
//lua_pushnumber(L, sum);//可能多个
/* 返回返回值的个数 */
return 2;
}
注册函数
lua_register(L, "average", average);
lua调用
avg, sum = average(10, 20, 30, 40, 50)
5. c++调lua
//定义lua文件add.lua
function add(x, y)
return x + y
end
//c++调用lua
lua_State *L;
int luaAdd(int x, int y)
{
int sum;
lua_getglobal(L, "add");
lua_pushnumber(L, x);
lua_pushnumber(L, y);
lua_call(L, 2, 1);
sum = (int)lua_tonumber(L, -1);
lua_pop(L, 1);
return sum;
}
luaL_dofile(L, "add.lua");
lua_pcall(L, 0, LUA_MULTRET, 0);
sum = luaAdd(10, 15);
printf("The sum is %d\n", sum);
6. lua_tinker:对lua简单的封装。
lua_tinker::decl(L, "def_game_id", game_id);//应该是往lua定义全局变量def_game_id=game_id;
lua_tinker::def(L, "reg_db_dispatcher", reg_db_dispatcher);//将c++函数注册到lua
7.protobuf的lua文件
local pb = require "protobuf"
pb.register_file("../pb/common_enum_define.proto")//注册消息体。
7. lua继承
-- 基类--------------------------start
base_character = {}
-- 创建
function base_character:new()
local o = {}
setmetatable(o, {__index = self}) //设置原表
return o
end
function base_character:Name()
print("table base");
end
-- 基类--------------------------end
-- 子类--------------------------start
player = base_character:new();
player.Name(); //输出:table base
//重写方法
function player:Name()
print("table player")
end
player.Name()//输出:table player
-- 子类-------------------------end
8.lua迭代器
标准库提供了集中迭代器,包括迭代文件每行的(io.lines),迭代table元素的(pairs),迭代数组元素的(ipairs),迭代字符串中单词的 (string.gmatch)等等。
for i,v in ipairs(t) do body end
will iterate over the pairs (1,t[1]), (2,t[2]), ···, up to the first integer key absent from the table.
9.math.randomseed(tostring(os.time()):reverse():sub(1, 6)) //随机数种子
10.Lua 生成随机数需要用到两个函数:
math.randomseed(xx), math.random([n [, m]])
math.randomseed(n) 接收一个整数 n 作为随机序列种子。
math.random([n [, m]]) 有三种用法: 无参调用, 产生 (0,1) 之间的浮点随机数; 只有参数 n, 产生 1-n 之间的整数; 有两个参数 n, m, 产生 n-m 之间的随机整数
11.字符串连接。local str = tostring(变量1)..tostring(变量2)。其中变量不能为nil
12.#操作符。可以获取最大数字键值。当全为自增数字键的时候可以用于获取数组长度。
13.赋值:r, k = k, r可以同时赋几个数的值。函数也可以返回多个值。
function fuc()
return i, j
end
14. _G。在Lua脚本层,Lua将所有的全局变量保存在一个常规的table中,这个table被称为全局环境,并且将这个table保存在一个全局变量_G中,也就是说在脚本中可以用_G获取这个全局table,并且有_G._G == _G
15.load方法:
local b = 200
local funtemp = load("return b")//load方法相当于返回一个函数,函数体就是load的字符串内容
local bvalue = funtemp()
相当于
function funtemp()
return b
end
16.使用sublime text编辑lua,设置Tool->Build System为lua,然后ctrl+b就可以编译lua。
17.self关键字,相当于this指针,在函数调用的时候会传递。local定义局部变量
18.lua for循环
for var=exp1,exp2,exp3 do
<执行体>
end
var从exp1变化到exp2,每次变化以exp3为步长递增var,并执行一次“执行体”。exp3是可选的,如果不指定,默认为1。
19.ipairs和pairs区别
local testTab = {}
testTab[2] = {}
testTab[3] = {}
for key, value in pairs(testTab) do --只能用pairs遍历,不能用ipairs遍历
XXX
end
20.函数参数
function test( p1,p2 )
print("p1--------------->",p1)
end
test(1)//会调用成功p1为1 p2为nil
21.函数调用“:”和“.”的区别。:默认会传递self(this指针),而 . 相当于静态方法。
22.if not a then //a为false或者nil
do...
end
23.多次require一个文件,只会执行一次。import() 与 require() 区别在于,require会从绝对路径查找,而import可以从相对路径查找。