How do I set the next value of a table in lua?

Viewed 46

I'm making a text game, and in this game you have an inventory. You search for items, and I have a function set to search for items. If someone already has an item, then how do I set the next item to whatever they found.

To make it simple, what I want is

inv = {}
if "player finds item" then
inv[nextvalue] = founditem
-- not the actual code

But I don't know how to get the set the nextvalue. Please help!

2 Answers

The question is not very clear, but I assume you want to use a Lua idom to append a value to a table:

inv = {}
if "player finds item" then
  inv[#inv+1] = founditem
end

You could also use the table.insert function.

inv = {}
if "player finds item" then
  table.insert(inv, founditem)
end

You need table.insert

-- table.insert(table,pos,element)
local inv = {}
if "player finds item" then
    table.insert(inv, nextvalue, founditem)
end

See document

Related