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

check items

Fortera Global

Intermediate OT User
Joined
Nov 20, 2015
Messages
1,180
Solutions
2
Reaction score
117
how to check items inside containers inside one parcel per example?

in this picture, to check items from all containers inside parcel

abhukVs.png


tfs 1.2


Lua:
if item and item:getId() == 2595 then      
    if item:getItemHoldingCount() ~= 0 then
    if (getContainerItem(item.uid, 0).itemid == 29526) then
 
Solution
Easy function:
Code:
function Container.getItemsById(self, itemId)
local list = {}
for index = 0, (self:getSize() - 1) do
    local item = self:getItem(index)
    if item then
        if item:isContainer() then
            local rlist = item:getItemsById(itemId)
            if type(rlist) == 'table' then
                for _, v in pairs(rlist) do
                    table.insert(list, v)
                end
            end
        else
            if item:getId() == itemId then
                table.insert(list, item)
            end
        end
    end
end
return list
end

example use:
Code:
local backpack = player:getSlotItem(CONST_SLOT_BACKPACK)
local items = backpack:getItemsById(2160)
if #items > 0 then
for _, item in pairs(items)...
Code:
if toPosition.x == CONTAINER_POSITION then
    local container = self:getContainerById(toPosition.y - 64)
    if container then
        local topParent = container:getTopParent()
        if topParent and topParent:getId() == 2595 then
            -- do something
        end
    end
end
 
put this in data/lib/core/container.lua
Lua:
function Container.recursiveGetItemById(itemid)
    local function search(container)
        for i = container:getItemHoldingCount() - 1, 0, -1 do
            local item = container:getItem(i)
            if item then
                if item:getId() == itemid then
                    return item
                elseif item:isContainer() then
                    search(Container(item.uid))
                end
            end
        end
    end
    return search(itemid)
end

in your script use item:recursiveGetItemById(29526)
 
put this in data/lib/core/container.lua
Lua:
function Container.recursiveGetItemById(itemid)
    local function search(container)
        for i = container:getItemHoldingCount() - 1, 0, -1 do
            local item = container:getItem(i)
            if item then
                if item:getId() == itemid then
                    return item
                elseif item:isContainer() then
                    search(Container(item.uid))
                end
            end
        end
    end
    return search(itemid)
end

in your script use item:recursiveGetItemById(29526)

thanks bro
I have one error

Code:
Lua Script Error: [Event Interface]
data/events/scripts/player.lua:Player@onMoveItem
data/events/scripts/player.lua:461: attempt to call method 'recursiveGetItemById' (a nil value)
stack traceback:
        [C]: in function 'recursiveGetItemById'
        data/events/scripts/player.lua:461: in function <data/events/scripts/player.lua:440>

I did:
Lua:
if item:recursiveGetItemById(29526) then
--- something
end

edit: how I can count this item recursive?

edit2, I did:
Lua:
if item and item:getId() == 2595 then 
        self:say("teste 1", TALKTYPE_SAY)
        if item:getItemHoldingCount() ~= 0 then
        self:say("teste 2", TALKTYPE_SAY)
            if item:recursiveGetItemById(29526) then
                self:say("teste 3", TALKTYPE_SAY)
            end
        end
    end
teste 3 no working, no error
 
Last edited by a moderator:
sorry, was rushed and i messed up the function
Lua:
function Container.recursiveGetItemById(self, itemid)
    local function search(container)
        for i = container:getItemHoldingCount() - 1, 0, -1 do
            local item = container:getItem(i)
            if item then
                if item:getId() == itemid then
                    return item
                elseif item:isContainer() then
                    search(Container(item.uid))
                end
            end
        end
    end
    return search(self)
end
 
sorry, was rushed and i messed up the function
Lua:
function Container.recursiveGetItemById(self, itemid)
    local function search(container)
        for i = container:getItemHoldingCount() - 1, 0, -1 do
            local item = container:getItem(i)
            if item then
                if item:getId() == itemid then
                    return item
                elseif item:isContainer() then
                    search(Container(item.uid))
                end
            end
        end
    end
    return search(self)
end
thanks bro, how I can print the count of the recursive item?
and the script will check only if its from self? or work per example if player move the parcel from depot/ground/.. to mail?

edit:
ZTxtAxD.png

the script no checking inside another containers
 
thanks bro, how I can print the count of the recursive item?
and the script will check only if its from self? or work per example if player move the parcel from depot/ground/.. to mail?

edit:
ZTxtAxD.png

the script no checking inside another containers
it returns the item, store the item inside a variable
Lua:
local item = item:recursiveGetItemById(id)
if item then
    print(item:getCount())
end
the reason it doesn't work for the second item is cause it returns the first item found, it doesn't give you back all the items
it will search through all container until it finds the first instance of that item id and returns the item userdata back to the function
 
it returns the item, store the item inside a variable
Lua:
local item = item:recursiveGetItemById(id)
if item then
    print(item:getCount())
end
the reason it doesn't work for the second item is cause it returns the first item found, it doesn't give you back all the items
it will search through all container until it finds the first instance of that item id and returns the item userdata back to the function

no checking inside the bag, as show the picture:
zj4yBHK.png


I'm using:
Lua:
if item and item:getId() == 2595 then
        self:say("teste 1", TALKTYPE_SAY)
        if item:getItemHoldingCount() ~= 0 then
        self:say("teste 2", TALKTYPE_SAY)
            if item:recursiveGetItemById(29526) then
                local itemP = item:recursiveGetItemById(29526)
                if itemP then
                    self:say("teste 3", TALKTYPE_SAY)
                    --print(itemP:getCount())
                end
            end
        end
    end

SsUzlR1.png

edit: I need to check/print all items with id 29526, not only one :(
 
Last edited by a moderator:
Easy function:
Code:
function Container.getItemsById(self, itemId)
local list = {}
for index = 0, (self:getSize() - 1) do
    local item = self:getItem(index)
    if item then
        if item:isContainer() then
            local rlist = item:getItemsById(itemId)
            if type(rlist) == 'table' then
                for _, v in pairs(rlist) do
                    table.insert(list, v)
                end
            end
        else
            if item:getId() == itemId then
                table.insert(list, item)
            end
        end
    end
end
return list
end

example use:
Code:
local backpack = player:getSlotItem(CONST_SLOT_BACKPACK)
local items = backpack:getItemsById(2160)
if #items > 0 then
for _, item in pairs(items) do
 print(item:getName(), item:getCount())
end
 
Solution
Easy function:
Code:
function Container.getItemsById(self, itemId)
local list = {}
for index = 0, (self:getSize() - 1) do
    local item = self:getItem(index)
    if item then
        if item:isContainer() then
            local rlist = item:getItemsById(itemId)
            if type(rlist) == 'table' then
                for _, v in pairs(rlist) do
                    table.insert(list, v)
                end
            end
        else
            if item:getId() == itemId then
                table.insert(list, item)
            end
        end
    end
end
return list
end

example use:
Code:
local backpack = player:getSlotItem(CONST_SLOT_BACKPACK)
local items = backpack:getItemsById(2160)
if #items > 0 then
for _, item in pairs(items) do
 print(item:getName(), item:getCount())
end

hey bro, nice too, but how to check if parcel is on ground/depot/etc ?
in ur example will check in slot backpack
 
Any container,
If the problem was solved, I remember to mark the best answer and give like.

local backpack = player:getSlotItem(CONST_SLOT_BACKPACK)

this will check in any container?

edit: I need to check itens inside parcel, the parcel can be in ground/depots or in others places
 
Last edited by a moderator:
other example:
Code:
function onSay(player, words, param)
local parcel = Tile(Position(x, y, z)):getItemById(2595)
if parcel then
    local search = parcel:getItemsById(2160)
    if #search > 0 then
        for _, item in pairs(search) do
            player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, string.format('name: %s count: %s', item:getName(), item:getCount()))
        end
    end
end
return false
end
 
other example:
Code:
function onSay(player, words, param)
local parcel = Tile(Position(x, y, z)):getItemById(2595)
if parcel then
    local search = parcel:getItemsById(2160)
    if #search > 0 then
        for _, item in pairs(search) do
            player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, string.format('name: %s count: %s', item:getName(), item:getCount()))
        end
    end
end
return false
end
This code break when tile is nil and tile can be nil if the position isnt right and tab the code properly please.
 
This code break when tile is nil and tile can be nil if the position isnt right and tab the code properly please.
It is a simple example, it does not have to be functional.

PAIR VS IPAIR
I recommend you read this:
Programming in Lua : 7.3
 
Last edited:
Back
Top