• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua Get every item in backpack

Scarlet Ayleid

Advanced OT User
Senator
Joined
Jul 7, 2007
Messages
4,049
Reaction score
238
Hey :)


I'm doing a spell that hits once per every weapon the player has in his backpack(and sub-backpacks), doing the spell itself is not the problem, the problem is how to get every item the player has in his backpack.


Thanks for the help :)
 
Some time ago, Ratser and I, managed to loop through the first backpack, by this:
Lua:
for i = 0, getContainerSize(getPlayerSlotItem(cid, CONST_SLOT_BACKPACK).uid) do
local thing = getContainerItem(getPlayerSlotItem(cid, CONST_SLOT_BACKPACK).uid, i)
end

Looping another backpack, I woldn't know how to do it, try to ask Ratser, I sent him a PM where I gave him an idea of how to do it, but I don't have it, and now I don't remember what I thought ;/
Good luck :)

EDIT: I guess you understand, thing will be the item
thing.itemid == xxxx
 
@Santi
thanks for the info, and I understood it, I'm no noob :)


so, to get every item, couldnt it be something like this:

Code:
function getAllContainerItems(item)
  if not isContainer(item.uid) then
      return nil
  end
  local table = {}
  for i = 0, getContainerSize(item.uid) do
    local thing = getContainerItem(item.uid, i)
    if thing.itemid == 0 then
        break --Backpack has no more items
    end
    table[#table + 1] = thing
    if isContainer(thing.uid) then
        local table2 = getAllContainerItems(thing)
        table = table.merge(table, table2) --added below
    end
  end
  return table
end
Code:
table.merge = function (table, table2)
    for i=1, #table2, 1 do
        table[#table + 1] = table2[i]
    end
    return table
end
can't see another way to do it, recursion is the only way, right?

@Edit, just tested, fixed and working :D
 
Last edited:
Back
Top