Thread Tools Display Modes
07/12/24, 12:54 AM   #1
static_recharge
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 38
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

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