SenL Posted February 8, 2016 Share Posted February 8, 2016 (edited) Hi, I have a basic question about table in lua. Below code is from recezib's "More DST characters" mod (workshop-343762271) - shadowpuppeteer component. I copied it and renamed "ShadowPuppeteer" to "BarbSummoner" for my char. I then added a function to get the count of barbs. However, this function does not work. Spoiler --Credits: Rezecib (workshop-343762271) local BarbSummoner = Class(function(self, inst) self.inst = inst -- self.shouldremove = false self.barbs = {} -- self.save_records = {} end) function BarbSummoner:Add(barb) print("BarbSummoner:Add") --self.inst.components.sanity:AddSanityPenalty(barb.GUID, 0.275) barb:ListenForEvent("death", function(inst) self:Remove(inst) end) table.insert(self.barbs, barb) end function BarbSummoner:Remove(barb) print("BarbSummoner:Remove") for k,v in pairs(self.barbs) do if barb == v then --self.inst.components.sanity:RemoveSanityPenalty(barb.GUID, 0.275) table.remove(self.barbs, k) end end end function BarbSummoner:RemoveAll() print("BarbSummoner:RemoveAll") for k,v in pairs(self.barbs) do -- local save_record = v:GetSaveRecord() -- table.insert(self.save_records, save_record) v:Remove() end end function BarbSummoner:OnSave() local barbs = {} local i = 1 if #self.barbs > 0 then for k,v in pairs(self.barbs) do barbs = v:GetSaveRecord() i = i + 1 end end -- if #self.save_records > 0 then -- for k,v in pairs(self.save_records) do -- barbs = v:GetSaveRecord() -- i = i + 1 -- end -- end print("BarbSummoner:OnSave", i-1) -- if self.shouldremove then -- self:RemoveAll() -- end return { barbs = barbs}end function BarbSummoner:OnLoad(data) print("BarbSummoner:OnLoad", #data.barbs) if data.barbs ~= nil and #data.barbs > 0 then for k,v in pairs(data.barbs) do local barb = SpawnSaveRecord(v) if barb.components.follower.leader ~= self.inst then barb.components.follower:SetLeader(self.inst) end -- self:Add(barb) end endend function BarbSummoner:GetBarbCount() --return #self.barbs or 0 local count = 0 for _ in pairs(self.barbs) do count = count + 1 end return countend return BarbSummoner The issue is after the component calls RemoveAll() function, the count of the table does not get reset to 0. Let's say I have summoned 6 barbs and then I called RemoveAll(). I expect the count to reset back to 0 but instead I get 6 again. How do I do this? Thanks. Edited February 8, 2016 by SenL Link to comment Share on other sites More sharing options...
Muche Posted February 9, 2016 Share Posted February 9, 2016 (edited) v:Remove() is EntityScript:Remove() which clears the entity but does not actually remove it from the self.barbs table. Try (in RemoveAll()): v:Remove() self:Remove(v) Edited February 9, 2016 by Muche Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now