Thread Tools Display Modes
06-14-16, 11:59 PM   #1
TimothyLuke
A Fallenroot Satyr
 
TimothyLuke's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 24
Access a MOD as an object

HI All,

I currently have a mod that works but I want to expose some of the data and functions to be able to be used by a second mod. While they are all in one mod everything works but would prefer to take a more modular approach. Is there an API or library framework I can use? I had a look at libStub but the main mod would be a mod that does things in its own right with or without the addon parts.

-- Draik
  Reply With Quote
06-15-16, 02:11 AM   #2
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,323
The Lua environment is shared amongst the entire UI system, meaning any global can be accessed by any addon. As such, you should try to use names that are unique, descriptive, and can be easily identified as belonging to your addon. Something like MyAddon_ExampleVariable.

It's also advised if you create separate addons as modules to your main addon, you list the main one as a dependency in the modules' ToC file. This makes sure the main addon loads before the modules. More info here.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)

Last edited by SDPhantom : 06-15-16 at 02:16 AM.
  Reply With Quote
06-15-16, 04:57 PM   #3
TimothyLuke
A Fallenroot Satyr
 
TimothyLuke's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 24
I've done that but I then hit a race condition. There are actions I need the main module to complete both before and after the the addon mods are loaded. eg:

Load Core
..... Load Optional Mod 1
..... Load Optional Mod n
Finish Core
But at the same time I also need the following case to work

Load Core
Finish Core
What I would like to achieve is:

Load Core Mod
Finish Core

Load Optional Mod n
Code:
local coremod = Global coreMod
coremod:loadData(variables)
coremod:performProcess(variables)
Could I expose the core mod as a Global Object?

Last edited by TimothyLuke : 06-15-16 at 05:47 PM.
  Reply With Quote
06-15-16, 09:01 PM   #4
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,240
Originally Posted by Huldrych View Post
Could I expose the core mod as a Global Object?
Certainly.
Code:
local CoreMod = {}
_G.CoreMod = CoreMod -- now in the global scope
It might be easier to post your code.
  Reply With Quote
06-15-16, 10:44 PM   #5
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,323
If you need a module to load while the core is loading, you can set LoadOnDemand in the ToC of the module. This allows the core to call LoadAddOn() when it needs the module to be loaded. I personally wouldn't recommend this route and would instead promote better core and module designs where race conditions don't exist.

Another thing to look at is if its actually necessary to split up an addon into other addons as modules. This should only be reserved for addons that want to include separate optional features or plugins. A better way to modularise an addon as part of having clean code is to put different sections of the addon as different files in the same addon, linked together by the ToC.

A key feature to modules within a addon is the shared table given to all Lua files of an addon. You can use this table to build and pass along an internal API that is more secure than using globals. To access this, I usually have this at the top of every Lua file.
Code:
local Name,Addon=...;
This stores the addon's folder name in Name and the shared table in Addon.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)

Last edited by SDPhantom : 06-15-16 at 10:59 PM.
  Reply With Quote
06-16-16, 01:13 AM   #6
TimothyLuke
A Fallenroot Satyr
 
TimothyLuke's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 24
Ok working through this further I have been enhancing Semlar's GnomeSequencer for Legion.

The Git repository for this is located here: https://github.com/TimothyLuke/DraikUI/tree/Legion

I have about 22 alts of different classes and have been using GS for a while.

The original GS is structured like so:
Errorhandler.lua <---- initial setup
Sequences.lua <---- Table of cast sequences. Sequences are loaded here via
local GNOME, Sequences = ...
This file doesn't exist out of the box and requires a person create the file and add in their code. If you are playing on multiple computers you have to manually sync this.
Core.lua <---- expands a sequence from sequences.lua into a secure button. This loads in the same sequences via
local GNOME, Sequences = ...
What most people like me ended up with was a massive Sequence.lua that was huge and hard to maintain

My first enhancement added some Spec ID management to this and the mod as uploaded here as Gnome Sequencer Enhanced looks like this and also some help meta data to the sequence.

ErrorHandler.lua
DraiksMacros/<<classname>>.lua <---- Each class has its own file for my macros e.g. paladin.lua mage.lua
Sequences.lua
Core.lua

This works as intended using the same
local GNOME, Sequences = ...
structure. I could put this on Github and sync that to my multiple computers.

---------------------------

Having gotten this far, what if other mod authors wanted to release their own compilation of macros. DBM, Skada, ... each have their own plugin system and a way of creating your own bundle of sequences would be better than repackaging into your own compilation .....

I tried to break these up into a GS-Core that contains
ErrorHandler.lua
Sequences.lua
Core.lua

to allow for backwards compatibility. If you had your sequences.lua and were happy doing that ... great

I then moved my macros to GS-Draik and had it depend on GS-Core. The problem is that this would load after GS-Core had initialised and completed its Core.lua routines. Again i didn't want to force the use of my extra add-on and wanted to be able to have a GS-Draik, a GS-newblevelling, GS-SuperPaladinCollection etc. The Core wouldn't know exactly how many of these existed prior to initialisation. Again as these were optional there may be none of these present at all.

I started by taking the initial
local GNOME, Sequences = ...
in error handler.lua and exposing the Sequences table as a Global. The GS-Draik files then loaded their sequences into the global.

After entering the world, GS-Core could read the sequences from GS-Draik but it wouldn't do the initialisation steps that occurred when they were the one mod. I can see the extra sequences are able to be seen in the GS-Core mod post load via some debugging but I am stuck. I can't then tell the GS-Core to rerun its initialisation with the new sequences.

Which brings me back where I am. -- Does this make sense?
  Reply With Quote
06-16-16, 02:41 AM   #7
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,323
In this case, you could set the modules as LoadOnDemand in their ToC as noted before. You can have the core scan for and load them when needed using code like this.
Lua Code:
  1. for i=1,GetNumAddOns() do
  2.     if not IsAddOnLoaded(i) and GetAddOnInfo(i):find("^GS%-") then
  3.         LoadAddOn(i);
  4.     end
  5. end
This scans through all addons and loads the ones starting with GS-.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote
06-16-16, 04:21 AM   #8
sezz
A Chromatic Dragonspawn
AddOn Author - Click to view addons
Join Date: Apr 2008
Posts: 158
Originally Posted by Huldrych View Post
After entering the world, GS-Core could read the sequences from GS-Draik but it wouldn't do the initialisation steps that occurred when they were the one mod.
you could create the buttons later (on PLAYER_LOGIN for example), this allows other addons to add their sequences first.
  Reply With Quote
06-17-16, 02:13 AM   #9
TimothyLuke
A Fallenroot Satyr
 
TimothyLuke's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 24
Thank you all

The Load on Demand solution was the best outcome and it works.
  Reply With Quote

WoWInterface » Developer Discussions » General Authoring Discussion » Access a MOD as an object


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