如何解决在Lua中使用'if语句多条件'的问题
2019-5-13 7:44:11
收藏:0
阅读:129
评论:1
我在计算机工艺中使用Lua来自动挖掘矿藏。但是,我的掘地龟以前程序一直运行得很好,如果遇到'熔岩/流动的熔岩/水/流动的水源'就会停止。
在我的程序中,我有很多管理函数,例如燃料管理、隧道、与沙子的碰撞等等。还有一个检测掘地龟是否遇到'方块'的函数。
如果方块只是一个空气块,掘地龟就会继续前进。否则,掘地龟会挖掉这个方块,并且如果前面还有方块的话就不会向前移动。
问题是什么?前面提到的四个源都被视为方块,掘地龟无法向前移动。
我尝试使用多条件来修复这个问题,但它不起作用,掘地龟会向任何方向移动和挖掉方块。
所以我认为我的if语句的创建方式不好,可能是语法(拼接许多或者符号成为())。
如何解决这个问题?
function blockDetection(position,justDetection)
success,detectionBlock = nil
block_name = ""
if position == "right" then
turtle.turnRight()
success,detectionBlock = turtle.inspect()
turtle.turnLeft()
if success then
block_name = detectionBlock.name
if justDetection == true and detectionBlock.name == "minecraft:air" then
block_name = true
elseif justDetection == true and detectionBlock.name ~= "minecraft:air" then
block_name = false
else
end
end
end
end
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- Lua 虚拟机加密load(string.dump(function)) 后执行失败问题如何解决
- 我想创建一个 Nginx 规则,禁止访问
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?

我认为你的问题在于你忘记了返回
block_name。如果省略return语句,则会隐式返回0个参数,因此尝试访问任何参数都会得到nil值。例如,if blockDetection(position,justDetetcion) then --[[something]] end,由于nil被认为是假,因此永远不会执行then-end块。你还应该更改代码中的另一件事: 你不应该使用
x==true。如果x是布尔值,则if x==true then ...等同于if x then ...。因此,你的代码应该像这样:
function blockDetection(position, justDetection) success, detectionBlock = nil block_name = "" if position == "right" then turtle.turnRight() success, detectionBlock = turtle.inspect() turtle.turnLeft() if success then block_name = detectionBlock.name if justDetection and detectionBlock.name == "minecraft:air" then block_name = true elseif justDetection and detectionBlock.name ~= "minecraft:air" then block_name = false else end end end return block_name end