Reputation: 1079
Problem
Weird situation when consuming the UnitClass() function from World Of Warcrafts lua API. It is returning nil. The parameter for it is a "target name", see the reference here
If I reload though, I do get a value back as I would expect. So the following is happening:
What I Think Is Happening
I think I need to wait for a specific event, not sure what event though. If I invoke the same script from the chat window I get values back after I log in or reload, which is how I thought it should work from my lua script
Code
ClassColors.lua
function GetUnitClassColor(UnitName)
local class, classFileName = UnitClass(UnitName)
local color = RAID_CLASS_COLORS[classFileName]
print(class)
print(classFileName)
return color
end
core.lua (which has my initialisation code)
function GetCharacterData()
local playerName = UnitName("Player")
local playerRealm = GetRealmName()
local playerNameRealm = playerName .. '-' .. playerRealm
return playerNameRealm
end
----------------------Initialize----------------------------------
local mainFrame = CreateFrame("Frame")
mainFrame:RegisterEvent("ADDON_LOADED")
mainFrame:RegisterEvent("PLAYER_LOGOUT")
mainFrame:SetScript("OnEvent", function(self, event, arg1)
local currentCharacter = GetCharacterData()
local newCharacter = true
if event == "ADDON_LOADED" and arg1 == "MyAddOn" then
local characterClassColor = GetUnitClassColor(UnitName("Player"))
So you can see my intention was to get the color table returned and assigned to characterClassColor, but I get nothing at all just nill from class and classFileName variables in the GetUnitClassColor.
Thoughts on this? I've been looking at the events in the API docs to see if I'm missing anything obvious.
Upvotes: 2
Views: 1854
Reputation: 1079
I solved this by removing the GetUnitClassColor(UnitName)
function and simply invoking print(UnitClass("player"))
. Guess there is some internal logic I am not seeing where UnitClass can't handle the actual player's name at load time, but does recognise "player"
Upvotes: 2