Computercraft 返回一个数组的函数,使用第一个元素作为布尔值
2019-12-14 21:14:20
收藏:0
阅读:99
评论:1
编辑以获得更多详细信息:
我试图让一只站在树苗前面的海龟等待树苗长大后再砍下它。它将对比前面的物品和树干,直到它们匹配。我当前正在使用的系统可以工作,但我希望有一种稍微更简洁的方法来编写它。
checkTarget = {
forward = function(tgt)
check = {turtle.inspect()} --创建第一个元素为布尔值,第二个元素为信息表的表
local rtn = {false, check[2]}
if type(tgt) == "table" then
for k, v in pairs(tgt) do
if check[2].name == v then
rtn = {true, v}
break
end
end
elseif tgt == nil then
return check[1]
elseif check[2].name == tgt then
rtn[1] = true
end
return rtn
end,--继续
这个函数接受一个参数,可以是字符串或字符串数组,用于进行对比。当它检查前面的方块时,将详细信息保存在 rtn 的第二个元素中,并将第一个元素默认设置为 false。如果该字符串与已检查方块的名称相匹配,则将 rtn[1] 更改为 true 并返回整个 rtn,这是在执行 checkTarget.forward("minecraft:log") 时底部的表格。
我的问题是,我当前正在创建一个用于存储从 checkTarget 返回的数组的临时变量,并调用变量的第一个元素来获取它是否为 true。我希望在 if 语句中不使用临时变量 (tempV) 的方法。
repeat
local tempV = fox.checkTarget.forward("minecraft:log")
if tempV[1] then
cut()
fox.goTo({x = 0, y = 0, z = 0})
fox.face(0)
end
tempV = fox.checkTarget.forward("minecraft:log")
until not run
{
false,
{
state = {
stage = 0,
type = "桦木",
},
name = "minecraft:sapling",
metadata = 2
}
}
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- 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 代码?

将下面翻译成中文并且保留原本的 markdown 格式,
Instead oflocal tempV = fox.checkTarget.forward("minecraft:log") if tempV[1] then end
You can doif fox.checkTarget.forward("minecraft:log")[1] then end
> and then calling the variable's first element to get if it's true or > not. With `tempV[1]` you're not calling the first element, you're indexing it. To call something you have to use the call operator `()` which doesn't make sense as a boolean is not callable.翻译:
local tempV = fox.checkTarget.forward("minecraft:log") if tempV[1] then end
if fox.checkTarget.forward("minecraft:log")[1] then end
```
使用
tempV[1],你不是在调用第一个元素,而是在索引第一个元素。要调用东西,必须使用调用运算符
(),但将布尔值用作参数是不合理的。