View Single Post
03-29-24, 11:09 AM   #3
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,898
Lua Code:
  1. function addonTable:doSomething()
It's a matter of what scope you want the function/variable to have
  • Added to global table, All addons have acess to it.
  • Added to "namespace" table, the current addon has access in all its .lua files
  • local at the root level of a .lua file - code below it in that .lua file has access
  • local inside a code chunk (if, for, anything with an "end" statement) - only code inside the chunk after the local declaration has access.

This is also effected by load sequence (.toc order). Eg. you can't use a function added to the namespace table before the file it is declared in has been loaded.

.toc
Code:
file1.lua
file2.lua
file1.lua
Code:
local addon, ns = ...
ns:RunFunc()
file2.lua
Code:
local addon, ns = ...
function ns:RunFunc()
    print("Are we here yet?")
end
Errors because file1 is loaded before file2 has a chance to "create" the function.

Consider your .lua files as functions in their own right

Code:
BlizzardSpace["NameOfTheAddon"] = {}
function file1.lua("NameOfTheAddon", BlizzardSpace["NameOfTheAddon"])
    local addon, ns = ... -- your code goes here
    -- etc. etc.
end
function file2.lua("NameOfTheAddon", BlizzardSpace["NameOfTheAddon"])
    local addon, ns = ... -- your code goes here
    -- etc. etc.
end
(generalised representation)
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 03-29-24 at 11:47 AM.
  Reply With Quote