Thread Tools Display Modes
01-18-14, 04:57 AM   #1
LanceDH
A Cyclonian
 
LanceDH's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2012
Posts: 41
Not sure what I'm looking for

Hi,

So I have an addon for quite some time now but it's getting a bit big and messy since it's all in 1 file.
Now I want to split it up in different files (think classes in say C++) but I have absolutely no idea how to do it.
I've tried searching for guides about it but I don't quite know what exactly I'm looking for, so I haven't found anything.
To give an idea about the addon:
It tracks different things related to battlegrounds (mainly randoms) such as your honor/conquest points, your kills/deaths in the current bg, your exp gained, ....
Each one of those tracks is like its own class so to speak and i want to create a seperate file for them, rather than have all of them in 1 big spaghetti code.
The main thing I'm looking for is something where I can just write a file and link it to the core myself.
  Reply With Quote
01-18-14, 05:10 AM   #2
Duugu
Premium Member
 
Duugu's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 851
I would do it like this:

core.lua
Lua Code:
  1. function evaluate_all()
  2.     if stuff then
  3.         for _, v in pairs(stuff) do
  4.             v:evaluate()
  5.         end
  6.     end
  7. end

module1.lua
Lua Code:
  1. if not stuff then stuff = {} end -- <local stuff = stuff or {}> would work too, or?
  2. table.insert(stuff, {
  3.     data = {a=1, b=2},
  4.     evaluate = function()
  5.         if a > a then
  6.         return a
  7.     end
  8. })

module2.lua
Lua Code:
  1. if not stuff then stuff = {} end
  2. table.insert(stuff, {
  3.     data = {n1="nag", n2="nagnag"},
  4.     evaluate = function()
  5.         if n1 == n2 then
  6.         return "nagnagnag"
  7.     end
  8. })

Last edited by Duugu : 01-18-14 at 05:18 AM.
  Reply With Quote
01-18-14, 05:46 AM   #3
LanceDH
A Cyclonian
 
LanceDH's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2012
Posts: 41
So variables can just go cross ... file then ?
How would I go about calling a function from say module1 in core?
Or can I just call them cross file as well like the variables as long as I don't make them local ?
I guess there's isn't something like module1.function1() like you'd have in c

Edit:
just tested it and seems to work... can't believe it was that easy

Last edited by LanceDH : 01-18-14 at 06:43 AM.
  Reply With Quote
01-18-14, 08:03 AM   #4
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by Duugu View Post
I would do it like this:
Do not do it like that.

Instead, use the private table Blizzard provides each and every addon for exactly this purpose. This table is not in the global namespace (though you can put it there if you want) and you can access it by adding a line like this to each of your files:

Code:
local ADDON_NAME, addon_private_table = ...
You can use whatever variable names you want, you don't have to use the same names in each file, and you don't need to assign them at all in files where you don't need them, but you do need to assign them in the main chunk (top-level in the file, not in a function, not in a do...end block, etc.), and you should assign them at or near the top of the file.

The first value in the file-level vararg (the three dots) is a string containing your addon's name. It's the same value that's passed with your addon's ADDON_LOADED event, and that you can pass to GetAddOnInfo and similar functions.

The second value is a table. You can put whatever you want in it. It's only visible to the files in your addon, though you can make it global if you really want:

Code:
local ADDON, private = ...
_G[ADDON] = private
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
01-18-14, 08:15 AM   #5
Duugu
Premium Member
 
Duugu's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 851
Ok. That makes sense. I'll use it from now on. Thank you.

Last edited by Duugu : 01-18-14 at 08:18 AM.
  Reply With Quote
01-18-14, 11:41 AM   #6
LanceDH
A Cyclonian
 
LanceDH's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2012
Posts: 41
Well I'm currently switching to the different files and it's working out quite well so far.
I'm using a global table right now but I'll see if I can change it to the private version.
If it fine to have functions in the global space though?
For example my core file has a function to get your kills end some of the other files use that function.
I add the addon name to the beginning of the variable names to lower the change of issues, but it's still a bit iffy.
I might be able to just switch it to the files instead and have them local, but I'm just curious in case I run into something like it in the future.
  Reply With Quote
01-18-14, 12:08 PM   #7
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by LanceDH View Post
If it fine to have functions in the global space though?
For example my core file has a function to get your kills end some of the other files use that function.
Just put them in your addon's namespace. There's no reason to make everything a global. For example, if this is what you have now:

Code:
-- File A
function MyAddon_DoThings()
     print("Doing things.")
end

-- File B
MyAddon_DoThings()
Change it to:

Code:
-- File A
local ADDON, private = ...
function private:DoThings()
    print("Doing things without globals!")
end

-- File B
local ADDON, private = ...
private:DoThings()
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
01-18-14, 12:18 PM   #8
LanceDH
A Cyclonian
 
LanceDH's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2012
Posts: 41
Originally Posted by Phanx View Post
Just put them in your addon's namespace. There's no reason to make everything a global. For example, if this is what you have now:

Code:
-- File A
function MyAddon_DoThings()
     print("Doing things.")
end

-- File B
MyAddon_DoThings()
Change it to:

Code:
-- File A
local ADDON, private = ...
function private:DoThings()
    print("Doing things without globals!")
end

-- File B
local ADDON, private = ...
private:DoThings()
On one end it's pretty genious that it's so simple, on the other end it's confusing as hell if you're used to other languages.
  Reply With Quote

WoWInterface » Developer Discussions » General Authoring Discussion » Not sure what I'm looking for

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