Quantcast ACE3 - reusing addon table from other addon - WoWInterface
Thread Tools Display Modes
01-28-18, 04:19 PM   #1
robertito54321
A Murloc Raider
Join Date: Aug 2012
Posts: 8
ACE3 - reusing addon table from other addon

I was wondering if it was possible to access an addon's addon-table from within a different addon.

So basically I have two addons (2nd addon depends on 1st addon):
(dont mind the names, I changed them to illustrate my problem better)

Code:
local addonName1, addonTable1 = ...
local addon1 = LibStub("AceAddon-3.0"):NewAddon(addonName1)

addonTable1 = { --bunch of data }
Code:
local addonName2, addonTable2 = ...
local addon1 = LibStub("AceAddon-3.0"):GetAddon("addon1", true)
if not addon1 then return end
local addon2 = addon1:NewModule(addonName2)
So how could I access addonTable1?
I could wrap the variable in a function and have it return that specific table to me in addon2, but I was wondering if there was a more clever way of doing this.
Perhaps ace3 provides a clever way?
  Reply With Quote
01-28-18, 05:52 PM   #2
Eungavi
A Theradrim Guardian
Join Date: Nov 2017
Posts: 64
Afaik, the only way that you could access another addon's namespace is by globalizing it.

Addon1
Lua Code:
  1. local addonName1, addonTable1 = ...
  2.  
  3. _G[addonName1] = addonTable1

Addon2
Lua Code:
  1. local addonTable1 = addonName1

Last edited by Eungavi : 01-28-18 at 05:59 PM.
  Reply With Quote
01-28-18, 07:40 PM   #3
Kanegasi
A Frostmaul Preserver
 
Kanegasi's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2007
Posts: 250
For more clarification, the vararg ... is passed to every .lua file upon loading in WoW. It is a Blizzard implementation and is unique to every .toc file, so anything loaded by a single .toc file gets the same two arguments, the name of the .toc file and a table. Every file loaded after the first one gets the same table and any changes made to that table.

There is no way to access that vararg from another addon.

If you're doing something personal, you can just alter that addon's code to allow you to access it or just change the relevant code. If these are your addons, Engavi has a valid method.
  Reply With Quote
01-30-18, 07:45 AM   #4
myrroddin
A Molten Giant
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 912
The vararg method works well, but since you asked about Ace3, here's how to do it using that way.
Lua Code:
  1. -- First.lua
  2. -- this creates a table named MyAddOn that can be passed to any Lua file in your AddOn
  3. local MyAddOn = LibStub("AceAddon-3.0"):NewAddon("MyAddOn")
  4.  
  5. -- Second.lua
  6. -- get the table
  7. local MyAddOn = LibStub("AceAddon-3.0"):GetAddon("MyAddOn")
  8.  
  9. function MyAddOn:SomeFunction()
  10.     -- do something
  11. end
Please note that the crucial difference between using the vararg that is built into the game and Ace3's method is that the vararg can only be passed between Lua files within the same AddOn, while using GetAddon in Ace3 can access a totally different AddOn's functions.

For example (I haven't looked at the code to make certain, I'm just theorizing) let's say you want to write a module for HandyNotes.
Lua Code:
  1. -- Core.lua
  2. local HandyNotes = LibStub("AceAddon-3.0"):GetAddon("HandyNotes")
  3. local MyHNModule = HandyNotes:NewModule("CoolBeans")
  4.  
  5. -- access a function in HandyNotes called SomeFunction
  6. -- obviously there is not actual function named that
  7. -- this is just an example
  8. function HandyNotes:SomeFunction()
  9.     -- call the function CoolBeans from my module
  10.     -- each time HandyNotes:SomeFunction() is called,
  11.     -- CoolBeans will also be called
  12.     MyHNModule:CoolBeans()
  13. end
  14.  
  15. function MyHNModule:CoolBeans()
  16.     -- brew tasty coffee
  17. end
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » ACE3 - reusing addon table from other 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