如何在Lua中从一个变量调用表格?

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。

1个回答

2

目前,你的是全局的,这对于这个例子来说是不好的。首先,将所有元素存储如下:

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与使用itemDetails.name作为第一个参数调用string.match相同。它可用于所有string函数。

所以这里的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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接