[Function][TFS 1.1]item:getParentsList()

Discussion in 'Mods & Lua Functions' started by 2Rec, Jul 16, 2017.

  1. 2Rec

    2Rec Active Member

    Joined:
    Jul 31, 2013
    Messages:
    146
    Likes Received:
    79
    Best Answers:
    24
    A small function i made while ago. Maybe would be useful to someone.
    Function returns a table of every item's parent userdata.

    Don't know if it's scripted good or can be more efficient.
    Would be glad for some more experienced scripter to revise it in this case.
    Code (Lua):
    1.  
    2. function Item.getParentsList(self, PList)
    3. local PList = {}
    4.  
    5.     if self:getParent() == self:getTopParent() then
    6.         if self:getTopParent():isItem() then
    7.             table.insert(PList, 1, self:getTopParent())
    8.         end
    9.     return PList
    10.     end
    11.  
    12.     if not self:getParent() then
    13.     return PList
    14.     end
    15.  
    16. local actualParent = self:getParent()
    17. local topParent = self:getTopParent()
    18.  
    19. table.insert(PList, 1, actualParent)
    20.  
    21.     while actualParent ~= topParent do
    22.         actualParent = actualParent:getParent()
    23.             if actualParent:isItem() then
    24.             table.insert(PList, 1, actualParent)
    25.             end
    26.     end
    27.  
    28. return PList
    29. end
    30.  
     
    silveralol likes this.

Share This Page

Loading...