Thread Tools Display Modes
09-30-16, 12:30 PM   #1
zack1040
A Defias Bandit
Join Date: Sep 2016
Posts: 2
Lua Addon programming

I'm having a problem with my code. So far everything loads fine except the values I'm pushing. And only at the start. When I reload the ui, it inputs the proper values and I can continue on with my day. I end up with unitXp (at the start) and maxXp showing up as 0. And when I reload the ui, it's like, "oh yeah, shit. forgot these. here ya go", then it's all good. I'm no stranger to object oriented programming, however I've never used lua before. HELP APPRECIATED

Lua Code:
  1. local unitXp = UnitXP("player") -- Returns the number of experience points the specified unit has in their current level. (only works on your player)
  2. local maxXp = UnitXPMax("player") -- Returns the number of experience points the specified unit needs to reach their next level. (only works on your player)
  3. local restState = GetRestState() -- Returns information about a player's rest state (saved up experience bonus)
  4. local xpDisabled = IsXPUserDisabled() -- Returns 1 if the character has disabled experience gain.
  5. local petXp = GetPetExperience() -- Returns the pet's current xp, and total xp required for next level.
  6. local xpExhaustion = GetXPExhaustion() -- Returns your character's current rested XP, nil if character is not rested.
  7. local debugtrue = false
  8.  
  9.  
  10. local oldxp = 0
  11. local pass1 = true
  12.  
  13.  
  14. print("======================================")
  15. print("LOADED INFORMATION")
  16. print("asdf:" .. unitXp)
  17. print("asdf:" .. maxXp)
  18. print("asdf:" .. petXp)
  19. print("asdf:" .. restState)
  20. print(xpDisabled)
  21. print(xpExhaustion)
  22. print("======================================")
  23.  
  24.  
  25.  
  26.  
  27. --sets the oldxp to current xp once at beginning of addon
  28. local function setXp()
  29. if pass1 then
  30. oldxp = unitXp
  31. pass1 = false
  32. print("if you see me more than once, we got problems")
  33. end
  34. end
  35.  
  36.  
  37. setXp()
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44. --returns the number of enemies to kill, of last kill, to gain new level
  45. local function killToLevel()
  46. if debugtrue then
  47. print(maxXp .. " maxxp")
  48. print(unitXp .. " unitXp")
  49. print(oldxp .. " oldxp")
  50. end
  51. local xpAmt = unitXp - oldxp
  52. if xpAmt > 1 then
  53. local killnum = (maxXp - unitXp)/(unitXp - oldxp)
  54. oldxp = unitXp
  55. return ceil(killnum)
  56. else
  57. return 0
  58. end
  59. end
  60.  
  61.  
  62. local MyAddon = CreateFrame("frame","MyAddonFrame")
  63. MyAddon:SetBackdrop({
  64.     bgFile="Interface\\DialogFrame\\UI-DialogBox-Background",
  65.     edgeFile="Interface\\DialogFrame\\UI-DialogBox-Border",
  66.     tile=1, tileSize=32, edgeSize=32,
  67.     insets={left=11, right=12, top=12, bottom=11}
  68. })
  69. windowWidth = 300
  70. windowHeight = 75
  71. MyAddon:SetWidth(windowWidth)
  72. MyAddon:SetHeight(windowHeight)
  73. MyAddon:SetPoint("CENTER",UIParent)
  74. MyAddon:EnableMouse(true)
  75. MyAddon:SetMovable(true)
  76. MyAddon:RegisterForDrag("LeftButton")
  77. MyAddon:SetScript("OnDragStart", function(self) self:StartMoving() end)
  78. MyAddon:SetScript("OnDragStop", function(self) self:StopMovingOrSizing() end)
  79. MyAddon:SetFrameStrata("FULLSCREEN_DIALOG")
  80.  
  81.  
  82. local experienceA = MyAddon:CreateFontString(nil, "OVERLAY", "GameFontNormal")
  83. local experience1 = MyAddon:CreateFontString(nil, "OVERLAY", "GameFontNormal")
  84. local experienceB = MyAddon:CreateFontString(nil, "OVERLAY", "GameFontNormal")
  85. local experience2 = MyAddon:CreateFontString(nil, "OVERLAY", "GameFontNormal")
  86. local experienceC = MyAddon:CreateFontString(nil, "OVERLAY", "GameFontNormal")
  87. local experience3 = MyAddon:CreateFontString(nil, "OVERLAY", "GameFontNormal")
  88.  
  89.  
  90. print(killnum)
  91.  
  92.  
  93. local function draw()
  94. if debugtrue then
  95. print("debugging")
  96. end
  97.  
  98.  
  99. experienceA:SetPoint("TOPLEFT", 15, -15)
  100. experienceA:SetFormattedText("Experience: ")
  101.  
  102.  
  103. experience1:SetPoint("TOPRIGHT", -15, -15)
  104. experience1:SetFormattedText(unitXp .. " / " .. maxXp)
  105.  
  106.  
  107. experienceB:SetPoint("TOPLEFT", 15, -30)
  108. experienceB:SetFormattedText("Extra rest XP:  ")
  109.  
  110.  
  111. experience2:SetPoint("TOPRIGHT", -15, -30)
  112. experience2:SetFormattedText("None")
  113. if xpExhaustion then
  114. print("here 1")
  115. experience2:SetFormattedText(xpExhaustion)
  116. else<span class="Apple-tab-span" style="white-space:pre"> </span>
  117. print("here 2")
  118. experience2:SetFormattedText("None")
  119. end
  120.  
  121.  
  122. experienceC:SetPoint("TOPLEFT", 15, -45)
  123. if not restState then
  124. experienceC:SetFormattedText("You are rested.")
  125. else
  126. experienceC:SetFormattedText("You are not rested.")
  127.  
  128.  
  129. experience3:SetPoint("RIGHT", -15, -15)
  130. experience3:SetFormattedText(killToLevel())
  131.  
  132.  
  133. end
  134. end
  135.  
  136.  
  137. draw()
  138.  
  139.  
  140. --updates variables when called
  141. local function updateVariables()
  142. if debugtrue then
  143. print("updatevariables function")
  144. end
  145. unitXp = UnitXP("player")
  146. maxXp = UnitXPMax("player")
  147. restState = GetRestState()
  148. xpDisabled = IsXPUserDisabled()
  149. petXp = GetPetExperience()
  150. xpExhaustion = GetXPExhaustion()
  151. draw()
  152. end
  153.  
  154.  
  155. updateVariables()
  156.  
  157.  
  158. --Updates frames when XP is gained
  159. local frame = CreateFrame("FRAME", "FooAddonFrame");
  160. frame:RegisterEvent("PLAYER_XP_UPDATE");
  161. local function eventHandler(self, event, ...)
  162. if debugtrue then
  163. print("eventHandler xp update function")
  164. end
  165. updateVariables()
  166. print(killnum .. ": after updateVariables")
  167. end
  168. frame:SetScript("OnEvent", eventHandler);

so, before reload, after startup



and after startup and after reload

  Reply With Quote
09-30-16, 12:53 PM   #2
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,877
The game doesn't know about your character until after the PLAYER_LOGIN event so things like your:

Code:
local unitXp = UnitXP("player")
won't be getting the correct information first time in.

Register the PLAYER_LOGIN event and do all you initialisation there.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.
  Reply With Quote
10-01-16, 11:37 AM   #3
zack1040
A Defias Bandit
Join Date: Sep 2016
Posts: 2
Thumbs up

finally got this updating at start, thanks for the help Fizzlemizz!
this is what i ended up with:

Lua Code:
  1. --Updates values when the player logs in
  2. local loginEvent = CreateFrame("FRAME", "LoginAddonFrame");
  3. loginEvent:RegisterEvent("PLAYER_LOGIN");
  4. local function eventHandler(self, event, ...)
  5.     unitXp          = UnitXP("player")
  6.     maxXp           = UnitXPMax("player")
  7.     restState       = GetRestState()
  8.     xpDisabled      = IsXPUserDisabled()
  9.     petXp           = GetPetExperience()
  10.     xpExhaustion    = GetXPExhaustion()
  11.     if debugtrue then
  12.         print("EVENT: player login")
  13.         print("LOADED INFORMATION")
  14.         print(unitXp)
  15.         print(maxXp)
  16.         print(restState)
  17.         print(xpDisabled)
  18.         print(petXp)
  19.         print(xpExhaustion)
  20.         print ("============")
  21.     end
  22. end
  23.  
  24. loginEvent:SetScript("OnEvent", eventHandler);

then im not sure if this part was necessary, but added this code at the bottom
Lua Code:
  1. --Updates frames when player enters world
  2. local enterWorld = CreateFrame("FRAME", "FooAddonFrame");
  3. enterWorld:RegisterEvent("PLAYER_ENTERING_WORLD");
  4. local function eventHandler(self, event, ...)
  5.     if debugtrue then
  6.         print("eventHandler update variables on player enter world")
  7.     end
  8.     draw()
  9. end
  10. enterWorld:SetScript("OnEvent", eventHandler);
  Reply With Quote
10-01-16, 11:44 AM   #4
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,877
You don't need two frames, just register both events to a single frame:
Lua Code:
  1. local loginEvent = CreateFrame("FRAME", "LoginAddonFrame");
  2.     loginEvent:RegisterEvent("PLAYER_LOGIN");
  3.     loginEvent:RegisterEvent("PLAYER_ENTERING_WORLD");
  4.     local function eventHandler(self, event, ...)
  5.         if event == "PLAYER_LOGIN" then
  6.             unitXp          = UnitXP("player")
  7.             maxXp           = UnitXPMax("player")
  8.             restState       = GetRestState()
  9.             xpDisabled      = IsXPUserDisabled()
  10.             petXp           = GetPetExperience()
  11.             xpExhaustion    = GetXPExhaustion()
  12.             if debugtrue then
  13.                 print("EVENT: player login")
  14.                 print("LOADED INFORMATION")
  15.                 print(unitXp)
  16.                 print(maxXp)
  17.                 print(restState)
  18.                 print(xpDisabled)
  19.                 print(petXp)
  20.                 print(xpExhaustion)
  21.                 print ("============")
  22.             end
  23.         elseif event == "PLAYER_ENTERING_WORLD" then -- could just be else as you only have the two events registered
  24.             if debugtrue then
  25.                 print("eventHandler update variables on player enter world")
  26.             end
  27.             draw()
  28.         end
  29.     end
  30.     loginEvent:SetScript("OnEvent", eventHandler);

The local draw() function would have to be defined before you create the frame.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 10-01-16 at 11:48 AM.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Lua Addon programming


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