ESOUI - View Single Post - Some LUA questions
View Single Post
03/18/19, 05:58 AM   #2
Baertram's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 3,087
Hey, I recently started messing around with Addons in ESO. I've built my first basic addon and there are still some questions left about structering code and XML fragments.

1. There are several ways to call functions

local example = function(x, y, z)
local example = Addon.function(x, y, z)
local example = Addon:function(x, y, z)

When do I use which call? Has it something to do if the call is in a external file?
Your choice
function is just a function.

Addon is a table (https://wiki.esoui.com/LUA_Tables) and you add a function to it via Addon.function. So you are able to access Addon within your code if it's defined local, or without local it will be globally accessible from your AND other addons. So be sure to define a unique name like MyAddonsName = {} instead of Addon = {} (which other addons might use as well).
You use a table like Addon = Addon or {} at the start of several lua files of your project if you want to pass variables etc. to different lua files of your project. This way you can make the files "communicate" with each other (and other addons use your functions as well, e.g. build an API, or at least check if your global table exists and know your addon is active this way).

Addon: says you are using kind of Object oriented programming. lua does not really support it, but uses metatables (search for it please) to define objects and re-use predefined functions and attributes from parent objects.
Addon:callFunction(parameter1) is the same like Addon.callFunction(objectName, parameter1).

2. When exactly do I need to tell the Event_Manager to shut down the listener and will it be harmful for the latency or FPS if I keep the listener alive for the whole time?

I know everytime when I do not need the listener anymore I can turn it off but is it necessary in terms of performance?
Yes it is. Depends on WHAT you check and how often the events trigger. e.g. combat events should ALWAYS use an event filter as well:

And some other events need a check that it won't be called if you are not in a dungeon or the circumstances you like your addon to work in. If users deactivate a setting (libAddonMenu-2.0) in your addon it often makes sense to add the parameter "requiresReload = true" to this LAM setting. And at your event registering functions (e.g. in event_add_on_loaded or event_player_Activated) check if this setting is enabled or disabled+ only register the event callback funcs if it is enabled.

3. When is it usefull to make new sub folders with external .lua files except for languages and libs?

I can imagine that it's usefull for e.g. constants which you use through the whole addon but makes it sense to make a, lets say, combat sub folder with a combat.lua and a synergy.lua file in it which deal with all the combat stuff?

I've built a settings menu with a combat tracker ON/OFF option. When the tracker is ON, the [setFunc] should turn on the EM:RegisterForEvent - EVENT_PLAYER_COMBAT_STATE with a OnPlayerCombatState callback function which lays in a combat/combat.lua file. For some reason it cannot find the OnPlayerCombatState function. (Yes, I've put the files in the Addon.txt) On the other hand when I scamble everything into one file, everything works as intented.
This again depends on how you like it. Structure it the way you like to.
For your combat example see my answer to 2. about the lam settings requiresReload + event_player_Activated to register the event properly then. Otherwise it might be too late (depening on the event you are using) or not working properly anymore.
And for combat please ALWAYS use the event filters or this might be a real FPS or performance issue as they trigger for EACH user in your group e.g. + other addons use it a lot too.

BUT PLEASE do NOT include libraries anymore in the addons subfolders!
Let the user install them as standalone libraries (like addons, via Minion e.g.) and just put in your txt file the tag ##DependsOn: Library1 Library2
This will assure you are not shipping embedded libraries with old versions which might be a problem after updates again! And this assures the idea of libraries is fullfilled.

lua is reading your code from top to the bottom of a file.
If you define the func OnPlayerCombatState in the same file where you register the event and want to acccess the func OnPlayerCombatState you need to assure that the func OnPlayerCombatState is defined MORE TO THE TOP of the file than the event registering.
If you define the func in anothe file you need to be sure to put it into your addon table like
in file /src/combat.lua
function myAddonName.OnPlayerCombatState(...)
in file myAddonName.lua
event_manager:registerforevent(myAddonNme.name, EVENT_..., myAddonName.OnPlayerCombatState)

See my answer to your question 1 for that as well.

4. Are there any tutorials regarding building a UI?

I just want to start with a simple resizable rectangle background with a close button on the top right hand side and some information in it like labels or controls (lists, dropdown boxes).
As far as I know you can build the UI without touching a XML file but it also works the other way around. What is the better option?
You need to advance in the parts of this tutorial but it should help you.
Again this depends on your ideas and likes.
I'd go with XML as a base and use lua functions to update some stuff dynamically like re-anchoring the controls etc. if needed.

You can also build it totally within lua source code and just define the controls in there without XML.
XML got some nice stuff like using "templates" for e.g. a backdrop (black rectangle background in eso) or an input text field etc.
Maybe spy on other addons how they do it

Last edited by Baertram : 03/18/19 at 06:11 AM.
  Reply With Quote