Player entering world event - WoWInterface
Thread Tools Display Modes
10-17-20, 05:12 AM   #1
A Cyclonian
Join Date: Apr 2009
Posts: 43
Player entering world event


Quick question...

If i register player_entering_world event and upon it i want to print this:
GetSpellPowerCost("Blizzard(Rank 1)")

I will get nil.

Same goes for addon_loaded event ... at least 1st time (since addon_loaded fires more than once ?! )

Is there any event that will at 1st login fire after everything is loaded?

Thank you.
  Reply With Quote
10-17-20, 06:33 AM   #2
Premium Member
Rilgamon's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 818
You might try to use the spellid instead of the name.
"Using spellName or spellLink only returns the info if the spell is in your spellbook; otherwise it returns nil."

So perhaps your spellbook is not yet available (event SPELLS_CHANGED should tell you when its loaded).
The cataclysm broke the world ... and the pandas could not fix it!
  Reply With Quote
10-17-20, 08:45 AM   #3
A Cliff Giant
DahkCeles's Avatar
AddOn Author - Click to view addons
Join Date: Jun 2020
Posts: 73
I've found there's two sources of delay, especially in Classic: (a) asynchronously querying the localized spell name; and (b) waiting for the spell book to be available at PLAYER_LOGIN.

I quickly wrote the snippet below to convert spellIDs into localized names, but not to check the spell book until at least PLAYER_LOGIN and possibly later if asynchronous queries return info even later. This snippet also deletes non-existing spellIDs, which could happen for classic vs. retail differences.

If you are creating click-cast frames for use during combat, then you should change this to do as much as possible at PLAYER_LOGIN even if some spells are still slow loading -- its possible the player is logging in during a raid fight after the game crashed, and they will need to rejoin the fight on a best-effort basis before combat lockdown kicks in.

Lua Code:
  1. local spells = {1459, 23028, 1008,  604}  -- mage buffs
  2. local spellsLoaded = 0
  4. local function updateSpells()
  5.   if (spellsLoaded == #spells) then
  6.     for __, spellName in ipairs(spells) do
  7.       if (GetSpellInfo(spellName)) then
  8.         -- doSomething()
  9.       else
  10.         -- doSomethingElse()
  11.       end
  12.     end
  13.   end
  14. end
  16. local playerIsLoggedIn = false
  17. local f = CreateFrame("Frame")
  18. f:RegisterEvent("PLAYER_LOGIN")
  19. f:SetScript("OnEvent", function()
  20.   updateSpells()
  21.   playerIsLoggedIn = true
  22.   f:RegisterEvent("LEARNED_SELL_IN_TAB")
  23.   pcall(f.RegisterEvent, f, "ACTIVE_TALENT_GROUP_CHANGED") -- retail only
  24.   f:SetScript("OnEvent", updateSpells)
  25. end
  27. local i = 1
  28. while spells[i] do
  29.   local entry = i  -- This local reference avoids race conditions between the loop and async queries.
  30.   if (C_Spell.DoesSpellExist(entry)) then
  31.     local spell = Spell:CreateFromSpellID(entry)
  32.     spell:ContinueOnSpellLoad(function()
  33.       spells[entry] = GetSpellInfo(spells[entry])
  34.       spellsLoaded = spellsLoaded + 1
  35.       if (playerLoginHappened) then
  36.         updateSpells()
  37.       end
  38.     end)
  39.     i = i + 1  -- moves to the next spell
  40.   else
  41.     -- The spell doesn't exist; maybe because of classic vs retail?
  42.     tremove(spells, i)  -- shifts the next spell forward
  43.   end
  44. end
  Reply With Quote
10-17-20, 09:41 AM   #4
A Cyclonian
Join Date: Apr 2009
Posts: 43
@Rilgamon spells_changed event also first time return nil...

@DahkCeles nice little piece of code... thank you for it... i will make use of it... im not doing anything fancy... all my code is for my own personal use.. i just want to display for my farming mage how many blizzards can i do with current mana and with regard of mana regen some prediction of how many ticks needed for next +1
  Reply With Quote
10-17-20, 10:04 AM   #5
A Cliff Giant
DahkCeles's Avatar
AddOn Author - Click to view addons
Join Date: Jun 2020
Posts: 73
In that case, you might be able to get away with just using spellID 190356. I'm not sure if this corresponds to any particular ranks or classic vs retail... this is just grabbing it off wowpedia really fast.

The script below loads the spell data, and once the data is available it then creates a font string and scripts it to update any time your mana changes.

Lua Code:
  1. local spell = CreateSpellFromSpellID(190356)
  2. spell:ContinueOnSpellLoad(function()
  3.   local maxCost = GetSpellPowerCost(190356)[1].cost
  4.   local f = CreateFrame("Frame")
  5.   f:SetPoint("CENTER")
  6.   f:SetSize(25,25)
  7.   local fs = f:CreateFontString(nil, "ARTWORK")
  8.   fs:SetAllPoints()
  9.   f:RegisterUnitEvent("UNIT_POWER_UPDATE", "player")
  10.   f:SetScript("OnEvent", function()
  11.     if (manaCost) then
  12.       fs:SetText(string.format("%d", UnitPower("player", 0)/maxCost))
  13.     end
  14.   end)
  15. end)

Edit: disclaimer, I'm not a frost mage and I haven't tested this code.
  Reply With Quote
10-17-20, 10:37 AM   #6
A Cyclonian
Join Date: Apr 2009
Posts: 43
Yup.. i did something similar... thank you.

Problem with spellid is that if you are not 60 and want the calc to scale with max rank of spell.

Anyhow.. i fixed my problem... it works fine... thank all for assistance. Cheers
  Reply With Quote

WoWInterface » Developer Discussions » General Authoring Discussion » Player entering world event

Thread Tools
Display Modes

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