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

getPlayerItems(cid)

leyendario

kind of scripter
Joined
Aug 1, 2009
Messages
49
Reaction score
14
Location
Venezuela
this is a function that returns a table with all items that the player currently have. it doesn't count depot.
Code:
function getPlayerItems(cid)
    local items = {}
    local function checkContainer(uid)
        local container = getContainerSize(uid) - 1
        for a = 0, container do
            local item = getContainerItem(uid,a)
            if item.uid > 0 then
                table.insert(items,item.uid)
                if isContainer(item.uid) then
                    checkContainer(item.uid)
                end
            end
        end
    end
    local player = getThing(cid)
    if isPlayer(cid) then
        for i = 0, 10 do
            local slot = getPlayerSlotItem(cid,i)
            if slot.uid > 0 then
                table.insert(items,slot.uid)
                if isContainer(slot.uid) then
                    checkContainer(slot.uid)
                end
            end
        end
        return items
    end
    return false
end

use:
Code:
local items = getPlayerItems(cid)
if items then
    for _, item in ipairs(items)
        ...
    end
else
    ...
end
 
Last edited:

Ejemplo:
Bulto es de 20 celdas, cuando usas 'getContainerSize(uid)' el devuelve el valor actual(20), debido a eso, cuando usas 'for a = 0, container do' el numbero '0' cuenta como una celda(es la primera celda de un bulto o contenedor), el total de celdas que va a estar dando loop va ser 21 y no 20.
 
the function 'checkContainer' may return an error because on the line: local item = getContainerItem(uid, a)
The value a, can return a number more big than 10, 10 is the last value slot,
 
the function 'checkContainer' may return an error because on the line: local item = getContainerItem(uid, a)
The value a, can return a number more big than 10, 10 is the last value slot,

Re-Quote my quote:
Code:
container - 1
 
So like this:

Code:
function getPlayerItems(cid)
    local items, player = {}, getThing(cid) ; if not (isPlayer(cid)) then return false ; 
    local function checkContainer(uid)
        local container = (getContainerSize(uid) - 1) ; for a = 0, container do
            local item = getContainerItem(uid, a) ; if item.uid > 0 then
                table.insert(items, item.uid) ; if isContainer(item.uid) then checkContainer(item.uid) ; end ; 
            end ; 
        end ; 
    end ; 
    for i = 0, 10 do
        local slot = getPlayerSlotItem(cid, i) ; 
        if slot.uid > 0 then table.insert(items, slot.uid) ; 
            if isContainer(slot.uid) then checkContainer(slot.uid) ; end ; 
        end ; 
    end ; 
    return items ; 
end ; [FONT=courier new]
[/FONT]
 
tried with all kind of containers and i didnt give any error. anyways it was looping 21 slots instead of 20 as Shinmaru said. i fixed that...
 
So now the difference between yours and mine are limited to my habitual anal retentive and superior* coding style. Basically the first line is the only imperative change I'd make to yours:

local items, player = {}, getThing(cid) ; if not (isPlayer(cid)) then return false ;

that way you have less nesting and don't waste 14 cycles declaring a function that may not be used before doing the check.

* blue
: opinion, don't take it too offensively

red: important factors.
 
I also did a thing similar to this a couple days ago, but it gets all items from a container and not from the player
http://otland.net/f16/get-every-item-backpack-133718/#post1293226


Code:
function getAllContainerItems(item) --Gets all items inside the current container
  if not isContainer(item.uid) then
      return nil
  end
  local table = {}
  for i = 0, (getContainerSize(item.uid) -1) 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)
        for i=1, #table2, 1 do
            table[#table + 1] = table2[i]
        end
    end
  end
  return table
end
 
Last edited:
Back
Top