Search code examples
luainventoryitemstake

LUA: Looking for a specific table by its variable


I'm currently starting work on a text adventure game in Lua--no addons, just pure Lua for my first project. In essence, here is my problem; I'm trying to find out how I can do a "reverse lookup" of a table using one of its variables. Here's an example of what I've tried to do:

print("What are you trying to take?")
bag = {}
gold = {name="Gold",ap=3}
x = io.read("*l")
if x == "Gold" then
     table.insert(bag,gold)
     print("You took the " .. gold.name .. ".")
end

Obviously, writing a line like this with every single object in the game would be very... exhausting--especially since I think I'll be able to use this solution for not just taking items but movement from room to room using a reverse lookup with each room's (x,y) coordinates. Anyone have any ideas on how to make a more flexible system that can find a table by the player typing in one of its variables? Thanks in advance!

-blockchainporter


Solution

  • This doesn't directly answer your question as you asked it, but I think it would serve the purpose of what you are trying to do. I create a table called 'loot' which can hold many objects, and the player can place any of these in their 'bag' by typing the name.

    bag = {}
    loot = {
        {name="Gold", qty=3},
        {name="Axe", qty=1},
    }
    
    print("What are you trying to take?")
    x = io.read("*l")
    i = 1
    while loot[i] do
        if (x == loot[i].name) then
            table.insert(bag, table.remove(loot,i))
        else
            i = i + 1
        end
    end
    

    For bonus points, you could check 'bag' to see if the player has some of that item already and then just update the quantity...

    while loot[i] do
        if (x == loot[i].name) then
            j, found = 1, nil
            while bag[j] do
                if (x == bag[j].name) then
                    found = true
                    bag[j].qty = bag[j].qty + loot[i].qty
                    table.remove(loot,i)
                end
                j = j + 1
            end
            if (not found) then
                table.insert(bag, table.remove(loot,i))
            end
        else
            i = i + 1
        end
    end
    

    Again, this isn't a 'reverse lookup' solution like you asked for... but I think it is closer to what you are trying to do by letting a user choose to loot something.

    My disclaimer is that I don't use IO functions in my own lua usage, so I have to assume that your x = io.read("*l") is correct.


    PS. If you only ever want objects to have a name and qty, and never any other properties (like condition, enchantment, or whatever) then you could also simplify my solution by using key/val pairs:

    bag = {}
    loot = { ["Gold"] = 3, ["Axe"] = 1 }
    
    print("What are you trying to take?")
    x = io.read("*l")
    for name, qty in pairs(loot) do
        if x == name then
            bag.name = (bag.name or 0) + qty
            loot.name = nil
        end
    end