View Single Post
03/10/23, 04:16 PM   #10
Baertram
Super Moderator
 
Baertram's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 4,989
Well Ive never used a callback
And how did you let your addon properly load then if you never did use a callback so far?
Every EVENT_MANAGER:RegisterFor is using a callbackFunction that is called as the event fires, e.g. the OnAddonLoaded function you have defined for your EVENT_ADD_ON_LOADED (hopefully).

You cannot do such:
Lua Code:
  1. AddCustomMenuItem("MyAddon Save",  MyAddon.SavePlayerContext(playerName))
  2. end

AddCustomMenuItem got a dedicated signature, which means you need to pass in a function as the 2nd parameter and not a result of a function, which is what would be passed in if you "call" your function MyAddon.SavePlayerContext(playerName) there!
You need to add it with a closure around it so it does not get called as you pass it in the parameter, but get's called as it's own function as the 2nd param is needed:

Lua Code:
  1. AddCustomMenuItem("MyAddon Save",  function() MyAddon.SavePlayerContext(playerName) end)

This function() yourFunctionNameWithParam(param1, param2) end is doing the following:
It will pass in a function (an anonymous one without name) and as this function is called at any time within LibCustomMenu code, the internal function MyAddon.SavePlayerContext(playerName) will be called, at that time (as the context emnu entry is clicked).

Else you'd just pass in the return value of MyAddon.SavePlayerContext(playerName), at the point the AddCustomMenuItem("MyAddon Save", MyAddon.SavePlayerContext(playerName)) was read from the code interpreter (means as the code is read the first time which would be way too early even before the contex menu was opened)!

And if that is nil as your function MyAddon.SavePlayerContext(playerName)) does not return anything, this is why you get the nil error but function expected !
If would work again if your function MyAddon.SavePlayerContext(playerName) would return a function e.g. return function() doSomething(playerName) end.

Last edited by Baertram : 03/10/23 at 04:20 PM.
  Reply With Quote