Table
首先, ..表示连接
Lua里面所有使用索引的地方都是从1开始的!
Table遍历:
For key,val int pairs(tab3) do
Print(key..”:”..val)
end
表内容赋值:
或者[]的形式
Tab1.key1 或者 Tab1[“key1”]
连接:
我们可以使用 concat() 方法来连接两个 table,
print( table.concat(mytable) )
print( table.concat(mytable,","))
print(table.concat(mytable,",",2,4) )
插入:
-- 在末尾插入
table.insert(fruits,"mango")
-- 在索引为 2 的键处插入
table.insert(fruits,2,"grapes")
例子:
排序:
table.sort(fruits)
获得长度:
mytable[#mytable+1]="PHP"
!!当我们获取 table 的长度的时候无论是使用 # 还是 table.getn 其都会在索引中断的地方停止计数,而导致无法正确取得table 的长度。
比如我们写:
这个行为未定义,有时候会出错
一般会自己实现:
循环
While循环
While(condition) do
Statements
End
For 循环
For i = 1,10,2 do
Statements
End
For k,v in pairs(tab1) do
Statements
End
Function 可以赋值以及作为参数
pairs和 ipairs
ipairs遇到nil就停止
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.
for k,v in pairs(t) do body end
will iterate over all key–value pairs of table t.
具体参考:https://blog.csdn.net/witch_soya/article/details/7556595
可以定义自己的迭代函数
MODULE
module.lua:
usemodule.lua:
-- require "模块名"
-- require ("模块名")
eg:
包路径设置:
RootDefine.lua中:
--protobuf和消息包处理的包含路径
package.path = package.path .. ";./Protobuf/?.lua;./Packet/?.lua;"
debugger.lua中:
ZBS = "E:/ZeroBraneStudioEduPack(1)/";
LuaPath = "E:/NewClientNMG/branches/uLuaClient/src/NMGClient/Assets/DJMain/BundleData/LuaScript/";
package.path =package.path..";./?.lua;"..ZBS.."lualibs/?/?.lua;"..ZBS.."lualibs/?.lua;"..LuaPath.."?.lua;"
关于module( ... ,package.seeall)
一般在一个Lua文件内以module函数开始定义一个包。module同时定义了一
个新的包的函数环境,以使在此包中定义的全局变量都在这个环境中,而非
使用包的函数的环境中。理解这一点非常关键。 “module(...,package.seeall)”
的意思是定义一个包,包的名字与定义包的文件的名字相同,并且在包的函数
环境里可以访问使用包的函数环境。