WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Error 'attempt to concatenate global' (https://www.wowinterface.com/forums/showthread.php?t=29464)

thejoester 12-21-09 05:14 PM

Error 'attempt to concatenate global'
 
ok I am getting this error in my addon

Quote:

Date: 2009-12-21 16:09:03
ID: 1
Error occured in: Global
Count: 1
Message: ...terface\AddOns\JoesEclipseAlert\JoesEclipseAlert.lua line 84:
attempt to concatenate global 'SID' (a nil value)
Debug:
[C]: ?
...terface\AddOns\JoesEclipseAlert\JoesEclipseAlert.lua:84: JoesEclipseAlert_OnEvent()
[string "*:OnEvent"]:1:
[string "*:OnEvent"]:1
The line of code it is erroring on is

Code:

debugLog = "DEBUG: Spell ID:" .. SID;
I have delared this in my OnLoad() function and assigned it a value of one, but I still get this error.

any help?

Xrystal 12-21-09 05:17 PM

Prior to that line add the following :

print("DEBUG: Spell ID:",SID);

Then see what it shows in your chat window. Maybe it is getting to that line prior to the OnLoad call. You can test this by setting SID at the top of your lua file it is used in and set it to a value there.

Akryn 12-21-09 06:37 PM

Don't create a global named SID. Put it in a namespace or make it local. Calling a global that is just asking for some other addon to overwrite it.

Also, what Xrystal said. :)

thejoester 12-22-09 12:00 AM

Quote:

Originally Posted by Xrystal (Post 171273)
Prior to that line add the following :

print("DEBUG: Spell ID:",SID);

Then see what it shows in your chat window. Maybe it is getting to that line prior to the OnLoad call. You can test this by setting SID at the top of your lua file it is used in and set it to a value there.


OK i found the issue, I was assigning its value from UnitBuff() and passing it the spell id, however it doesn't always have a value for spell id.

So I created a function to check the value and if it was nil then I assigned it "none" and that fixed it.

Also, I only made it a global variable because I was trying to get rid of the error. I have made it local again.

Taroven 12-22-09 04:27 AM

Quote:

Originally Posted by thejoester (Post 171314)
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.

SDPhantom 12-28-09 01:18 AM

Quote:

Originally Posted by Taroven (Post 171336)
Code:

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


That's odd, because the expression nil acts as false, so this should return "LOL" as well regardless of whether or not nil was manually assigned to the variable or table entry.

Edit: Just to make sure, I ran this through Lua and it works as I had described.

nightcracker 12-28-09 04:57 AM

Quote:

Originally Posted by SDPhantom (Post 172217)
That's odd, because the expression nil acts as false, so this should return "LOL" as well regardless of whether or not nil was manually assigned to the variable or table entry.

Edit: Just to make sure, I ran this through Lua and it works as I had described.

Note that if you are trying to get a value from a table while the variable is no table at all, it will always fail, even when you have an "or" set. Then you should you this:

Code:

print(type(foo)=="table" and foo.bar or "LOL")

SDPhantom 12-28-09 05:50 AM

Quote:

Originally Posted by nightcracker (Post 172231)
Note that if you are trying to get a value from a table while the variable is no table at all, it will always fail, even when you have an "or" set.

While attempting to index a nil variable will result in Lua throwing an error and terminating execution, this is not the purpose of the above poster's explanation. My post was a correction on Lua's behavior in the specific operation performed.


All times are GMT -6. The time now is 05:20 PM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI