返回数组的 computercraft 函数,使用布尔值的第一个元素



编辑以获取更多详细信息:

我试图让一只坐在树苗前的等它长大后再把它砍下来。它将日志与前面的项目进行比较,直到匹配为止。我目前正在使用的系统可以工作,但我希望有一种稍微简约的方法来编写它。

checkTarget = {
forward = function(tgt)
check = {turtle.inspect()} --creates table with first as boolean, second as information table
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,--continued 

这需要一个参数(字符串或字符串数组(进行比较。当它检查前面的块时,它会将详细信息保存到 rtn 中的第二个元素,将第一个元素保存到默认值 false。如果字符串与选中块的名称匹配,则它将 rtn[1] 更改为 true 并返回所有内容,这是执行 checkTarget.forward("minecraft:log"( 时底部的表。

我的问题是,我目前正在创建一个一次性变量来存储从checkTarget返回的数组,然后调用变量的第一个元素以获取它是否为真。我希望有一种方法可以在没有一次性变量 (tempV( 的情况下将其包含在 if 语句中

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 = "birch", 
}, 
name = "minecraft:sapling", 
metadata = 2 
} 
}

而不是

local tempV = fox.checkTarget.forward("minecraft:log")
if tempV[1] then
end

你可以做

if fox.checkTarget.forward("minecraft:log")[1] then
end

然后调用变量的第一个元素以获取它是否为 true 或 不。

使用tempV[1],您不是在调用第一个元素,而是在索引它。

要调用某些内容,您必须使用调用运算符()因为布尔值是不可调用的,这没有意义。

最新更新