1

So I am trying to capture a list of items in a store and then store it myself in a table. I got the capture going, and it now captures 'name' for the item name, 'type' for the item type and 'amount' which is a number of the amount, i.e. name = 'bag', type = 'clothing' and amount = '3'. I want to add that to an array so I can have this capture all the items in the store into that array, how would I do that? Also, how can I have it later do something like 'list bags' and it will list everything that matched sword? THank you.

1 Answer 1

1

"Array" is a foreign concept but you can do a lot with a table.

Here is a table with names as the key, which you can use if the names are unique.

name = "bag1"
type = "sword"
amount = 3


store = store or {} -- Lua idiom for initializing a variable to an empty table 
                    -- if it doesn't already have a value
store[name] = { type = type, amount = amount }


for name, item in pairs(store) do
    if item.type == "sword" then
        print(name, item.type, item.amount)
    end 
end

Output:

bag1    sword   3

I suggest that use a number of the amount so you have numeric operations available for it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.