I found this tricky at first also by the way, but once it clicks it'll seem simple.
The value of func can be either a string, which is the name of the function to call, or an actual function that you define as the table entry itself.
You're actually trying to call a function, which by itself is neither. What you need to do is set "func" to be either the string "SetAfk", or an anonymous function that calls SetAfk() and pass whatever it returns.
You can define a function something like this:
function()
somevar = 'arse'
return somevar
end
So, here's what you need to do (I've split off the table that you're passing to :RegisterChatCommand() to try and make it more clear):
either:
local myopts = {
type = 'execute',
name = 'cs',
desc = 'cs',
func = function() return SetAfk() end,
}
FayAfk:RegisterChatCommand({ '/fayafk' }, myopts)
or:
local myopts = {
type = 'execute',
name = 'cs',
desc = 'cs',
func = 'SetAfk',
}
But, I keep getting the following error:
Message: ..\AddOns\FayAfk\FayAfk.lua line 25:
attempt to call global 'SetAfk' (a nil value)
What am I doing wrong...
From my script:
Anyway, there are *hundreds* of examples of this on the SVN.
Don't be lazy. Read code.
I've read all the examples. Many times.
I have downloaded mods and tried different stuff hundreds of times until my head felt like it was going to explode.
Don't Understand it? Yes.
Lazy? No.
Sorry, thought classroom was for when you didn't understand something.
:(
The value of func can be either a string, which is the name of the function to call, or an actual function that you define as the table entry itself.
You're actually trying to call a function, which by itself is neither. What you need to do is set "func" to be either the string "SetAfk", or an anonymous function that calls SetAfk() and pass whatever it returns.
You can define a function something like this:
So, here's what you need to do (I've split off the table that you're passing to :RegisterChatCommand() to try and make it more clear):
either:
or:
Hope that helps (and is correct!).
cheers,
- Fin
That helps a lot.
if your function is part of the addon object...
then use "func = 'Blorp'" This ensures 'self' is passed correctly.
If you don't need any args passed, you can pass the function object, "func = Blah.Blorp"
or
"func = fump"
Or, if you need to ensure certain args are passed, "func = function() fump("blah", "smoo") end"