Thread Tools Display Modes
07/12/24, 12:54 AM   #1
static_recharge
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 40
Tables vs Objects Help

I am having a very hard time understanding what is going on here and would greatly appreciate any help. I am diving into using the ZO objects for add-on development to expand my horizons. I have created an object that does exactly what I want it to do with a global reference "LibTimer". If I use the global reference everything works as intended:
Code:
LibTimer:Start()
No errors.

However when I try to use a local shortform such as:
Code:
local LT = LibTimer --this is at the start of the file
...
LT:Start()
I get an error about indexing a nil value. When I try to debug the issue, LT does in fact = nil. I don't know why this is not working.
  Reply With Quote
07/12/24, 04:46 AM   #2
sirinsidiator
 
sirinsidiator's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 1,591
That's most likely a timing issue.

Calling LibTimer:Start() directly will look up the global variable then and there.
Assigning LibTimer to a local LT and then calling LT:Start() right in the same function will have the same effect:
Lua Code:
  1. function myFunc()
  2.     local LT = LibTimer
  3.     LT:Start()
  4. end

The problem is, when you assign local LT at the start of the file, it is looking up LT at a different time than when you call LT:Start():
Lua Code:
  1. local LT = LibTimer -- looks at LibTimer once when the file is loaded
  2. function myFunc()
  3.     LT:Start() -- LT is now nil
  4. end

To avoid that issue, you will have to understand when LibTimer is assigned to the global variable and then make sure your local is only assigned afterwards.
In most cases it should be enough to add the library to your DependsOn in the addon manifest. That way the game will always load library files before your own addon files.
However some libraries may not initialize during file load, but instead wait for an event like EVENT_ADD_ON_LOADED or EVENT_PLAYER_ACTIVATED or something else, which only occurs after files have loaded.
In that case you'll have to move the code from the root of your file into a function and call that function after the library has been fully loaded.
Such special cases are usually explained in the library description though.
  Reply With Quote
Today, 01:27 AM   #3
static_recharge
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 40
That seems to have fixed the issue. Thank you for your insight and help as always!
  Reply With Quote
Today, 01:36 AM   #4
static_recharge
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 40
I have a follow up question about library dependancies. Does the On_Addon_Loaded event for the library always happen before the dependant add-on? Or does that need to be checked manually?
  Reply With Quote
Today, 01:53 AM   #5
Baertram
Super Moderator
 
Baertram's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 5,099
The load order is defned via the ## DependsOn and ## OptionalDependsOn, regardless if an addon or library, in all txt files of all "addons".
All "addons" (each txt manifest file so to say) gets into the load order before all run through the EVENT_ADD_ON_LOADED in that order then.
And all "addons" fire their EVENT_ADD_ON_LOADED in that load order too.

If your addon needs a library as must have use ## DependsOn: LibraryName>=LibraryVersion (in the libraries ttx file, the tag ## AddOnVersion) and that assures your addon cannot load if that dependency is missing OR using an old version smaller then LibraryVersion!

If it optionally needs it use ## OptionalDependsOn: LibraryName=>LibraryVersion

Same works for any addon.

At the EVENT_ADD_ON_LOADED, as YOUR addon's name is found then in that loop over all addons, you can be sure the global variable LibLibraryName exists alread as it was loaded before your addon.
Same for all other addons, so if your addon optionally depends on OtherAddon1 the global variable OtherAddon1 exists as your addon loads (if that addon got a global variable).
And else you can always check in the EVENT_ADD_ON_LOADED if any addonName was loaded there, to find out if the other addon loaded already before your addon (or even after your addon if you do not unregister that event in your addon code!).


Attention:
EVENT_ADD_ON_LOADED must not always mean the other addon or library has finished preparing!
Always check the description and API of these addons and libraries, sometimes they provide a function you need to use to check if they are ready.
Some addons e.g. are ready at EVENT_PLAYER_ACTIVATED first, and some libraries start to do stuff at EVENT_ADD_ON_LOADED and finally fire a callback that your can use via CALLBACK_MANAGER or ThatOtherLibraryGlobal:APIFunctionIfLibIsReady() or ThatOtherLibraryGlobal.isLibReady boolean...

Last edited by Baertram : Today at 02:00 AM.
  Reply With Quote

ESOUI » Developer Discussions » General Authoring Discussion » Tables vs Objects Help


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