WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   General Authoring Discussion (https://www.wowinterface.com/forums/forumdisplay.php?f=20)
-   -   Not sure what I'm looking for (https://www.wowinterface.com/forums/showthread.php?t=48823)

LanceDH 01-18-14 04:57 AM

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.

Duugu 01-18-14 05:10 AM

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. })

LanceDH 01-18-14 05:46 AM

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

Phanx 01-18-14 08:03 AM

Quote:

Originally Posted by Duugu (Post 289814)
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


Duugu 01-18-14 08:15 AM

Ok. That makes sense. I'll use it from now on. Thank you.

LanceDH 01-18-14 11:41 AM

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.

Phanx 01-18-14 12:08 PM

Quote:

Originally Posted by LanceDH (Post 289838)
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()


LanceDH 01-18-14 12:18 PM

Quote:

Originally Posted by Phanx (Post 289842)
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.


All times are GMT -6. The time now is 03:37 AM.

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