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

TFS 1.4 getListOfContainerItems

Xikini

I whore myself out for likes
Senator
Premium User
Joined
Nov 17, 2010
Messages
6,788
Solutions
581
Reaction score
5,354
Finds all of the items in a container, and returns it as a table.

Lua:
function getListOfContainerItems(container)
    if not container:isContainer() then
        return false
    end
    
    local containers, items = {}, {}
    table.insert(containers, container)
    while #containers > 0 do
        for i = 0, containers[1]:getSize() - 1 do  
            local item = containers[1]:getItem(i)
            table.insert(items, item)
            if item:isContainer() then
                table.insert(containers, item)
            end
        end
        table.remove(containers, 1)
    end
    
    return items
end

Example usage; Grabbing all of the items inside a players depot.

Untitled.png
Lua:
local actions_test = Action()

function actions_test.onUse(player, item, fromPosition, target, toPosition, isHotkey)
    
    local townId = 1
    local depotChest = player:getDepotChest(townId) 
    local depotList = getListOfContainerItems(depotChest)
    
    local itemList = ""
    for i = 1, #depotList do
        if itemList ~= "" then
            itemList = itemList .. ", "
        end
        itemList = itemList .. depotList[i]:getId()
    end
    print(itemList)
    
    return true
end

actions_test:id(2173)
actions_test:register()
 
Might want to call it something slightly different, because container:getItems() already exists. xP

For clarification, my script recursively goes through and grabs every item regardless of how deep inside it needs to check. (container inside container inside container et cetera)
where container:getItems() only grabs the items inside the immediate container without checking if any of those items are containers.
 
Might want to call it something slightly different, because container:getItems() already exists. xP

For clarification, my script recursively goes through and grabs every item regardless of how deep inside it needs to check. (container inside container inside container et cetera)
where container:getItems() only grabs the items inside the immediate container without checking if any of those items are containers.
Call it whatever, the name was just an example of how it could be done instead
 
Back
Top