Thread Tools Display Modes
03-29-24, 09:52 AM   #1
Codger
An Aku'mai Servant
AddOn Author - Click to view addons
Join Date: Mar 2021
Posts: 30
What does this code do?

I've seen this in a couple addons but haven't been able to find any info on what it does:

Lua Code:
  1. local frame1 = {}
  2. myAddon.frame1 = frame1

I'm guessing it's another way of setting up a namespace?
What I am doing in an addon is:

Lua Code:
  1. local addonName, addonTable = ...
  2. function addonTable:doSomething()

which requires me to preface everywhere I use doSomething() with addonTable.
Curious if this is a better way?
  Reply With Quote
03-29-24, 10:40 AM   #2
Xrystal
nUI Maintainer
 
Xrystal's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 5,935
Yep, looks like it.

I use
Lua Code:
  1. local addonName, addonTable = ...
at the top of each of my lua files and give them their own sub table for their functionality and data.

For example - if I were to have my own ButtonManager for some reason I could have something like this

Lua Code:
  1. addonTable.ButtonManager = {}

And then I could create addon wide functions like this
Lua Code:
  1. addonTable.ButtonManager.Create = function(...)
  2. end


I still use the regular local variables and functions if no other file needs to access it.
Or global functions if I want external addons access to certain variables or functions - uniquely named of course and validated

You could simplify the addonTables accessors as follows ( using the previous example )

Lua Code:
  1. -- Create the files data table
  2. addonTable.ButtonManager = {}
  3.  
  4. -- Simplify it
  5. local bm = addonTable.ButtonManager
  6.  
  7. -- Use it
  8. bm.Create = function(...)
  9. end

But I only do that if it's more than 5/10 lines worth of copying and pasting of the table.
__________________


Characters:
Gwynedda - 70 - Demon Warlock
Galaviel - 65 - Resto Druid
Gamaliel - 61 - Disc Priest
Gwynytha - 60 - Survival Hunter
Lienae - 60 - Resto Shaman
Plus several others below level 60

Info Panel IDs : http://www.wowinterface.com/forums/s...818#post136818
  Reply With Quote
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,879
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
03-29-24, 11:02 PM   #4
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,326
Originally Posted by Codger View Post
Lua Code:
  1. local frame1 = {}
  2. myAddon.frame1 = frame1

I'm guessing it's another way of setting up a namespace?
On the surface, this is just storing one table into another and keeping a local reference to it. Taking the rest of the message into account, I'm assuming the real question is, "What is the 'local reference' for?"

A local used in this way is acting as a cache as it's quicker to access than trying to grab it from the table again. Trying to grab anything from a table is known as an "index operation". Just like any other operation, this takes time, which can add up. In the grand scheme of things, doing this isn't going to cause a noticeable effect unless the affected code is running very frequently, like in loops, OnUpdate scripts, or combat log events.



Originally Posted by Codger View Post
What I am doing in an addon is:

Lua Code:
  1. local addonName, addonTable = ...
  2. function addonTable:doSomething()

which requires me to preface everywhere I use doSomething() with addonTable.
Curious if this is a better way?
The equivalent to implementing it in this code would be either of the following.
Lua Code:
  1. local addonName, addonTable = ...
  2.  
  3. local function doSomething(self)
  4. end
  5.  
  6. addonTable.doSomething = doSomething
-or-
Lua Code:
  1. local addonName, addonTable = ...
  2.  
  3. function addonTable:doSomething()
  4. end
  5.  
  6. local doSomething = addonTable.doSomething
The first one may be used if you're defining a function that is also used in the same file, then have a chunk exporting your local definitions to your addon table.
The second one is more useful if the function was defined in another file and you want to create a local reference for speed.



Keep in mind:
A call v:name(args) is syntactic sugar for v.name(v,args), except that v is evaluated only once.
-Lua 5.1 Reference Manual - (§2.5.8) Function Calls

The colon syntax is used for defining methods, that is, functions that have an implicit extra parameter self. Thus, the statement
function t.a.b.c:f (params) body end
is syntactic sugar for
t.a.b.c.f = function (self, params) body end
-Lua 5.1 Reference Manual - (§2.5.9) Function Definitions

The dot notation better reflects how the function exists in memory and the colon notation is a modifier on how to define or call it. There's no way to use the colon notation when calling from or defining as a local.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)

Last edited by SDPhantom : 03-29-24 at 11:39 PM.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » What does this code do?


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