• 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 TFS 1.2 - Counting amounts of certain itens in BP

Joined
Dec 13, 2015
Messages
45
Reaction score
13
I'm trying to count how many of a certain item the player has in his backpack.

For that I'm using player:getSlotItem(CONST_SLOT_BACKPACK):getItemCountById(item).
The problem is that this function only checks the slots in the bp that it was asked to do so. It dosent check the other containers inside that bp.

Also I couldn't find out what the optional argument subtype = -1 stands for in getItemCountById(itemId[, subtype = -1]). Maybe it would allow the function to also check the other containers inside the bp?

Does anyone know how can I solve this problem? How can I make it do a deep search for total item count?

I guess I can use player:getItemCount and them just check if has one of that item equipped and them lower count by 1. But that would require 9 checks.
 
Last edited:
Solution
I'm trying to count how many of a certain item the player has in his backpack.

For that I'm using player:getSlotItem(CONST_SLOT_BACKPACK):getItemCountById(item).
The problem is that this function only checks the slots in the bp that it was asked to do so. It dosent check the other containers inside that bp.

Also I couldn't find out what the optional argument subtype = -1 stands for in getItemCountById(itemId[, subtype = -1]). Maybe it would allow the function to also check the other containers inside the bp?

Does anyone know how can I solve this problem? How can I make it do a deep search for total item count?

I guess I can use player:getItemCount and them just check if has one of that item equipped...
I'm trying to count how many of a certain item the player has in his backpack.

For that I'm using player:getSlotItem(CONST_SLOT_BACKPACK):getItemCountById(item).
The problem is that this function only checks the slots in the bp that it was asked to do so. It dosent check the other containers inside that bp.

Also I couldn't find out what the optional argument subtype = -1 stands for in getItemCountById(itemId[, subtype = -1]). Maybe it would allow the function to also check the other containers inside the bp?

Does anyone know how can I solve this problem? How can I make it do a deep search for total item count?

I guess I can use player:getItemCount and them just check if has one of that item equipped and them lower count by 1. But that would require 9 checks.
Code:
function Container:getItemCountById2(itemid, deepSearch)
    local ret = 0
    local size = self:getSize()
    for i = 0, size-1 do
        local item = self:getItem(i)
        if item then
            if item:isContainer() and deepSearch then
                ret = ret + item:getItemCountById2(itemid, deepSearch)
            end
            if item:getId() == itemid then
                ret = ret + 1
            end
        end
    end
    return ret
end
 
Solution
Code:
function Container:getItemCountById2(itemid, deepSearch)
    local ret = 0
    local size = self:getSize()
    for i = 0, size-1 do
        local item = self:getItem(i)
        if item then
            if item:isContainer() and deepSearch then
                ret = ret + item:getItemCountById2(itemid, deepSearch)
            end
            if item:getId() == itemid then
                ret = ret + 1
            end
        end
    end
    return ret
end

Thanks!
 
Back
Top