View Single Post
09-17-14, 07:10 PM   #2
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
1) You're creating a new font string every time, instead of reusing the old one. Keep track of the already-created rows and only create a new one if you're already using all the existing ones.

2) Your update loop doesn't need to manually increment i -- the whole purpose of a for loop is to do that for you.

3) You're creating multiple objects with the global names "Level" and "LevelName", which is bad for many reasons. These objects don't need names at all, these names are extremely generic and extremely likely to conflict with other addons or the default UI, and since they're all the same they're even conflicting with each other. You also appear to be setting many other generically named variables like "charLevel" and "ofsy" in the global namespace, which is called leaking and is bad. Put "local" keywords in front of each variable you declare, unless it absolutely needs to be global.

I'd suggest something more like this:

Code:
local rowFrames = {}

local function CreateRow(i)
	local f = CreateFrame("Button", nil, parent) -- Doesn't need a global name.
	f:SetWidth(40)
	f:SetHeight(16)
	if i == 1 then
		f:SetPoint("TOPLEFT", parent, 6, -6)
	else
		f:SetPoint("TOPLEFT", rowFrames[i-1], "BOTTOMLEFT")
	end

	local text = f:CreateFontString(nil, "OVERLAY") -- Doesn't need a global name.
	text:SetPoint("LEFT")
	text:SetPoint("RIGHT")
	text:SetJustifyH("LEFT")
	text:SetFont(STANDARD_TEXT_FONT, 12, "OUTLINE")
	text:SetTextColor(1, 1, 1, 1)
	f:SetFontString(text) -- This is why it's a Button instead of a plain Frame,
		-- so you can now :SetText on the Button itself.

	f:SetID(i)
	rowFrames[i] = f
	return f
end

for i = 1, NUM_ROWS_TO_DISPLAY do
	local row = rowFrames[i] or CreateRow(i)
	row:SetText(YOUR_TEXT_HERE)
end

-- Hide any unused rows:
for i = NUM_ROWS_TO_DISPLAY + 1, #rowFrames do
	rowFrames[i]:SetText(nil)
end
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote