I recently wrote an addon called "Recipe Helper", trying to learn Lua (and programming in general)... The addon works as it should until I get to a profession trainer or vendor who sells recipes. I cant figure out what Im doing wrong, and hope I get some pointers as to how to fix my issue.
What the addon does is, let you know where to find a mat for a recipe (shift key down) when you mouse over this item on your bags, auction house, tradeskill window, etc.., it will also let you know which professions can use the mat with mouse over.
this is the addons core file.
rh_RawMats = {};
lineAdded = false
local function RH_MatsShow(tooltip, ...)
if IsShiftKeyDown() then
__,link = GameTooltip:GetItem()
id = tonumber(link:match('item:(%d+)'))
itemid = rh_RawMats[id]
if itemid and not lineAdded then
GameTooltip:AddLine("" ..itemid,0,0,0,0)
lineAdded = true
end
end
return rh_RawMats
end
local function RH_MatsHide(tooltip, ...)
lineAdded = false
end
GameTooltip:HookScript("OnTooltipSetItem", RH_MatsShow)
GameTooltip:HookScript("OnTooltipCleared", RH_MatsHide)
--mat proffession tooltip
rh_Whatreagent = {};
lineAdded = false
local function RH_ReagentShow(tooltip, ...)
_,link = GameTooltip:GetItem()
id = tonumber(link:match('item:(%d+)'))
myreagent = rh_Whatreagent[id];
if myreagent and not lineAdded then
GameTooltip:AddLine("|cff00bfff\nReagent used for:|cfff0e68c"..myreagent,0,0,0,0)
lineAdded = true
end
end
local function RH_ReagentHide(tooltip, ...)
lineAdded = false
end
GameTooltip:HookScript("OnTooltipSetItem", RH_ReagentShow)
GameTooltip:HookScript("OnTooltipCleared", RH_ReagentHide)
and this is just a small sample from the reagents file
local r = {
[1]="\nAlchemy",
[2]="\nBlacksmithing",
[3]="\nCooking",
[4]="\nEnchanting",
[5]="\nEngineering",
[6]="\nFirst Aid",
[7]="\nInscription",
[8]="\nJewelcrafting",
[9]="\nLeatherworking",
[10]="\nMining",
[11]="\nTailoring"
}
rh_tbl_reagents = {}
--[[Minor Healing Potion]]rh_Whatreagent[118]=r[1].."\n"
--[[Refreshing Spring Water]]rh_Whatreagent[159]=r[3].."\n"
--[[Goretusk Liver]]rh_Whatreagent[723]=r[3].."\n"
--[[Silverleaf]]rh_Whatreagent[765]=r[1].."\n"
--[[Chunk of Boar Meat]]rh_Whatreagent[769]=r[3].."\n"
--[[Malachite]]rh_Whatreagent[774]=r[2]..r[8].."\n"
--[[Light Hide]]rh_Whatreagent[783]=r[9].."\n"
this is the error I get when I visit a recipe vendor or a trainer
27x Recipe Helper\Recipe Helper-RH-v2.6.7.lua:31: attempt to index global 'link' (a nil value)
Recipe Helper\Recipe Helper-RH-v2.6.7.lua:31: in function <Recipe Helper\Recipe Helper.lua:29>
[C]: ?
[C]: in function `SetTrainerService'
[string "*:OnEnter"]:3: in function <[string "*:OnEnter"]:1>
Locals:
(*temporary) = <function> defined =[C]:-1
= <function> defined =[C]:-1
= <function> defined @Recipe Helper\Recipe Helper.lua:29
Well, you're creating a lot of unnecessary and generically-named global variables, so it's not outside the realm of possibility that your addon's variables are being unexpectedly overwritten by other addons, or even the default UI, if they are leaking globals by the same name. As a general rule, every variable you define should be local unless there is a specific need for it to be global (eg. saved variables must be global).
Anyway, I guess your error is coming from these lines:
_,link = GameTooltip:GetItem()
id = tonumber(link:match('item:(%d+)'))
...in which case it would indicate that the GameTooltip is not displaying an item when this function is called. This can happen if the item it's been asked to display isn't cached yet, and it has to retrieve item info from the server. You should add a check to make sure the link variable has a value before proceeding with the rest of your function:
_,link = GameTooltip:GetItem()
[b][color="#007f4f"]if not link then return end[/color][/b]
id = tonumber(link:match('item:(%d+)'))
There are aslo a couple other (mostly minor, aside from the many globals) issues with your code:
rh_RawMats = {}
[b][color="#007f4f"]local[/color][/b] lineAdded = false
local function RH_MatsShow(tooltip, ...)
if IsShiftKeyDown() then
[b][color="#007f4f"]local[/color][/b] __,link = GameTooltip:GetItem()
[b][color="#007f4f"]local[/color][/b] id = tonumber(link:match('item:(%d+)'))
[b][color="#007f4f"]local[/color][/b] itemid = rh_RawMats[id]
if itemid and not lineAdded then
GameTooltip:AddLine("" ..itemid,0,0,0,0)
[COLOR="#007f4f"][I]-- ^ There's no need to concatenate the itemid with a blank string,
-- and you are currently making this text black, which is nearly
-- invisible since the tooltip background is also black.[/I][/COLOR]
lineAdded = true
end
end
return rh_RawMats
[COLOR="#007f4f"][I]-- ^ There's not really any point in returning this value, since this
-- function is set as a script, whose return value is not captured or
-- used by anything. This value is also already available as a global
-- variable, so there's no need to call a function to get it if you
-- want to use it somewhere else. I'd just delete this line.[/I][/COLOR]
end
local function RH_MatsHide(tooltip, ...)
lineAdded = false
end
GameTooltip:HookScript("OnTooltipSetItem", RH_MatsShow)
GameTooltip:HookScript("OnTooltipCleared", RH_MatsHide)
--mat proffession tooltip
rh_Whatreagent = {}
lineAdded = false
[COLOR="#007f4f"][I]^ -- You already defined this variable on line 3, so you do not need to
-- define it again. If this code is actually in a different file, make
-- this variable local like the one on line 3. If it's in the same file
-- either remove this line or -- since this variable appears to serve
-- a different purpose -- make it local and give it a different name.[/I][/COLOR]
local function RH_ReagentShow(tooltip, ...)
[b][color="#007f4f"]local[/color][/b] _, link = GameTooltip:GetItem()
[b][color="#007f4f"]local[/color][/b] id = tonumber(link:match('item:(%d+)'))
[b][color="#007f4f"]local[/color][/b] myreagent = rh_Whatreagent[id];
if myreagent and not lineAdded then
GameTooltip:AddLine("|cff00bfff\nReagent used for:|cfff0e68c"..myreagent,0,0,0,0)
[COLOR="#007f4f"][I]-- ^ Same problem as the mats line. Stop making text black! This
-- text contains its own color codes, so it's not as bad, but in
-- this case there's no point in passing color values at all.
-- Also, don't prefix your added line with a line break, or use
-- line breaks in the middle of your text. If you want to add
-- multiple lines, call AddLine multiple times. If you want to
-- add a blank line, call AddLine with a " " space as the text.[/I][/COLOR]
lineAdded = true
end
end
local function RH_ReagentHide(tooltip, ...)
lineAdded = false
end
GameTooltip:HookScript("OnTooltipSetItem", RH_ReagentShow)
GameTooltip:HookScript("OnTooltipCleared", RH_ReagentHide)
_,link = GameTooltip:GetItem()
[b][color="#007f4f"]if not link then return end[/color][/b]
id = tonumber(link:match('item:(%d+)'))
This was the problem. I took your other suggestions and applied them as well. I had the all set as local at one time, but I thought switching them to global would magically fix the problem or something along those lines. In the addline portions, Im not sure why I was making the text all black. I guess I wasn't fully understanding the samples I found on various sites, this was the reason I had to add color to the line. I appreciate you clearing that up for me. I still have a lot to learn. I really appreciate you taking the time to help, Thank you
Rollback Post to RevisionRollBack
To post a comment, please login or register a new account.
What the addon does is, let you know where to find a mat for a recipe (shift key down) when you mouse over this item on your bags, auction house, tradeskill window, etc.., it will also let you know which professions can use the mat with mouse over.
this is the addons core file.
and this is just a small sample from the reagents file
and from cloth file...
this is the error I get when I visit a recipe vendor or a trainer
any input would be appreciated
Anyway, I guess your error is coming from these lines:
...in which case it would indicate that the GameTooltip is not displaying an item when this function is called. This can happen if the item it's been asked to display isn't cached yet, and it has to retrieve item info from the server. You should add a check to make sure the link variable has a value before proceeding with the rest of your function:
There are aslo a couple other (mostly minor, aside from the many globals) issues with your code:
This was the problem. I took your other suggestions and applied them as well. I had the all set as local at one time, but I thought switching them to global would magically fix the problem or something along those lines. In the addline portions, Im not sure why I was making the text all black. I guess I wasn't fully understanding the samples I found on various sites, this was the reason I had to add color to the line. I appreciate you clearing that up for me. I still have a lot to learn. I really appreciate you taking the time to help, Thank you