WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   includes? (https://www.wowinterface.com/forums/showthread.php?t=32763)

mankeluvsit 05-26-10 11:07 AM

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"?

v6o 05-26-10 11:19 AM

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.

Torhal 05-26-10 04:14 PM

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.

mankeluvsit 05-26-10 08:22 PM

Quote:

Originally Posted by Torhal (Post 189664)
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 ?

Torhal 05-26-10 10:18 PM

Quote:

Originally Posted by mankeluvsit (Post 189681)
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.

mankeluvsit 05-27-10 11:27 AM

Quote:

Originally Posted by Torhal (Post 189690)
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

Torhal 05-27-10 12:43 PM

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.

numein 05-27-10 03:53 PM

@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...

Xrystal 05-27-10 04:26 PM

Quote:

Originally Posted by Torhal (Post 189803)
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 :D

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.


All times are GMT -6. The time now is 01:18 PM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI