Thread Tools Display Modes
04-03-16, 11:09 AM   #1
Uitat
A Chromatic Dragonspawn
 
Uitat's Avatar
AddOn Author - Click to view addons
Join Date: May 2011
Posts: 162
Namespacing, please help!!

so im trying to figure out namespacing... yes im slow on the curve please be patient with me.

so in my core filei would do this right

Code:
-- defines the namespace
local _, test = ...
and in all other files i would need this right?
Code:
-- defines  the namespace
local _, test =...

-- concatenates the file with the namespace??
test.modules[#test.modules+1] = function()

end
is this correct?
__________________
  Reply With Quote
04-03-16, 11:53 AM   #2
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,879
It doesn't matter what you call them in each file, they are just a reference to a string containing the name of the addon and a reference to a table the game creates, that is unique to each addon.

These two "parameters" (the ...) are passed to all .lua files (not xml) under your addon folder including files in sub-folders, so:

file 1
Code:
local addonname, addontable = ...
addontable.somestring = "Hi There."
function addontable:Smile()
    print("SMILE!")
end
file 2
Code:
local _, NS = ...
print(NS.somestring)
NS:Smile()
prints:
Hi There.
SMILE!
In short, it's not really a namespace, it's just a convenient mechanism to pass information etc. between files (modules) in your addon without using globals.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 04-04-16 at 01:12 AM.
  Reply With Quote
04-03-16, 05:47 PM   #3
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,290
If you would like to create something modular i can provide you the basics:

Main file:
Lua Code:
  1. local AddonName, Addon = ...
  2.  
  3. local Main = { }
  4. Addon.Main = Main
  5.  
  6. Main.modules = { }
  7.  
  8. Main.events = CreateFrame("Frame")
  9. Main.events:RegisterEvent("ADDON_LOADED")
  10.  
  11. Main.events:SetScript("OnEvent", function(self, event, ...)
  12.     Main[event](Main, ...)
  13. end)
  14.  
  15. function Main:CreateModule(name)
  16.     local module = { }
  17.  
  18.     module.name = name
  19.  
  20.     self.modules[name] = module
  21.  
  22.     return module
  23. end
  24.  
  25. function Main:GetModule(name)
  26.     return self.modules[name]
  27. end
  28.  
  29. function Main:CallModule(module, func, ...)
  30.     if type(module[func]) == "function" then
  31.         module[func](module, ...)
  32.     end
  33. end
  34.  
  35. function Main:ADDON_LOADED(addon)
  36.     if addon == AddonName then
  37.         for moduleName, module in pairs(self.modules) do
  38.             module:OnLoad()
  39.         end
  40.  
  41.         self:RegisterEvents()
  42.  
  43.         self.events:UnregisterEvent("ADDON_LOADED")
  44.     end
  45. end
  46.  
  47. function Main:RegisterEvents()
  48.     -- Register other main events here
  49.     self.events:RegisterEvent("PLAYER_ENTERING_WORLD")
  50. end
  51.  
  52. function Main:PLAYER_ENTERING_WORLD()
  53.     -- Player entered the world and every module is loaded, global API calls should work
  54.     self.events:UnregisterEvent("PLAYER_ENTERING_WORLD")
  55. end

Module file:
Lua Code:
  1. local AddonName, Addon = ...
  2.  
  3. local Main = Addon.Main
  4.  
  5. local ModuleOne = Main:CreateModule("ModuleOne")
  6.  
  7. function ModuleOne:OnLoad()
  8.     self.events = CreateFrame("Frame")
  9.     self.events:SetScript("OnEvent", function(this, event, ...)
  10.         ModuleOne[event](ModuleOne, ...)
  11.     end)
  12.  
  13.     self:RegisterEvents()
  14. end
  15.  
  16. function ModuleOne:RegisterEvents()
  17.     -- Register module events here
  18.     self.events:RegisterEvent("PLAYER_ENTERING_WORLD")
  19. end
  20.  
  21. function ModuleOne:PLAYER_ENTERING_WORLD()
  22.     -- Player entered the world, global API calls should work
  23.     self.events:UnregisterEvent("PLAYER_ENTERING_WORLD")
  24. end

Last edited by Resike : 04-03-16 at 06:01 PM.
  Reply With Quote
04-03-16, 07:48 PM   #4
Uitat
A Chromatic Dragonspawn
 
Uitat's Avatar
AddOn Author - Click to view addons
Join Date: May 2011
Posts: 162
Originally Posted by Fizzlemizz View Post
It doesn't matter what you call them in each file, they are just a reference to a string containing the name of the addon and a reference to a table the game creates, that is unique to each addon.

These two "parameters" are passed to all .lua files (not xml) under your addon folder including files in sub-folders, so:

file 1
Code:
local addonname, addontable = ...
addontable.somestring = "Hi There."
function addontable:Smile()
    print("SMILE!")
end
file 2
Code:
local _, NS = ...
print(NS.somestring)
NS:Smile()
prints:


In short, it's not really a namespace, it's just a convenient mechanism to pass information etc. between files (modules) in your addon without using globals.
great example thank you Fizzle
__________________
  Reply With Quote
04-03-16, 07:50 PM   #5
Uitat
A Chromatic Dragonspawn
 
Uitat's Avatar
AddOn Author - Click to view addons
Join Date: May 2011
Posts: 162
Originally Posted by Resike View Post
If you would like to create something modular i can provide you the basics:

Main file:
Lua Code:
  1. local AddonName, Addon = ...
  2.  
  3. local Main = { }
  4. Addon.Main = Main
  5.  
  6. Main.modules = { }
  7.  
  8. Main.events = CreateFrame("Frame")
  9. Main.events:RegisterEvent("ADDON_LOADED")
  10.  
  11. Main.events:SetScript("OnEvent", function(self, event, ...)
  12.     Main[event](Main, ...)
  13. end)
  14.  
  15. function Main:CreateModule(name)
  16.     local module = { }
  17.  
  18.     module.name = name
  19.  
  20.     self.modules[name] = module
  21.  
  22.     return module
  23. end
  24.  
  25. function Main:GetModule(name)
  26.     return self.modules[name]
  27. end
  28.  
  29. function Main:CallModule(module, func, ...)
  30.     if type(module[func]) == "function" then
  31.         module[func](module, ...)
  32.     end
  33. end
  34.  
  35. function Main:ADDON_LOADED(addon)
  36.     if addon == AddonName then
  37.         for moduleName, module in pairs(self.modules) do
  38.             module:OnLoad()
  39.         end
  40.  
  41.         self:RegisterEvents()
  42.  
  43.         self.events:UnregisterEvent("ADDON_LOADED")
  44.     end
  45. end
  46.  
  47. function Main:RegisterEvents()
  48.     -- Register other main events here
  49.     self.events:RegisterEvent("PLAYER_ENTERING_WORLD")
  50. end
  51.  
  52. function Main:PLAYER_ENTERING_WORLD()
  53.     -- Player entered the world and every module is loaded, global API calls should work
  54.     self.events:UnregisterEvent("PLAYER_ENTERING_WORLD")
  55. end

Module file:
Lua Code:
  1. local AddonName, Addon = ...
  2.  
  3. local Main = Addon.Main
  4.  
  5. local ModuleOne = Main:CreateModule("ModuleOne")
  6.  
  7. function ModuleOne:OnLoad()
  8.     self.events = CreateFrame("Frame")
  9.     self.events:SetScript("OnEvent", function(this, event, ...)
  10.         ModuleOne[event](ModuleOne, ...)
  11.     end)
  12.  
  13.     self:RegisterEvents()
  14. end
  15.  
  16. function ModuleOne:RegisterEvents()
  17.     -- Register module events here
  18.     self.events:RegisterEvent("PLAYER_ENTERING_WORLD")
  19. end
  20.  
  21. function ModuleOne:PLAYER_ENTERING_WORLD()
  22.     -- Player entered the world, global API calls should work
  23.     self.events:UnregisterEvent("PLAYER_ENTERING_WORLD")
  24. end
oh this is cool, i may need to talk to you both a little more, as i am really trying to get some things moduled as to ease the cpu usage
__________________
  Reply With Quote

WoWInterface » Developer Discussions » General Authoring Discussion » Namespacing, please help!!


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