3

我正在为 ComputerCraft 中的海龟创建一个程序。该程序将使乌龟控制一个仓库,用于存放我在游戏中的物品。它会检查我放入了什么物品,然后它会找出箱子的位置,去那里,然后把它倒进去。我将每个箱子的位置存储在一个表中。例如:

cobblestone = {2,0,1}

这告诉乌龟鹅卵石箱子存放在 x=2 y=0 和 z=1 的位置。为了让海龟知道它需要存储什么,它会:

itemDetails = turtle.getItemDetail()
name = string.gsub(itemDetails.name, "minecraft:", "")

这让海龟获取项目的详细信息。然后它将一个变量设置为项目名称减去 minecraft: 在它的开头(我不能有一个名为“minecraft:cobblestone”的变量)。我不知道如何使用这个变量来调用表格并找到它的位置让乌龟去它。如果有人可以提供帮助,我将不胜感激。提前致谢!此外,请注意,代码仍然是为调试和测试目的而设置的。设置是一只乌龟,前面有一个输入箱,右边有一个燃料箱,后面有一个仓库。

我试过做:

cobblestone = {2,0,1}
--Putting a piece of cobblestone in--
itemDetails = turtle.getItemDetail()
--Returning name of "minecraft:cobblestone"--
name = string.gsub(itemDetails.name, "minecraft:", "")
print name[1]

到目前为止,这还没有奏效。

pos = {0,0,0}
looking = 0
cobblestone = {2,0,1}
function fuel()
    if turtle.getFuelLevel() < 20 then
        turtle.select(16)
        turtle.refuel(1)
    end
end
function left()
    turtle.turnLeft()
    looking = looking - 1
    if looking < 0 then
        looking = 3
    end
        print(looking)
end
function right()
    turtle.turnRight()
    looking = looking + 1
    if looking > 3 then
        looking = 0
    end
        print(looking)
end
function forward()
    fuel()
        if turtle.forward() then
            if looking == 0 then
                pos[1] = pos[1] - 1
            elseif looking == 1 then
                pos[3] = pos[3] - 1 
            elseif looking == 2 then
                pos[1] = pos[1] + 1
            elseif looking == 3 then
                pos[3] = pos[3] + 1
            else
                print("wot")
            end
        end

end
function up()
    fuel()
    turtle.up()
    pos[2] = pos[2] + 1
end
function down()
    fuel()
    turtle.down()
    pos[2] = pos[2] - 1
end
function goHome()
    while pos[3] > 0 do
        while  looking > 1 do
            left()
        end
        forward()
    end
    while  pos[2] > 0 do
        down()
    end
    while  pos[1] > 0 do
        while  looking > 0 do
            left()
        end
        forward()
    end
end

while true do
    turtle.select(1)
    while not turtle.suck() do
        sleep(1)
    end
    itemDetails = turtle.getItemDetail()
    name = string.gsub(itemDetails.name, "minecraft:", "")
    print(name)
        while looking < 2 or looking > 2 do
            left()
        end
        for i = pos[1],name[1]-1 do
            forward()
        end
        while looking > 3 or looking < 3 do
            right()
        end
        for i = pos[3],name[3]-1 do
            forward()
        end
        for i = pos[2],name[2]-1 do
            up()
        end
        while looking < 2 or looking > 2 do
            left()
        end
        turtle.select(1)
        turtle.drop()
        goHome()
end

我希望能够做到:

cobblestone = {2,0,1}
--Putting a piece of cobblestone in--
itemDetails = turtle.getItemDetail()
--Returning name of "minecraft:cobblestone"--
name = string.gsub(itemDetails.name, "minecraft:", "")
print name[1]

并让海龟打印与 cobblestone[1] 相同的内容,即 2。但是,它返回 nil。

4

1 回答 1

2

目前,你cobblestone是全球性的,这个例子有什么不好。首先,像这样存储所有元素:

local myelems = {
   cobblestone = {2,0,1};
   grass = {3,1,2};
}

等等。尽可能使用local变量,这完全是编程的好习惯。

所以让我们看看你的代码:

-- Returning name of "minecraft:cobblestone"
local name = string.gsub(itemDetails.name, "minecraft:", "")
print(name[1])

它能做什么?(我稍微改了一下)

起初,string.gsub在这里看起来有点矫枉过正,请string.match改用:

local name = itemDetails.name:match("^minecraft:(.+)$")

这与使用第一个参数itemDetails.name:match调用相同。它可以用于所有功能。string.matchitemDetails.namestring

所以这里name字符串变量。不同语言的索引字符串可能有不同的结果。在 lua 中,实际上提供了对所有string函数的访问,以便更轻松地调用它们(如上所述)。没有这样的功能string[1],所以你得到nil。但是,现在我们有一个表myelems,其中键是字符串,值是您的表。

完整代码:

-- Table for all elements
local myelems = {
   cobblestone = {2,0,1};
   grass = {3,1,2};
}
-- Putting a piece of cobblestone in
local itemDetails = turtle.getItemDetail()
--Returning name of "minecraft:cobblestone"
local name = itemDetails.name:match("^minecraft:(.+)$")
-- Get our table
local elem = myelems[name]
if elem then
  print(elem[1], elem[2], elem[3])
end
于 2018-12-23T10:54:15.013 回答