文章摘要
这篇文章介绍了Lua脚本中的一些基本功能和操作,主要围绕以下几个方面展开: 1. **函数定义与调用**:文章展示了如何定义函数并进行调用,例如`test_print`函数通过循环打印输入参数,调用`test`函数并传递字符串参数。 2. **表的操作**:文章详细描述了如何使用表(包括插入、删除和遍历操作)来存储和管理数据。例如,通过`table.insert`向表`g`中添加元素,并通过`table.remove`删除特定元素。 3. **字符串与表的结合操作**:文章演示了如何在表中存储字符串和数值,以及如何对表进行排序和遍历。 4. **错误处理与调试**:文章中还包含了一些调试功能,如使用`debug.traceback`进行错误定位,以及自定义错误处理函数。 5. **代码结构与运行**:文章整体展示了如何组织和运行一个完整的Lua脚本,包括函数定义、调用、数据操作以及对代码的调试与分析。 文章通过一系列代码示例,全面展示了Lua脚本开发中的一些典型操作和应用场景,具有较高的实用价值。
function test_print(…)
for i=1,select(“#”,…) do
print(i,select(i,…))
end
end
function test_print(…)
for i=1,select(“#”,…) do
print(i,select(i,…))
end
end
test_print(11,12,13,14)
print()
print(debug.traceback())
print()
function test(…)
for i=1,arg.n do
print(i..”\t”..arg[i])
end
end
test(“a”,2,34,234)
print()
g={}
table.insert(g,{
name=”Clairs”,
level=70,
})
table.insert(g,{
name=”SEGA”,
level=35,
})
table.insert(g,{
name=”Millber”,
level=50,
})
function myprint()
for i,v in ipairs(g) do
print(i,v[“level”],v.name)
end
end
myprint()
function comp(a,b)
return a.level<b.level
end
table.sort(g,comp)
print()
myprint()
print()
function foo(str)
if type(str) ~=”string” then
error(“string error!”,2)
end
end
–foo({x=1 })
tb1={ “asdf”,”bate”,”game”,one=”heihei”}
table.insert(tb1,3,”haha”)
table.remove(tb1,2)
for i,v in ipairs(tb1) do
print(v)
end
print(#tb1)
© 版权声明
文章版权归作者所有,未经允许请勿转载。