View Single Post
04/25/14, 10:16 PM   #3
LilBudyWizer
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 32
I try to use just one global, the directory name of my addon.

Lua Code:
  1. MyAddon =
  2. {
  3.     Name="MyAddon",
  4.     Info = {},
  5.     Modules = {},
  6.     Classes = {},
  7.     Settings = {},
  8.     RefTbls = {},
  9.     State = {},
  10.     SavedVars = {},
  11. }

The above is in a file by itself loaded first. Then in each file I have:

Lua Code:
  1. local addon = MyAddon

I prefer using addon as it makes reuse easier, just one line in each file actually refers to the name of the addon. There isn't really any way around that without being able to execute a file. I would much prefer to have that line in a generic name like "init.lua" that's executed in each module so there is no reference to the actual addon name except in global.lua which defines it. If I need to use the actual addon name in the code, such as for error messages, it's addon.Name.

Within files using a module I then have:

Lua Code:
  1. local util=addon.Modules.Utility

And in the module itself:

Lua Code:
  1. local util=addon.Modules.Utility = {}

Ok, not really. The modules have to be there so it's clearer to me to actually place the creation of the table in global.lua. My settings are actually a class so Settings is an insance of that class with the actual settings in the saved variables. I found with saved variables I can't actually create them until the addon is loaded so I can't stick it in the global.lua. So each module needs an init called during the addon loaded event to set the saved variables. I don't actually refer to the saved variables in the global except through the following:

Lua Code:
  1. local SV = addon.SavedVars

Mostly just for shorthand purposes. Overall, personally, there's a big advantage to being able to access functions through a table. One of my testing functions tracks coverage, i.e. which functions have been called. With all the functions in tables I can easily list all the functions defined and never called.
  Reply With Quote