View Single Post
05/01/23, 04:31 AM   #6
Baertram
Super Moderator
 
Baertram's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 5,001
Basically the difference is not there, but it depends on if you create an object of a class (which basically is a table too, but uses metatable functionality of lua to create a "referencing copy" of the other tables -> object oriented inheritance).

You can reference the object created from myClass, via myClass:New(), by using self in the code then.

But if you define functions as myClass:functionName(param1, param2) and call the function from any code where you cannot directly use : notation, and need to switch to the . notation, the first param of the object created from myClass needs to be the reference to the object again.

myObjectCreatedFromMyClass.functionName(myObjectCreatedFromMyClass, param1, param2)
This replaces the self reference then.

So you may need to call your EVENT_MANAGER:RegisterForEvent("myAddonName_EventBlubb", EVENT_BLUBB, function(...) myObject.functionName(myObject, ...) )
where ... contains the eventId as 1st, then the other foloowing params of that event.
Or you need to use the : notation
EVENT_MANAGER:RegisterForEvent("myAddonName_EventBlubb", EVENT_BLUBB, function(...) myObject:functionName(...) )

You may not use myClass then, but the created object!
And if there is no object, just myClass = {} --simple table, then your call would be something like
EVENT_MANAGER:RegisterForEvent("myAddonName_EventBlubb", EVENT_BLUBB, function(...) myClass.functionName(...) )

If you directly call
EVENT_MANAGER:RegisterForEvent("myAddonName_EventBlubb", EVENT_BLUBB, myClass.functionName )
it may not work if your function was defined as
function myClass:functionName

If it was defined as
function myClass.functionName
it should work though IF the 1st param is the eventId then.

So there might be the need to capture it with an anonymous function around the myClass.functioName call

Last edited by Baertram : 05/01/23 at 04:35 AM.
  Reply With Quote