luacomputercraft

How to Call a Table from a Variable in Lua?


I'm creating a program for a turtle in ComputerCraft. The program is going to make the turtle control a warehouse of storage for my items in the game. It will check what item I put in, then it will figure out the chest's location, go there, and dump it in. I am storing the locations of each chest in a table. For example:

cobblestone = {2,0,1}

That tells the turtle that the cobblestone chest is stored at position x=2 y=0 and z=1. To get the turtle to tell what it needs to store, it does:

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

This gets the turtle to get the details of the item. It then sets a variable to be the name of the item minus the minecraft: at the beginning of it (I can't have a variable called "minecraft:cobblestone"). I don't know how to use this variable to call the table and find its position for the turtle to go to it. If anybody can help, I would appreciate any of the help. Thanks in advance! Also, just a heads up, the code is still set up for debugging and testing purposes. The setup is a turtle, with an input chest in front of it, a fuel chest to the right, and the warehouse behind it.

I've tried doing:

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]

So far this hasn't worked.

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

I want to be able to do:

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]

and have the turtle print the same thing as cobblestone[1] which would be 2. However, it returns with nil.


Solution

  • Currently, your cobblestone is global, what is bad for this example. At first, store all elements like this:

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

    and so on. Use local variables when you can, it's good practice in programming at all.

    So let's look at your code:

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

    What it does? (I've changed it a bit)

    At first,string.gsub look like an overkill here, use string.match instead:

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

    Here itemDetails.name:match is same as calling string.match with itemDetails.name as first argument. It can be used for all string functions.

    So here name is string variable. Indexing strings in different languages may have different results. In lua in actually provide access to all string functions to make calls to them easier (as above). There is no such function as string[1], so you get nil. However, now we have table myelems in which keys are strings and values are your tables.

    Full code:

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