• 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
Support Team
Joined
Nov 17, 2010
Messages
5,699
Solutions
408
Reaction score
3,811
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()
 
OP
OP
Xikini

Xikini

I whore myself out for likes
Support Team
Joined
Nov 17, 2010
Messages
5,699
Solutions
408
Reaction score
3,811
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.
 

Alpha

Relentless
Premium User
Joined
Apr 3, 2011
Messages
656
Solutions
36
Reaction score
461
Location
South Korea
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
 
Top