View Single Post
12-28-12, 11:56 PM   #10
skarie
An Aku'mai Servant
AddOn Author - Click to view addons
Join Date: Jun 2011
Posts: 37
Originally Posted by Phanx View Post
A couple thoughts on your final code:

1) There is no reason to create a separate frame for the backdrop vs. the bar. You can apply backdrops to a StatusBar object, as well as add ordinary textures to it, register events on it, set scripts on it, etc. StatusBar objects are just Frame objects with some extra features; they don't lose any features.

2) Even if you do want to create separate frames for some reason, you don't need to store them in separate tables; you should attach the child to the parent (eg. frames[i] = frame and then frame.bar = bar, instead of frames[i] = frame and bars[i] = bar).

3) You're parenting your bar to "self" but "self" is not defined in that scope, so your bars are actually not being parented to anything, which means they are not properly attached to the UI, and won't get hidden with Ctrl+Z. Again, if you really want separate frames for some reason, the bar frame should be parented to the regular frame.

4) For ease of maintenance, I'd suggest putting all of your variables/constants (like repframes and repTable) at the top of your file, so you can easily find them without having to scroll through a bunch of functions. It'll also make it easier to add more factions.

On a side note, the reason the OnEvent function is defined outside of the for-loop is because you only need one copy of it, and can attach the same function to multiple frames; defining the function inside the for-loop would create multiple copies of the function, taking up -- in your case -- 5 times as much memory but doing the same thing.

On another side note, while it doesn't affect functionality, it is conventional to use k/v only with pairs; when you're using ipairs, you use i/v instead. It just helps improve code readability, so it's more obvious that you're inside an ipairs loop (indexed values processed in sequential order) instead of a pairs loop (values processed in undefined order). It may not seem important now, but if you come back in a month or 6 months and want to change something, little helpers like this will add up and make it much easier to remember what everything does.

Revised version:
1) This is unknown to me. This is certainly helpful and will cut down lots of codes and run-times.
2) Nice idea. I didnt know a frame can have a frame as a child. This is similar to a frame carrying a text field as a child ?
3) yeah, that was a blunder...

F.CreateBD(bg) should be F.CreateBD(repframe) .. but i get the idea.

Questions for you (whenever you have time):
Can you have two separate OnEvent funtions defined in the same lua file ?

Lets say. I wanna track valor pts and honor pts together with reputation

Code:
 local function OnEvent(self, event, ...)
	local name, amount, texture, earnedThisWeek, weeklyMax, totalMax, isDiscovered = GetCurrencyInfo(self.currID)
	self:SetMinMaxValues(0, self.max)
	self:SetValue(amount)
	self.text:SetText(name)
		
end

---for i,v ....codes intentionally omitted....
	currframe:SetScript("OnEvent",OnEvent)
	currframe:RegisterEvent("CURRENCY_DISPLAY_UPDATE")
---
Is it ok to have two onEvent functions; one is tracking faction update and the other is tracking currency update ?
  Reply With Quote