Thread: Reuse frame
View Single Post
06-20-16, 10:18 AM   #1
maqjav
A Theradrim Guardian
AddOn Author - Click to view addons
Join Date: Feb 2012
Posts: 60
Reuse frame

Hello.

I have a table with 5 frames pre-created that I want to reuse to show different icons.

Code:
local mainFrame = _G.CreateFrame("Frame", "ItemsBar", UIParent)
mainFrame:SetSize(200, 20)
mainFrame:SetPoint("BOTTOM", UIParent, 0, 300)

local itemFrames = {}
for i=1, 5 do 
	local frame = _G.CreateFrame("Frame", "ItemFrame"..i, mainFrame)
	frame:SetSize(20, 20)
	frame:SetPoint("LEFT", (5+16)* (i-1), 0)
	itemFrames[i] = frame
end
The idea is that when I type an ID, I read from another table a list of items, and I want to display its icons on my itemFrames.

Code:
function loadItems(itemsIds) then
	-- clear frames
	for i, frame in ipairs(itemFrames) then
		itemFrame:ClearAllPoints()
		itemFrame:Hide()
	end

	for i, itemId in ipairs(itemsIds) do
		local _, _, _, _, _, _, _, _, _, itemTexture, _ = GetItemInfo(itemId)
		if itemTexture and i <= 5 then
			local texture = itemFrames[i]:CreateTexture()
			texture:SetSize(18, 18)
			texture:SetTexture(itemTexture)
			texture:SetAllPoints(itemFrames[i])
			itemFrames[i]:Show()
		else break
		end
	end
end
If I use this method too many times it will consume a lot of memory, because I will be creating a Texture per icon every time, so the first question is: can I change a frame texture without creating a new one?

The second problem I have is that this code works fine the first time, the next times or it doesn't show icons at all, or some of them have the previous textures, or if my array has 5 items but the previous time it had 3, it will show only 3 ignoring the last 2. It seems to have a random behaviour.

Thanks.