Lua脚本学习实例
运算符
- 基本运算:
支持加 (+)、减(-)、乘(*)、除(/) 等基本数学计算。
local i=10print(i) -- 日志打印:10i=(i+1)*2print(i) -- 日志打印:22例子:
local position_1= Engine.Vector(0, 0,0) --定义了一个(0,0,0,)的坐标local position_2= Engine.Vector(100, 50,0)local midpoint=(position_1+position_2)/2 --计算得出两个坐标的中点local midpoint_X=midpoint.X --分别获取坐标中的X、Y、Zlocal midpoint_Y=midpoint.Ylocal midpoint_Z=midpoint.Zprint("坐标中点为 X:"..tostring(midpoint_X).." Y:"..tostring(midpoint_Y).." Z:"..tostring(midpoint_Z))-- 日志打印:坐标中点为 X:50 Y:25 Z:0- 如果有使用过元梦扣叮,那么该操作类似于下面这样:
- 关系运算符:
与元梦扣叮的类似,支持等于(==)、不等于(~=)、大于(>)、小于(<)、大于等于(>=)、小于等于(<=)。
local temperature = 28print(temperature > 25) -- 日志打印:true- 如果有使用过元梦扣叮,那么该操作类似于下面这样:
- 逻辑运算符:
与元梦扣叮的条件判断类似,支持与(and)、或(or)、非(not)。
local is_day = truelocal is_sunny = false
print(is_day and is_sunny) -- 日志打印:falseprint(is_day or is_sunny) -- 日志打印:trueprint(not is_day) -- 日志打印:false- 如果有使用过元梦扣叮,那么该操作类似于下面这样:
流程控制
- 条件判断:
| 逻辑 | 语句 | 元梦扣叮中使用 |
|---|---|---|
| 如果 就 | if true then —执行语句 end | ![]() |
| 如果 就 否则 | if true then —执行语句 else —执行语句 end | ![]() |
| 如果 就 否则如果 就 否则如果 就 | if true then —执行语句 elseif true then —执行语句 elseif true then —执行语句 end | ![]() |
local is_weekend = false--可以试着修改is_weekend和temperature值来观察打印结果local temperature = 28
if not is_weekend then if temperature > 30 then print("工作日高温警告!") elseif temperature < 25 then print("注意添加衣物") else print("今日天气适宜") endelse print("周末休息日")end- 如果有使用过元梦扣叮,那么该操作类似于下面这样:
- 循环及循环控制:
| 逻辑 | 语句 | 元梦扣叮中使用 |
|---|---|---|
| 只要 就重复执行 | while true do —执行语句 end 只要true,就重复执行 | |
| 重复执行x次 | for i = 1, 10 do —执行语句 end 重复执行10次,其中i是循环次数 | ![]() |
| 遍历 | for key, value in pairs(t) do —执行语句 end t为表,key为选取的编号,value为数据的值(这里指只列举了for循环最常用的两个方法,其他方法可自行查找) | ![]() |
| 重复执行 直到 | repeat —执行语句 until true 重复执行,直到true | |
| 跳出循环 | break | ![]() |
local connection_attempts = 0print("尝试连接智能灯泡...")while true do -- 无限循环 connection_attempts = connection_attempts + 1 print("第"..connection_attempts.."次尝试连接")
-- 模拟随机连接成功 if math.random(1,5) == 3 then print("连接成功!") break end
-- 最多尝试5次 if connection_attempts >=5 then print("连接失败,请检查设备") break endend- 如果有使用过元梦扣叮,那么该操作类似于下面这样:
local water_temp = 25 -- 初始水温(摄氏度)local attempts = 0 -- 加热尝试次数
print("开始烧水...")repeat attempts = attempts + 1 -- 模拟加热过程 water_temp = water_temp + math.random(15,25) print(string.format("第%d次测温:%d℃", attempts, water_temp)) --用string的format函数格式化字符串打印
-- 安全保护(即使没烧开也最多加热5次) if attempts >=5 then print("加热超时,请检查设备") break enduntil water_temp >= 100
if water_temp >= 100 then print("水已烧开,可冲泡饮品")end- 元梦扣叮中没有类似 repeat…until 的流程控制方法,使用以下元梦扣叮可以实现类似功能,但逻辑并不完全一致,仅供参考:
函数
-
Lua中函数主要有两种类型:
a) 执行某些任务(元梦扣叮中常规函数)。
b) 计算某些值并返回(元梦扣叮中的取值函数)。
--常规函数格式local function 函数名(参数列表) --参数列表,函数体内可以获取到的变量,也可以没有参数 -- ↓ 函数体开始 -- 函数体中编写希望执行的操作 -- ↑ 函数体结束end
函数名(参数列表) --调用函数
--取值函数格式local function 函数名(参数列表) -- ↓ 函数体开始 -- 函数体中编写希望执行的操作 return 返回值 -- ↑ 函数体结束end
local tmp=函数名(参数列表) --使一个变量等于函数的返回值- 如果有使用过元梦扣叮,那么该操作类似于下面这样:
- 下面我们来定义一个简单的函数。
-- 定义简单的函数local function SayHello() print("你好,欢迎来到Lua的世界!")end-- 调用SayHello()
-- 定义带参数的函数local function ShowItem(item_name, num) --Lua在定义函数时,可以不用确定参数类型,但也可以ShowItem(string item_name,number num) print("当前商品:"..item_name) print("剩余库存:"..num)end-- 调用ShowItem("铅笔", 20) --日志打印:当前商品:铅笔 剩余库存:20- 如果有使用过元梦扣叮,那么该操作类似于下面这样:
-- 定义返回值函数local function checkStock(item) local stock = math.random(0, 10) -- 模拟库存 return stock > 0 --返回一个布尔值,表示是否库存end
if checkStock("铅笔") then print("铅笔有库存")else print("铅笔已售罄")end- 如果有使用过元梦扣叮,那么该操作类似于下面这样:
表
表基础与定义:
Lua中的表(table),类似于元梦扣叮中的变量组(布尔值组、数值组等)。
local tab_1={} --创建了一个空的表print(type(tab_1)) --日志打印:table
local tab_2={"星宝","易斑斑","虎子哥","好好鸭","紫萝萝"}print(tab_2[1]) --日志打印:星宝 在表后面,用方括号+索引,可以取得对应序号的元素print(tab_2[2]) --日志打印:易斑斑print(tab_2[5]) --日志打印:紫萝萝print(tab_2[6]) --日志打印:nil或者无内容 tab_2中没有索引为6的元素,所以取不到,就会返回nil啦,nil表示没有、空。print(type(tab_2[6])) --日志打印:niltab_2[6]="玫珊珊" --把tab_2中索引为 6 的值设置为"玫珊珊"print(tab_2[6]) --日志打印:玫珊珊- 如果有使用过元梦扣叮,那么该操作类似于下面这样:


索引与元素:
索引值对应的元素可以修改,也可以新增元素。
-- 例:水果货架(默认为数字索引)local fruits = {"苹果", "香蕉", "橘子"}
-- 基础访问print("第一种水果:"..fruits[1]) -- 日志打印:第一种水果:苹果
-- 修改元素后访问fruits[2] = "芒果" -- 修改元素print("修改后的第二种水果:"..fruits[2]) -- 日志打印:修改后的第二种水果:芒果
-- 动态扩展fruits[4] = "草莓"print("新增第四种水果:"..fruits[4]) -- 日志打印:新增第四种水果:草莓元素数据类型:
与元梦扣叮有所不同的是,表中的元素可以是任意数据类型。
-- 举例:定义一个包含不同数据类型的商品local product = { "卡通橡皮", -- 字符串 5, -- 数字 true -- 布尔值}
print("商品名称:"..product[1]) -- 日志打印:商品名称:卡通橡皮print("库存数量:"..product[2]) -- 日志打印:库存数量:5print("是否热销:"..tostring(product[3])) -- 日志打印:是否热销:true表的嵌套:
在Lua中,表,也可以作为表的元素,作为元素的表可以称为子表,或者嵌套表。
-- 元素:文具店货架(主表)local shelf = { { -- 第一个商品(子表) "铅笔", -- 名称 20, -- 库存量 false -- 是否热销 }, { -- 第二个商品(子表) "笔记本", 15, true }}
-- 访问嵌套表数据print("第一层货架商品:"..shelf[1][1]) -- 日志打印:铅笔 [1]获取第一个元素,是一个子表,再次用[1]获取子表的第一个元素,"铅笔"print("第二件商品库存:"..shelf[2][2]) -- 日志打印:15自定义索引:
Lua中,表的索引值可以自行定义,可以是数字或者是字符串。
-- 数字索引(默认方式)local fruits = {"苹果", "香蕉", "橘子"}print(fruits[1]) -- 日志打印:苹果(索引从1开始)
-- 字符串索引local product = { name = "牛奶", -- 等价于 ["name"] = "牛奶" price = 6.5, ["生产日期"] = "2023-08-20" -- 含中文或空格必须用方括号}
print(product.name) -- 日志打印:牛奶print(product["生产日期"]) -- 日志打印:2023-08-20
-- 混合索引(数字+字符串)local student = { "张小萌", -- 索引1(数字) age = 12, -- 索引"age"(字符串) [3] = "四年级", -- 显式指定数字索引3 ["home room"] = "A班" -- 含空格的字符串索引}
print(student[1].."的年龄:"..student.age) -- 日志打印:张小萌的年龄:12print(student["home room"]) -- 日志打印:A班







