• 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+ [TFS 1.3] How to do Deep Search in a For Loop in Main Backpack to Searching Items Inside Other Backpacks?

Yan18

Member
Joined
Jun 14, 2014
Messages
104
Solutions
3
Reaction score
17
Hi guys

How to do a deep search for loop to search items in backpacks inside main backpack?
 
Solution
I do exactly that in this function.
I'm sure you can modify it to fit your needs.

I do exactly that in this function.
I'm sure you can modify it to fit your needs.

 
Solution
The main concept is to
  • store results in a list
  • put the list in function output
  • call the same function on subcontainer to get results from it
  • merge subcontainer search results with main (or parent) list

example:
(not actual code, just explaination how to do that)
Code:
function somekindofsearch(item)
declare main list
normal container search
if items[i]:isContainer() then
  local sublist = somekindofsearch(items[i])
  for k, v in pairs(sublist) do
    insert results to main list
  end
end

return some list
end
 
You will have to make a recursive function.
Yes, but I wanna know if there are easiest ways, but I think recursive is the only solution for this problem haha. Thanks!

I do exactly that in this function.
I'm sure you can modify it to fit your needs.

Thanks! I'm gonna look.

The main concept is to
  • store results in a list
  • put the list in function output
  • call the same function on subcontainer to get results from it
  • merge subcontainer search results with main (or parent) list

example:
(not actual code, just explaination how to do that)
Code:
function somekindofsearch(item)
declare main list
normal container search
if items[i]:isContainer() then
  local sublist = somekindofsearch(items[i])
  for k, v in pairs(sublist) do
    insert results to main list
  end
end

return some list
end
Yes, pratically it's necessary to do a recursive function as Boy67 said. I wanted to know if there was a easiest ways. But I will do recursive function! Thanks!
 
Here is another example that uses a repeat loop and only inserts
¡This method always is recursive!
Lua:
function Container:getItems()
    local index, containers, items = 0, {self}, {}
    repeat
        index = index +1
        local container = containers[index]
        for i = 0, container:getSize() - 1 do  
            local item = container:getItem(i)
            items[#items +1] = item
            if item:isContainer() then
                containers[#containers +1] = item
            end
        end
    until #containers <= index
    return items
end

Although if you are in TFS 1.4+ you should already be able to use the Container.getItems method by default that already come from the sources
This method has a single optional parameter and it is a boolean, container:getItems(true) to get all the elements of all the containers including the same containers
 
Here is another example that uses a repeat loop and only inserts
¡This method always is recursive!
Lua:
function Container:getItems()
    local index, containers, items = 0, {self}, {}
    repeat
        index = index +1
        local container = containers[index]
        for i = 0, container:getSize() - 1 do 
            local item = container:getItem(i)
            items[#items +1] = item
            if item:isContainer() then
                containers[#containers +1] = item
            end
        end
    until #containers <= index
    return items
end

Although if you are in TFS 1.4+ you should already be able to use the Container.getItems method by default that already come from the sources
This method has a single optional parameter and it is a boolean, container:getItems(true) to get all the elements of all the containers including the same containers
Thanks! This is similar the code of Xikini in topic: TFS 1.4 getListOfContainerItems (https://otland.net/threads/tfs-1-4-getlistofcontaineritems.278178/)

But it is very helpful! Thanks a lot!
 
It does exactly the same thing, only that it is done with another type of loop, but any one works, I only did it to give more examples of loops to the public who read this
 
container:getItems(true) - the param is bool recursive and it's coded already so you don't need to do any extra container checks in your loop.
 
Back
Top