Thread Tools Display Modes
07-17-10, 10:47 PM   #1
Helminthophobe
Premium Member
 
Helminthophobe's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2009
Posts: 15
Gold Earned in a Session

I've written a small, personal addon that deals with gold and mail to be sent to my bank toon. I've run into a little problem and I'm stuck. The addon is a small frame that shows a tooltip with my current gold and the amount of gold I have gained or lost that session along with some other information.

The XML has the following:
Code:
<OnEnter>
  tooltip()
</OnEnter>
This is my basic code:
Code:
local startgold = GetMoney()

function  tooltip()
  local money = GetMoney()
  GameTooltip:SetOwner(frame, "ANCHOR_NONE")
  GameTooltip:SetPoint("TOPLEFT", "frame", "BOTTOMLEFT")
  GameTooltip:AddLine("Title", .4,.7,.8)
  GameTooltip:AddDoubleLine("Current Gold:", GetCoinTextureString(money))
  if startgold >= money then
    local sessiongold = startgold - money
    GameTooltip:AddDoubleLine("Session Gold:", GetCoinTextureString(sessiongold))
  else
    local sessiongold = money - startgold
    GameTooltip:AddDoubleLine("Session Gold:", GetCoinTextureString(sessiongold))
  end
  GameTooltip:Show()
end
I simplified the code for posting here. My variables and functions are named a little better in the actual code.

The problem is "startgold" is nil when the addon loads. I've tried to put it in the XML in <OnLoad>. I've tried to use events like ADDON_LOADED and PLAYER_ENTERING_WORLD but none seem to work. I've downloaded a couple mods that do this already but being a beginner at programming, I have trouble following their code. Can someone point me in the right direction? Any help would be greatly appreciated.
  Reply With Quote
07-18-10, 12:24 AM   #2
Gendr
A Deviate Faerie Dragon
AddOn Author - Click to view addons
Join Date: Mar 2008
Posts: 11
http://paste.wowace.com/opw3dgzay601furj/
  Reply With Quote
07-18-10, 01:37 PM   #3
Helminthophobe
Premium Member
 
Helminthophobe's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2009
Posts: 15
Thanks for the reply Gendr but that still gives me a nil value for "startgold ". I just used OnUpdate, and if "startgold" was nil, it would try to GetMoney() until it was no longer nil. I'm not sure if that is the best solution but it's a functioning solution.
  Reply With Quote
07-18-10, 02:03 PM   #4
numein
A Cyclonian
 
numein's Avatar
AddOn Author - Click to view addons
Join Date: Jun 2009
Posts: 43
Just wait until PLAYER_ENTERING_WORLD triggers before you call GetMoney(), and it should be fine. It doesn't work before the event...

If you don't need saved variables, watch for PLAYER_ENTERING_WORLD and call GetMoney() from the OnEvent function ...
  Reply With Quote
07-18-10, 02:23 PM   #5
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
Just to clarify, PEW (PLAYER_ENTERING_WORLD) still works for using saved variables.
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
07-18-10, 03:08 PM   #6
Helminthophobe
Premium Member
 
Helminthophobe's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2009
Posts: 15
I tried the OnEvent function with PLAYER_ENTERING_WORLD and it still came back as nil, which made me think it had to do with the order of things loading...
  Reply With Quote
07-18-10, 03:18 PM   #7
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
What does your code look like when using it after PEW?
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
07-18-10, 04:08 PM   #8
Helminthophobe
Premium Member
 
Helminthophobe's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2009
Posts: 15
It looks different now since I'm using OnUpdate but here is the basics of what I had...
Code:
function OnLoad(frame)
  frame:RegisterEvent("PLAYER_ENTERING_WORLD")
end

function OnEvent(frame, event, ...)
  if event == "PLAYER_ENTERING_WORLD" then
    startgold = GetMoney()
  end
end

function  tooltip()
  local money = GetMoney()
  GameTooltip:SetOwner(frame, "ANCHOR_NONE")
  GameTooltip:SetPoint("TOPLEFT", "frame", "BOTTOMLEFT")
  GameTooltip:AddLine("Title", .4,.7,.8)
  GameTooltip:AddDoubleLine("Current Gold:", GetCoinTextureString(money))
  if startgold >= money then
    local sessiongold = startgold - money
    GameTooltip:AddDoubleLine("Session Gold:", "-" .. GetCoinTextureString(sessiongold))
  else
    local sessiongold = money - startgold
    GameTooltip:AddDoubleLine("Session Gold:", "+" .. GetCoinTextureString(sessiongold))
  end
  GameTooltip:Show()
end
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Gold Earned in a Session


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