Thread Tools Display Modes
08-04-16, 07:17 PM   #1
Tosaido
A Fallenroot Satyr
 
Tosaido's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2015
Posts: 23
Best practice when creating/accessing global function

Hello,

What would be the best way to create and access a global function.

is:

file1:
Lua Code:
  1. function MyFunction()
  2.     --code
  3.     return a, b
  4. end

file2:
Lua Code:
  1. local a, b = MyFunction()


fine to use, or is there a better way? (The function is called only once every session and no i don't have a global function named MyFunction :])

Last edited by Tosaido : 08-04-16 at 07:21 PM.
  Reply With Quote
08-04-16, 07:29 PM   #2
syncrow
A Flamescale Wyrmkin
 
syncrow's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2014
Posts: 149
Your code is fine, if your global functions are named well and unique (not interfering with other addons)

You could also use functions within a metatable:

file 1:
Lua Code:
  1. local name, addon = ...
  2.  
  3. -- create function
  4. function addon:MyFunction()
  5.     -- content
  6.     return a, b
  7. end

file 2:
Lua Code:
  1. local name, addon = ...
  2. local a, b = addon:MyFunction()
__________________

Last edited by syncrow : 08-04-16 at 07:37 PM.
  Reply With Quote
08-04-16, 07:39 PM   #3
Tosaido
A Fallenroot Satyr
 
Tosaido's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2015
Posts: 23
Hi, thanks for the quick response, although i forgot to mention that file 1 and 2 are part of different addons.
  Reply With Quote
08-04-16, 07:50 PM   #4
syncrow
A Flamescale Wyrmkin
 
syncrow's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2014
Posts: 149
Lua Code:
  1. -- file 1
  2. local name, addon = ...
  3. MySmartAddonVariable = addon

Lua Code:
  1. -- file 2
  2. local addon = MySmartAddonVariable
  3. local a, b = addon:MyFunction()


Assuming using your method: use a prefix for all your global functions:
Lua Code:
  1. function SyncUI_MyFunction()
  2.     --content
  3. end
  4.  
  5. function SyncUI_MyOtherFunction()
  6.     --content
  7. end
__________________

Last edited by syncrow : 08-05-16 at 06:04 AM.
  Reply With Quote
08-04-16, 08:27 PM   #5
Tosaido
A Fallenroot Satyr
 
Tosaido's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2015
Posts: 23
My functions are named quite specific so i doubt anyone is going to accidentally access them.

I'v seen _G pop-up quite often, why use that if you can just calling the function directly?
  Reply With Quote
08-04-16, 08:55 PM   #6
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,877
_G is just the name of the global table or the space where globals are stored.

you can:
/run print(_G.PlayerFrame)
or:
/run print(_G["PlayerFrame"])
or:
/run print(PlayerFrame)

For accessing globals using a string reference
Lua Code:
  1. local var = _G["SomeEntry"]
  2. -- or
  3. _G["SomeEntry"] = SomeValue
is most comonly used. It "replaced" the old GetGlobal/SetGlobal functions (although these are still usable).

Edited for clarity per Tosaido's observations.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 08-05-16 at 01:14 AM.
  Reply With Quote
08-04-16, 09:15 PM   #7
Tosaido
A Fallenroot Satyr
 
Tosaido's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2015
Posts: 23
Ah ok...

so it's used for something like this...(Bad example for using globals but ehh)

Lua Code:
  1. local alphabet = {a, b, ...all the way to, z}
  2.  
  3. for _, Letter in pairs(alphabet) do
  4.     _G["MyTable" .. Letter] = {}
  5. end

where as
Lua Code:
  1. "MyTable" .. Letter = {}
  2. or
  3. ["MyTable" .. Letter] = {}
  4. or
  5. local Name = "MyTable" .. Letter
  6. Name = {}
wouldn't work.

Do i have that correct?

Last edited by Tosaido : 08-04-16 at 09:17 PM.
  Reply With Quote
08-04-16, 09:22 PM   #8
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,877
Lua Code:
  1. local Name = "MyTable" .. Letter
  2. Name = {}

If Letter had been predefined then
local Name = "MyTable" .. Letter

would create a local variable called Name with the string value "MyTable" and whatever Letter contained otherwise you would get a error for trying to concatinate a nil variable.

Name = {}
Would then replace the string with an empty table. Otherwise yes, exactly.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 08-05-16 at 12:30 AM.
  Reply With Quote
08-04-16, 09:43 PM   #9
Tosaido
A Fallenroot Satyr
 
Tosaido's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2015
Posts: 23
Yea i was implying that what i wrote in the second Lua Code snippet would replace the code within the for loop in the first Lua Code snippet.
  Reply With Quote
08-04-16, 10:02 PM   #10
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,877
Gello wrote a reply on the Blizzard forums that might also be of use (bottom part of the post). It's post #6, you might have to press enter on the Navbar again to get to the actual post.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.
  Reply With Quote
08-05-16, 01:13 AM   #11
Torhal
A Pyroguard Emberseer
 
Torhal's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 1,196
Originally Posted by syncrow View Post
Then you could globalize your metatable to get access:
You keep using that word. A metatable is definitely not what you keep showing in your examples, though.
__________________
Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Author of NPCScan and many other AddOns.
  Reply With Quote
08-05-16, 02:17 AM   #12
zork
A Pyroguard Emberseer
 
zork's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 1,740
Just make one global variable of type table that is named after your addon. Put all functions you want globally accessible in that table by reference.
__________________
| Simple is beautiful.
| WoWI AddOns | GitHub | Zork (WoW)

"I wonder what the non-pathetic people are doing tonight?" - Rajesh Koothrappali (The Big Bang Theory)
  Reply With Quote

WoWInterface » AddOns, Compilations, Macros » AddOn Help/Support » Best practice when creating/accessing global function

Thread Tools
Display Modes

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