View Single Post
12-22-09, 04:27 AM   #5
Taroven
A Cyclonian
AddOn Author - Click to view addons
Join Date: Dec 2006
Posts: 49
Originally Posted by thejoester View Post
So I created a function to check the value and if it was nil then I assigned it "none" and that fixed it.
Better trick might be to use a quick inline expression. You can also do the same to validate the spellID.
Code:
debugLog = "DEBUG: Spell ID:" .. SID or "none"
...which basically reads like "if SID then spellID else none". For extra conditions:
Code:
foo = foo and bar or lol and boo or bar == boo and nil
--[[ if foo then bar
elseif lol then boo
elseif bar == boo then nil
]]

-- In practical application, ripped from EventHorizon's guts...
for i in pairs(frame) do
	local gr = frame[i].glyphrefresh or nil
	local gs = frame[i].glyphstacks or nil
	if gr and (gr[3] == spellname) then
		if gs[destguid] then
			gs[destguid] = gs[destguid] - 1
			frame[i].stacks:SetText(gs[destguid] > 0 and gs[destguid] or nil)
		end
		--debug("SUCCESS! "..gr[3].." has triggered "..frame[i].auraname)
	end
end
Edit/note: If working with tables, an entry that exists but is set to nil acts differently than a variable. If you just want to check if the table index is there and don't care about the contents, cool, but if you're doing anything that'll error on nil you need to specify what you want.
Code:
local foo = nil
print(foo or "LOL")
-- Result: "LOL"

local foo = {}
foo.bar = nil
print(foo.bar or "LOL")
-- Result: nil

-- Now to specify what we're looking for.
print(foo.bar ~= nil and foo.bar or "LOL")
-- Result: "LOL"

foo.bar = "HAITHAR"
print(foo.bar ~= nil and foo.bar or "LOL")
-- Result: "HAITHAR"
Hopefully that's all somewhat relevant and makes sense. Not something you're gonna use every line of code, but it does help a lot, especially when working with variables that have potential to nil out on you.
__________________
Former author of EventHorizon Continued and Other Releases.

Last edited by Taroven : 12-22-09 at 04:44 AM. Reason: Added stuff.
  Reply With Quote