Thread Tools Display Modes
Prev Previous Post   Next Post Next
02-12-17, 08:55 AM   #1
Layback_
An Onyxian Warder
Join Date: Feb 2016
Posts: 358
Would like to ask some advice regarding AceAddon-3.0

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:
  1. local N, T = ...;
  2.  
  3. local E = LibStub("AceAddon-3.0"):NewAddon(N);
  4.  
  5. T[1] = E
  6. T[2] = {};
  7.  
  8. _G[N] = T;
  9.  
  10. function E:OnInitialize()
  11.     if not TrackerDB then
  12.         TrackerDB = {};
  13.     end
  14.  
  15.     self.db = TrackerDB;
  16. end

Experience.lua
Lua Code:
  1. local E, L = unpack(select(2, ...));
  2.  
  3. local LSM = LibStub("LibSharedMedia-3.0");
  4. local font = LSM:Fetch("font", "MeatEdition");
  5.  
  6. local Experience = E:NewModule("Experience", "AceEvent-3.0");
  7.  
  8. local function CommaValue(value)
  9.     local left, num, right = string.match(value, '^([^%d]*%d)(%d*)(.-)$');
  10.  
  11.     return left .. (num:reverse():gsub('(%d%d%d)', '%1,'):reverse()) .. right;
  12. end
  13.  
  14. function Experience:UpdateText()
  15.     local level = UnitLevel("player");
  16.     local curXP, maxXP = UnitXP("player"), UnitXPMax("player");
  17.     local perXP = curXP / maxXP * 100;
  18.  
  19.     self.Frame.NameText:SetText(UnitName("player"));
  20.     self.Frame.ExpText:SetFormattedText("|cffeda900Level:|r %d, |cffeda900Exp:|r %s / %s (%.2f%%)", level, CommaValue(curXP), CommaValue(maxXP), perXP);
  21. end
  22.  
  23. function Experience:ADDON_LOADED(event, name)
  24.     if name == E:GetName() then
  25.         local point, relativeFrame, relativePoint, ofsx, ofsy;
  26.         if E.db.pos then
  27.             point, relativeFrame, relativePoint, ofsx, ofsy = unpack(E.db.pos);
  28.         else
  29.             point, relativeFrame, relativePoint, ofsx, ofsy = "CENTER", "UIParent", "CENTER", 0, 200;
  30.         end
  31.  
  32.         local Frame = CreateFrame("Frame", "Experience", UIParent);
  33.         Frame:SetSize(280, 50);
  34.         Frame:SetPoint(point, relativeFrame, relativePoint, ofsx, ofsy);
  35.         Frame:SetTemplate(true); -- Custom frame function that sets template of frame
  36.  
  37.         local r, g, b = Frame.template:GetBackdropColor();
  38.         Frame.template:SetBackdropColor(r, g, b, 0.7);
  39.  
  40.         local _, class = UnitClass("player");
  41.         local color = RAID_CLASS_COLORS[class];
  42.  
  43.         local NameText = Frame:CreateFontString(nil, "OVERLAY");
  44.         NameText:SetFont(font, 16, "OUTLINE");
  45.         NameText:SetPoint("TOP", 0, -5);
  46.         NameText:SetTextColor(color.r, color.g, color.b);
  47.         NameText:SetText("");
  48.  
  49.         local ExpText = Frame:CreateFontString(nil, "OVERLAY");
  50.         ExpText:SetFont(font, 14, "OUTLINE");
  51.         ExpText:SetPoint("BOTTOM", 0, 5);
  52.         ExpText:SetTextColor(1, 1, 1);
  53.         ExpText:SetFormattedText("|cffeda900Level:|r %d, |cffeda900Exp:|r %s / %s (%.2f%%)", 0, 0, 0, 0);
  54.  
  55.         self.Frame = Frame;
  56.  
  57.         Frame.NameText = NameText;
  58.         Frame.ExpText = ExpText;
  59.     end
  60. end
  61.  
  62. function Experience:PLAYER_LOGIN()
  63.     self:UpdateText();
  64. end
  65.  
  66. function Experience:PLAYER_XP_UPDATE()
  67.     self:UpdateText();
  68. end
  69.  
  70. function Experience:PLAYER_LOGOUT()
  71.     local point, relativeFrame, relativePoint, ofsx, ofsy = self.Frame:GetPoint();
  72.  
  73.     if relativeFrame == UIParent then
  74.         relativeFrame = "UIParent";
  75.     end
  76.  
  77.     E.db.pos = {
  78.         point,
  79.         relativeFrame,
  80.         relativePoint,
  81.         ofsx,
  82.         ofsy,
  83.     }
  84. end
  85.  
  86. Experience:RegisterEvent("ADDON_LOADED");
  87. Experience:RegisterEvent("PLAYER_LOGIN");
  88. Experience:RegisterEvent("PLAYER_XP_UPDATE");
  89. 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!

Last edited by Layback_ : 02-12-17 at 09:08 AM.
  Reply With Quote
 

WoWInterface » Developer Discussions » Lua/XML Help » Would like to ask some advice regarding AceAddon-3.0


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off