如何从 Lua 中的变量调用 Table?

How to Call a Table from a Variable in Lua?

我正在 ComputerCraft 中为海龟创建程序。该程序将使 turtle control 成为我在游戏中的物品的存储仓库。它会检查我放入的物品,然后它会找出箱子的位置,去那里,然后把它倒进去。我将每个箱子的位置存储在 table 中。例如:

cobblestone = {2,0,1}

这告诉乌龟鹅卵石箱存储在位置 x=2 y=0 和 z=1。为了让乌龟说出它需要存储什么,它做了:

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

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

我试过:

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]

并让乌龟打印与 鹅卵石[1] 这将是 2。但是,它 returns 为 nil。

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

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

完整代码:

-- 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