Thread Tools Display Modes
Yesterday, 06:31 PM   #1
entr0py42
Join Date: Jul 2024
Posts: 1
Cannot initialize my addon

Hi all,
I'm trying to get into creating addons. I'm working on an addon that displays the total value of the items you've picked.

No matter what I try, I cannot initialize it. I've tried several approaches and inspected a few addons to understand how they do it. I think the problem is OOP related.

The error I get:
Code:
user:/AddOns/SimpleFarmTrack/SimpleFarmTrack.lua:37: function expected instead of nil
stack traceback:
user:/AddOns/SimpleFarmTrack/SimpleFarmTrack.lua:37: in function '(anonymous)'
The code:
Lua Code:
  1. local ADDON_NAME = "SimpleFarmTrack"
  2. SimpleFarmTrack = {}
  3. local async = LibAsync
  4.  
  5.  
  6. local function GetItemPrice(itemLink)
  7.     local price = LibPrice.ItemLinkToPriceGold(itemLink)
  8.     if (price == nil or price == 0) then price = GetItemLinkValue(itemLink, true) end
  9.     return price
  10. end
  11.  
  12.  
  13. local total = 0
  14. function SimpleFarmTrack:OnItemLoot(eventCode, name, itemLink, quantity)
  15.   total = total + GetItemPrice(itemLink) * quantity
  16.   SimpleFarmTrackLabel:SetText(string.format("%.2f",total))
  17. end
  18.  
  19. function SimpleFarmTrack:AddEventHandlers()
  20.   EVENT_MANAGER:RegisterForEvent(
  21.     ADDON_NAME,
  22.     EVENT_LOOT_RECEIVED,
  23.     function(...)
  24.       async:Call(self:OnItemLoot(...))
  25.     end
  26.   )
  27. end
  28.  
  29. function SimpleFarmTrack:OnAddOnLoaded(event, addonName)
  30.   if addonName == ADDON_NAME then self:AddEventHandlers() end
  31. end
  32.  
  33. EVENT_MANAGER:RegisterForEvent(
  34.     ADDON_NAME,
  35.     EVENT_ADD_ON_LOADED,
  36.     function(...)
  37.       SimpleFarmTrack:OnAddOnLoaded(...)
  38.     end
  39. )


If I remove the entire EVENT_MANAGER part at the end and replace it with
Lua Code:
  1. SimpleFarmTrack:AddEventHandlers()
it works.

Last edited by entr0py42 : Yesterday at 06:55 PM. Reason: discovered some workaround
  Reply With Quote
Today, 02:32 AM   #2
Baertram
Super Moderator
 
Baertram's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 5,111
Do not use : notation for simple tables (SimpleFarmTrack = {}), that's only meant to be used for objects created from ZO_Object:New (SimpleFarmTrack = ZO_Whatever:New() ) where you want top reference the object via self!
Example would be ZO_Smithing class -> Cretaed object SMITHING, where self references SMITHING then.
Read the Wiki about this or search the forums here for more info, use search term "notation" or "object" or "class" and similar.

Code:
SMITHING:DoSomething(param1, param2)
SMITHING.DoSomething(SMITHING, param1, param2)
Here you can see what functions expect to have as params/signature if you use the different types of notations. If you use object notation : and call the function only with ., the 1st param1 must be the object reference! If you use the : it will automatically use internally self = the object reference. But: If the object was only created as {} table the : notation might not be working (usually it does, and even self does work inside the functions with :, but it's somehow not correct then).
So your self:AddEventHandlers() should rather be SimpleFarmTrack.AddEventHandlers() as self is SipleFarmTrack and it is a table, so . notation instead of : and usage of self.

To ease it:
Change your addons code to simple . Notation for your function definitions and calls. Example:

Lua Code:
  1. function SimpleFarmTrack.OnAddOnLoaded(eventId, addOnName)
  2. ...
  3. EVENT_MANAGER:RegisterForEvent(
  4.     ADDON_NAME,
  5.     EVENT_ADD_ON_LOADED,
  6.       SimpleFarmTrack.OnAddOnLoaded
  7. )
That way you do not have to wrap each call to the functions in an anonymous function(...) end around them, and can call them directly via table.function(params)

And in your OnAddOnLoaded event Handler make sure to unregister the EVENT_ADD_ON_LOADED again so your function OnAddOnLoaded won't fire and run for each other addon after your was was found!

Last edited by Baertram : Today at 05:08 AM.
  Reply With Quote
Today, 03:23 AM   #3
Calamath
AddOn Author - Click to view addons
Join Date: Aug 2019
Posts: 40
In my opinion, I do not see any particular problem in your short Lua code that would cause the error.

The SimpleFarmTrack table must have been overwritten and destroyed elsewhere after the main chunk was executed.

You could try /script d(SimpleFarmTrack) to see if it is broken.
Even if my guess is correct, it is not my job to find the culprit....
  Reply With Quote
Today, 03:44 AM   #4
votan
 
votan's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2014
Posts: 579
Originally Posted by Calamath View Post
In my opinion, I do not see any particular problem in your short Lua code that would cause the error.

The SimpleFarmTrack table must have been overwritten and destroyed elsewhere after the main chunk was executed.

You could try /script d(SimpleFarmTrack) to see if it is broken.
Even if my guess is correct, it is not my job to find the culprit....
For example a common mistake is to name your XML GUI root element the same as your addon.
  Reply With Quote
Today, 05:07 AM   #5
Baertram
Super Moderator
 
Baertram's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 5,111
Originally Posted by votan View Post
For example a common mistake is to name your XML GUI root element the same as your addon.
Or you define in your manifest txt file:
Code:
## SavedVariables: SimpleFarmTrack
The tablename you specify here is created in global table G at EVENT_ADD_ON_LOADED , and if that is the same as your already created SimpleFarmTrack table you overwrite it!
So use something like SimpleFarmTrack_SV instead
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » Cannot initialize my addon

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off