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

时间:2018-12-23 04:18:56

标签: lua computercraft

我正在ComputerCraft中为乌龟创建程序。该程序将使乌龟控制游戏中我的物品的存储仓库。它将检查我放入的物品,然后找出箱子的位置,移到那里并将其倾倒。我将每个箱子的位置存储在桌子中。例如:

cobblestone = {2,0,1}

这告诉乌龟,鹅卵石的箱子存放在x = 2 y = 0和z = 1的位置。要使乌龟知道需要存储什么,它可以做到:

itemDetails = turtle.getItemDetail()
name = string.gsub(itemDetails.name, "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]

并让乌龟打印与     鹅卵石[1] 应该是2。但是,它返回nil。

1 个答案:

答案 0 :(得分:1)

当前,您的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在这里看起来像是一种矫kill过正,请改用string.match

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

这里itemDetails.name:match与以string.match作为第一个参数调用itemDetails.name相同。它可以用于所有string函数。

因此,name string 变量。用不同语言索引字符串可能会产生不同的结果。在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
相关问题