• 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.X+ How to iterate over depot locker slots ?

Snavy

Bakasta
Senator
Joined
Apr 1, 2012
Messages
1,249
Solutions
71
Reaction score
621
Location
Hell
I am trying to iterate over the slots inside player depot locker and find a specific container but I haven't found a way to do it.
OTLAND, SHOW ME DA WEY!

KKHHHT! SPITS
 
giphy.gif


But isn't the depot locker a container? Tried to loop through it?
Hmm, a container doesn't seem to have a getItems function like a tile has? (or the docs is bad)

Item metatable

Verify an item is a container:
item:isContainer()

Container metatable:

Length/max index of items in this container:
Container:getItemHoldingCount()

Retrieve item of an iterated index:
Container:getItem(index)

Lua:
-- Generic dump to string function
function dump(o)
   if type(o) == 'table' then
      local s = '{ '
      for k,v in pairs(o) do
         if type(k) ~= 'number' then k = '"'..k..'"' end
         s = s .. '['..k..'] = ' .. dump(v) .. ','
      end
      return s .. '} '
   else
      return tostring(o)
   end
end

-- Search a container, returning countainers inside... this container
function searchContainer(containerItem)
	-- Failsafe
	if containerItem == nil or not containerItem:isContainer() then
		print("Function searchContainer used without passing in a proper container")
		return false
	end
	local containers = []
	-- actual search
	local maxIndex = containerItem:getItemHoldingCount()
	for i=1, maxIndex do -- Might need to add +1 or -1 to maxIndex to get the complete loop, Lua indexing is weird
		local item = containerItem:getItem(i)
		if item:isContainer() then
			table.insert(containers, item)
		end
	end
	return containers
end

-- (-.(^.-(^.^)-.^).-) start of my drunk code
function helloWorld(locker)
	if locker == nil or not locker:isItem() then
		print("input locker parameter not even recognized as an item")
		return false
	end

	if not locker:isContainer() then
		print("Locker (".. locker:getItemId() ..") is not a container???")
		return false
	end

	local containers = searchContainer(locker)
	-- Lets have a look into containers inside locker 
	print("Containers: ".. dump(containers))
end
 
Last edited:
giphy.gif


But isn't the depot locker a container? Tried to loop through it?
Hmm, a container doesn't seem to have a getItems function like a tile has? (or the docs is bad)

Item metatable

Verify an item is a container:
item:isContainer()

Container metatable:

Length/max index of items in this container:
Container:getItemHoldingCount()

Retrieve item of an iterated index:
Container:getItem(index)

Lua:
-- Generic dump to string function
function dump(o)
   if type(o) == 'table' then
      local s = '{ '
      for k,v in pairs(o) do
         if type(k) ~= 'number' then k = '"'..k..'"' end
         s = s .. '['..k..'] = ' .. dump(v) .. ','
      end
      return s .. '} '
   else
      return tostring(o)
   end
end

-- Search a container, returning countainers inside... this container
function searchContainer(containerItem)
    -- Failsafe
    if containerItem == nil or not containerItem:isContainer() then
        print("Function searchContainer used without passing in a proper container")
        return false
    end
    local containers = []
    -- actual search
    local maxIndex = containerItem:getItemHoldingCount()
    for i=1, maxIndex do -- Might need to add +1 or -1 to maxIndex to get the complete loop, Lua indexing is weird
        local item = containerItem:getItem(i)
        if item:isContainer() then
            table.insert(containers, item)
        end
    end
    return containers
end

-- (-.(^.-(^.^)-.^).-) start of my drunk code
function helloWorld(locker)
    if locker == nil or not locker:isItem() then
        print("input locker parameter not even recognized as an item")
        return false
    end

    if not locker:isContainer() then
        print("Locker (".. locker:getItemId() ..") is not a container???")
        return false
    end

    local containers = searchContainer(locker)
    -- Lets have a look into containers inside locker
    print("Containers: ".. dump(containers))
end

Thanks for the response @Znote :D

but I kind of gave up on iterating over the depot and decided to look into Game.createContainer(ItemID, size). The idea is to create a virtual container and add it to the player. once the V.Container is created it should open as a normal bag would in the right panel, but it wont. I have asked the question on the discord channel . But nobody has responded about that question. Do you know why it won't open the (virtual)container ? (should I just make a new thread about this ?)
 
Back
Top