Thread Tools Display Modes
05-26-10, 11:07 AM   #1
mankeluvsit
An Onyxian Warder
 
mankeluvsit's Avatar
Join Date: Sep 2008
Posts: 354
includes?

okay i have my scripts setup like this
Code:
interface
| - addons
   | - suite
     | - modules
        | - nameplate.lua
        | - something.lua
        | - something2.lua
        | - settings.lua
     | - media
          | - font
          | - tga
| - oUF_suite
*suite is just there, for an example.
in modules i have settings.lua i want to use around my ui, so i use
Code:
local iamdefaultfont = "Interface\\AddOns\\suite\\media\\font\\mono.ttf"
local iampixelfont = "Interface\\AddOns\\suite\\media\\font\\pixel.ttf"
local iamblank = "Interface\\AddOns\\suite\\media\\tga\\blank"
local iamglow = "Interface\\AddOns\\suite\\media\\tga\\glow"
local iambg = "Interface\\AddOns\\suite\\media\\tga\\bg"
local iamcolor = RAID_CLASS_COLORS[select(2, UnitClass("player"))]
instead of having to copy that to each lua, how can i include the settings so i can just put "iampixelfont"?

Last edited by mankeluvsit : 05-27-10 at 11:46 AM.
  Reply With Quote
05-26-10, 11:19 AM   #2
v6o
An Onyxian Warder
AddOn Author - Click to view addons
Join Date: Mar 2009
Posts: 399
I think the only thing you can do is to make a global variable (or table) in a .lua file and put it in the top of your .toc file so it's loaded before your scripts.


I've not really run into this problem because most of my addons or scripts don't reuse the same textures but if they do I just put them in their own media folder inside the addon folder even though it will end up being duplicates.
__________________
I stopped playing back World of Warcraft in 2010 and I have no plans on returning.
This is a dead account and if you want to continue any of my addons or make a fork then feel free to do so.
This is your permission slip.

If you need to contact me, do so on Twitter @v6ooo

Best regards, v6.

Last edited by v6o : 05-26-10 at 11:23 AM.
  Reply With Quote
05-26-10, 04:14 PM   #3
Torhal
A Pyroguard Emberseer
 
Torhal's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 1,196
A couple of patches ago, Blizzard added a built-in mechanism to have an addon-local table, by this method:

Code:
local ADDON_NAME, ADDON_TABLE = ...
Simply insert that at the top of each file and use "ADDON_TABLE" or whatever you call it, as your addon-local container table.
__________________
Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Author of NPCScan and many other AddOns.
  Reply With Quote
05-26-10, 08:22 PM   #4
mankeluvsit
An Onyxian Warder
 
mankeluvsit's Avatar
Join Date: Sep 2008
Posts: 354
Originally Posted by Torhal View Post
A couple of patches ago, Blizzard added a built-in mechanism to have an addon-local table, by this method:

Code:
local ADDON_NAME, ADDON_TABLE = ...
Simply insert that at the top of each file and use "ADDON_TABLE" or whatever you call it, as your addon-local container table.
so it would be

local settings.lua, iamdefaultfont, ect ect ect ?
  Reply With Quote
05-26-10, 10:18 PM   #5
Torhal
A Pyroguard Emberseer
 
Torhal's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 1,196
Originally Posted by mankeluvsit View Post
so it would be

local settings.lua, iamdefaultfont, ect ect ect ?
No: ADDON_NAME is exactly that - the name of the AddOn as found in the ToC file. The ellipsis (...) in the file body contains the AddOn name and a table which is local to the AddOn itself. You can name them whatever you want, since they are variables, but "settings.lua" isn't a valid variable name - it would be interpreted as "table.member" by the Lua interpreter.
__________________
Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Author of NPCScan and many other AddOns.
  Reply With Quote
05-27-10, 11:27 AM   #6
mankeluvsit
An Onyxian Warder
 
mankeluvsit's Avatar
Join Date: Sep 2008
Posts: 354
Unhappy

Originally Posted by Torhal View Post
No: ADDON_NAME is exactly that - the name of the AddOn as found in the ToC file. The ellipsis (...) in the file body contains the AddOn name and a table which is local to the AddOn itself. You can name them whatever you want, since they are variables, but "settings.lua" isn't a valid variable name - it would be interpreted as "table.member" by the Lua interpreter.


confused :P

Last edited by mankeluvsit : 05-27-10 at 12:07 PM.
  Reply With Quote
05-27-10, 12:43 PM   #7
Torhal
A Pyroguard Emberseer
 
Torhal's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 1,196
At the top of every Lua file loaded from the ToC file -

Code:
local ADDON_NAME, common = ...
The first variable, which I have chosen to name "ADDON_NAME", is defined as the name of the AddOn as it is found in the ToC file.

The second variable, which I have called "common", is the table which is only visible to the files which are loaded from the ToC file.

Now, in every file, you can do things like this:

Code:
function common.DoSomething()
        -- Your code goes here
end
Since the "common" table is visible to every file in your AddOn, you can call common.DoSomething() from anywhere in your AddOn. This allows the convenience of using a global table without any of the drawbacks.
__________________
Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Author of NPCScan and many other AddOns.
  Reply With Quote
05-27-10, 03:53 PM   #8
numein
A Cyclonian
 
numein's Avatar
AddOn Author - Click to view addons
Join Date: Jun 2009
Posts: 43
@Torhal: ty for info, didn't know about addon local vars

@mankeluvsit
If you need your "settings" just in one addon - what Torhal sad :P
If you need it across more addons (as you sad "in your ui"), then you would have to make a global table, and put all your vars in it. e.g.
Code:
MY_SETTINGS = {
defaultfont = "Interface\\AddOns\\suite\\media\\font\\mono.ttf"
pixelfont = "Interface\\AddOns\\suite\\media\\font\\pixel.ttf"
}
You can put that table in your settings.lua, or wherever you like...

Then you could use those variables across all addons. e.g.
Code:
local myfont = MY_SETTINGS.defaultfont
or
Code:
local sometext = parrentFrame:CreateFontString(nil, 'OVERLAY', 'MY_SETTINGS.defaultfont')


Another way to do it would be something like:
Code:
MY_SETTINGS = {}
After that create/use your settings variables
Code:
MY_SETTINGS.defaultfont = "Interface\\AddOns\\suite\\media\\font\\mono.ttf"
Anyway, since MY_SETTINGS would be global, pick some unique name for it...
  Reply With Quote
05-27-10, 04:26 PM   #9
Xrystal
nUI Maintainer
 
Xrystal's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 5,934
Originally Posted by Torhal View Post
At the top of every Lua file loaded from the ToC file -

Code:
local ADDON_NAME, common = ...
The first variable, which I have chosen to name "ADDON_NAME", is defined as the name of the AddOn as it is found in the ToC file.

The second variable, which I have called "common", is the table which is only visible to the files which are loaded from the ToC file.

Now, in every file, you can do things like this:

Code:
function common.DoSomething()
        -- Your code goes here
end
Since the "common" table is visible to every file in your AddOn, you can call common.DoSomething() from anywhere in your AddOn. This allows the convenience of using a global table without any of the drawbacks.
It is also useful if you find there is a big block of code you use often enough you can put in a separate file as a function and just plug it into any addon that needs to use it. Thus, you can segregate the code into more managable portions

utils.lua
Code:
local addonName, addonTable = ...

addonTable["utils"] = {}

addonTable["utils"].SomeUsefulFunction = function(params)
   whatever code you want to put here
end
mainaddon.lua
Code:
local addonName,addonTable = ...
local utils = addonTable["utils"]

utils.SomeUsefulFunction(params)

Edit:
It can also be used to plug in a saved variable table.

fontDB.lua
Code:
local addonName, addonTable = ...

FontData = {}
addonTable["FontDB"] = {}

addonTable["FontDB"].Create = function(parentTable)
    if parentTable then
       parentTable.FontData = parentTable.FontData or {}
   else
       FontData = FontData or {}
   end       
end
This would give you the choice of either calling FontDB.Create() or FontDB.Create(ThisAddonsSavedVarsTable). Just add FontData in the TOC if you choose not to add it as a sub table.

I use this functionality alot in my latest addons and will slowly incorporate them into my next set of updates to my old ones. And for functions that do not need to be shared across addons or files I keep to the normal local function set up.
__________________


Characters:
Gwynedda - 70 - Demon Warlock
Galaviel - 65 - Resto Druid
Gamaliel - 61 - Disc Priest
Gwynytha - 60 - Survival Hunter
Lienae - 60 - Resto Shaman
Plus several others below level 60

Info Panel IDs : http://www.wowinterface.com/forums/s...818#post136818

Last edited by Xrystal : 05-27-10 at 04:36 PM.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » includes?


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