I have made a lot of changes and now i get error
attempt to call method 'BGCallouts_func' (a nil value)
If i get it right the function cant be found so i must have made a fail in how to declare the function i guess . Can i get a hint what i missed and why function has a nil value?
function BGCallouts:ZONE_CHANGED_NEW_AREA()
self:BGCallouts_func(GetZoneText()) -- error
end
....
local function BGCallouts_func(msg)
-- BGCallouts:Print(msg)
if (msg=="Arathi Basin") then
ArathiBasin:Show(true)
elseif (msg=="Deepwind Gorge") then
DeepwindGorge:Show(true)
elseif (msg=="The Battle for Gilneas") then
Gilneas:Show(true)
elseif (msg=="Eye of the Storm") then
EotStorm:Show(true)
else
self:BGCallouts_hide()
end
return
end
That may prevent the error but it won't actually work the way you probably expect. When you just write "GetZoneText" that means you're passing a reference to that function -- you're not calling it and passing the string it returns, which is what I think you proably want.
When you use a colon between the table name and the function name, you're using "method notation" which means there's a hidden first argument called "self" that refers to the table. You should avoid mixing dots and colons, especially if you are just getting started with Lua.
If you have this:
local MyAddon = CreateFrame("Frame", "MyAddonFrame")
Then these ways of defining a function do the same thing:
function MyAddon:PrintStuff(msg) -- note the colon
DEFAULT_CHAT_FRAME:AddMessage(self:GetName() .. ": " .. msg)
end
function MyAddon.PrintStuff(self, msg) -- note the dot
DEFAULT_CHAT_FRAME:AddMessage(self:GetName() .. ": " .. msg)
end
And regardless of which style you used, these ways of calling the function do the same thing:
... but this way of calling it will result in an error:
MyAddon.PrintStuff("Hello!")
... and this way of defining the function is not the same:
function MyAddon.PrintStuff(msg)
DEFAULT_CHAT_FRAME:AddMessage(MyAddon:GetName() .. ": " .. msg)
-- ^ You have to write "MyAddon" instead of "self" here
-- because no variable named "self" is defined in this scope.
end
If you defined it that way, you can only call it like this:
MyAddon.PrintStuff("Hello!")
If you defined it that way and try to call it in either of the first two ways, you will get an error.
Rollback Post to RevisionRollBack
To post a comment, please login or register a new account.
attempt to call method 'BGCallouts_func' (a nil value)
If i get it right the function cant be found so i must have made a fail in how to declare the function i guess . Can i get a hint what i missed and why function has a nil value?
function BGCallouts:ZONE_CHANGED_NEW_AREA()
self:BGCallouts_func(GetZoneText()) -- error
end
....
local function BGCallouts_func(msg)
-- BGCallouts:Print(msg)
if (msg=="Arathi Basin") then
ArathiBasin:Show(true)
elseif (msg=="Deepwind Gorge") then
DeepwindGorge:Show(true)
elseif (msg=="The Battle for Gilneas") then
Gilneas:Show(true)
elseif (msg=="Eye of the Storm") then
EotStorm:Show(true)
else
self:BGCallouts_hide()
end
return
end
i used
BGCallouts.func = function(msg)
and for call
BGCallouts:func(GetZoneText)
BGCallouts:func(GetZoneText())
BGCallouts.func = function(self, msg)
If you have this:
Then these ways of defining a function do the same thing:
And regardless of which style you used, these ways of calling the function do the same thing:
... but this way of calling it will result in an error:
... and this way of defining the function is not the same:
If you defined it that way, you can only call it like this:
If you defined it that way and try to call it in either of the first two ways, you will get an error.