Hi all,
First of all, I would like to ask an excuse as this thread is more related to Ace3 framework than generic lua questions.
Tracker.lua
Lua Code:
local N, T = ...;
local E = LibStub("AceAddon-3.0"):NewAddon(N);
T[1] = E
T[2] = {};
_G[N] = T;
function E:OnInitialize()
if not TrackerDB then
TrackerDB = {};
end
self.db = TrackerDB;
end
Experience.lua
Lua Code:
local E, L = unpack(select(2, ...));
local LSM = LibStub("LibSharedMedia-3.0");
local font = LSM:Fetch("font", "MeatEdition");
local Experience = E:NewModule("Experience", "AceEvent-3.0");
local function CommaValue(value)
local left, num, right = string.match(value, '^([^%d]*%d)(%d*)(.-)$');
return left .. (num:reverse():gsub('(%d%d%d)', '%1,'):reverse()) .. right;
end
function Experience:UpdateText()
local level = UnitLevel("player");
local curXP, maxXP = UnitXP("player"), UnitXPMax("player");
local perXP = curXP / maxXP * 100;
self.Frame.NameText:SetText(UnitName("player"));
self.Frame.ExpText:SetFormattedText("|cffeda900Level:|r %d, |cffeda900Exp:|r %s / %s (%.2f%%)", level, CommaValue(curXP), CommaValue(maxXP), perXP);
end
function Experience:ADDON_LOADED(event, name)
if name == E:GetName() then
local point, relativeFrame, relativePoint, ofsx, ofsy;
if E.db.pos then
point, relativeFrame, relativePoint, ofsx, ofsy = unpack(E.db.pos);
else
point, relativeFrame, relativePoint, ofsx, ofsy = "CENTER", "UIParent", "CENTER", 0, 200;
end
local Frame = CreateFrame("Frame", "Experience", UIParent);
Frame:SetSize(280, 50);
Frame:SetPoint(point, relativeFrame, relativePoint, ofsx, ofsy);
Frame:SetTemplate(true); -- Custom frame function that sets template of frame
local r, g, b = Frame.template:GetBackdropColor();
Frame.template:SetBackdropColor(r, g, b, 0.7);
local _, class = UnitClass("player");
local color = RAID_CLASS_COLORS[class];
local NameText = Frame:CreateFontString(nil, "OVERLAY");
NameText:SetFont(font, 16, "OUTLINE");
NameText:SetPoint("TOP", 0, -5);
NameText:SetTextColor(color.r, color.g, color.b);
NameText:SetText("");
local ExpText = Frame:CreateFontString(nil, "OVERLAY");
ExpText:SetFont(font, 14, "OUTLINE");
ExpText:SetPoint("BOTTOM", 0, 5);
ExpText:SetTextColor(1, 1, 1);
ExpText:SetFormattedText("|cffeda900Level:|r %d, |cffeda900Exp:|r %s / %s (%.2f%%)", 0, 0, 0, 0);
self.Frame = Frame;
Frame.NameText = NameText;
Frame.ExpText = ExpText;
end
end
function Experience:PLAYER_LOGIN()
self:UpdateText();
end
function Experience:PLAYER_XP_UPDATE()
self:UpdateText();
end
function Experience:PLAYER_LOGOUT()
local point, relativeFrame, relativePoint, ofsx, ofsy = self.Frame:GetPoint();
if relativeFrame == UIParent then
relativeFrame = "UIParent";
end
E.db.pos = {
point,
relativeFrame,
relativePoint,
ofsx,
ofsy,
}
end
Experience:RegisterEvent("ADDON_LOADED");
Experience:RegisterEvent("PLAYER_LOGIN");
Experience:RegisterEvent("PLAYER_XP_UPDATE");
Experience:RegisterEvent("PLAYER_LOGOUT");
As I have hinted above, I am currently playing around with Ace3 & SavedVariables and making super simple addon which just displays player's name, level and exp amount.
(Yeah... I am aware of that this kind of addon is useless, it's just for studying
)
Q: Why am I using
Ace3?
A: Like I've said, I am using it for studying(?) purpose, and was interested in its AceEvent-3.0 functions as well.
Q: What is saved in
SavedVariables?
A: It currently stores the point data of the frame like point, relativeFrame, relativePoint, ofsx, ofsy.
However, as you can see above, my code structures are currently messed up (not optimized) and am still not confident of using them. Thus, I would like to ask some advice to those who are using either of them.
The reason that I've utilized ADDON_LOADED event is because of the db, SavedVariables.
If I am understanding correctly, the loading is done by the following orders.
1. Load lua files
2. :OnInitialize() function gets called
3. ADDON_LOADED event gets called
So, if I pull those initialization codes out from :ADDON_LOADED() function, it will just crash as it cannot find db.
That's pretty much all of what I can explain(?) so far.
Could I please get some advice regarding what I have done?
(Especially in terms of optimization)
Thank you!