Can frames be deleted? Will setting a frame to nil allow it to be collected by the garbage collector?
I have a frame of portrait buttons each of which is a member of my party. Members come and go and I want to make sure the buttons in the frame correctly match the current members. My simplistic way of accomplishing this is to recreate the frame and buttons each time a member joins (when GROUP_JOINED fires) or leaves (when GROUP_LEFT fires). But, this means leaving a lot of orphaned frames.
Surely, there’s a better way.
Cheers,
No. frames are forever… during a session.
Use a frame “pool”.
When no longer required, add the frame to a pool table (and remove it from your table of “current” frames). When you need a frame, check the pool and remove/re-use one from there or create a new frame if the pool is empty.
or something like that…
Optional child frames can be hidden/added as required.
Simpler still if it’s just for party members (max of 4) just create 4 frames (or collection of) and show/hide and update the details as required.
local f = CreateFrame("Frame", "WarfiePartyPortraits", UIParent)
f:SetSize(5, 5)
f:SetPoint("LEFT", 10, 0)
for i=1, 4 do
local p =CreateFrame("Frame", nil, f)
f["Party"..i] = p
p:Hide()
p:SetSize(30, 30)
p.Portrait = p:CreateTexture()
p.Portrait:SetAllPoints()
if i == 1 then
p:SetPoint("TOPLEFT")
else
p:SetPoint("TOPLEFT", f["Party"..i-1], "TOPRIGHT")
end
p.Name = p:CreateFontString()
p.Name:SetFontObject(GameFontNormal)
p.Name:SetJustifyH("CENTER")
p.Name:SetPoint("TOP", p, "BOTTOM", 0, -3)
end
f:SetScript("OnEvent", function(self)
for i=1, 4 do
local p = self["Party"..i]
if UnitExists("party"..i)
SetPortraitTexture(p, "party"..i)
p.Name:SetText(UnitName("party"..i))
p:Show()
else
p:Hide()
end
end
end)
f:RegisterEvent("GROUP_ROSTER_UPDATE")
You might use a pool if you were talking about a lager or unknown number of frames.
Frames themselves are just special types of tables so you can add key/value pairs to them (in this case the party portrait frames to f and the FontStrings to the portaits) just like any other table.
I know this was a while ago, Fizzle, but I took your suggestion to heart and restructured my threat addon to incorporate pools. While pools are surely overkill from my ‘party’ addon, it was an opportunity to use and learna about pools. So, thanks.
1 Like