Thread Tools Display Modes
11/26/18, 09:44 PM   #1
TheBunnynator1001
AddOn Author - Click to view addons
Join Date: Nov 2018
Posts: 4
Smile Where to put functions?

I'm brand new here, and I wanted to go ahead and voice this in my own words.
I've got a fairly decent grasp on Lua, and I wanted to start developing add ons for use on my client and potentially friends' clients as well. Unfortunately, I'm not entirely sure what I'm doing. I've read through some of the tutorials that are out there and they just don't compare to being able to ask questions and have them answered. My main question is this, is there somewhere I'm supposed to be putting functions within the code? Or can they be placed anywhere within the program? If you need more info on my add on to be able to answer, please feel free to PM me! I'd love to work with someone to learn more, and I could also use all the XML advice I can get! Thanks!
  Reply With Quote
11/26/18, 09:57 PM   #2
Rhyono
AddOn Author - Click to view addons
Join Date: Sep 2016
Posts: 659
Functions need to exist before they are used


So if function b() calls a(), a() needs to exist before b() or at least before b() is called.
  Reply With Quote
11/26/18, 10:00 PM   #3
TheBunnynator1001
AddOn Author - Click to view addons
Join Date: Nov 2018
Posts: 4
Originally Posted by Rhyono View Post
Functions need to exist before they are used


So if function b() calls a(), a() needs to exist before b() or at least before b() is called.
No right I get that, but in one of the tutorials it has almost like a main class, (where in this case it's a main function or table to hold other functions), and I'm wondering if I need to store all other data within that main, or if I need to create new table to store it in, or if I can just put it anywhere in the code itself. I think that's a better way to describe my issue.
  Reply With Quote
11/26/18, 10:02 PM   #4
Rhyono
AddOn Author - Click to view addons
Join Date: Sep 2016
Posts: 659
A variable/function can exist without being part of a "class" or table. Almost always though, you want it to either be part of it or definitely be a local variable/function.
  Reply With Quote
11/26/18, 10:04 PM   #5
TheBunnynator1001
AddOn Author - Click to view addons
Join Date: Nov 2018
Posts: 4
What is the reasoning for this? Just to keep the code clean or is there another purpose?
  Reply With Quote
11/26/18, 10:05 PM   #6
Rhyono
AddOn Author - Click to view addons
Join Date: Sep 2016
Posts: 659
If you don't make variables local, they are globally accessible by all addons. We really don't need a bunch of people overlapping their variables.
  Reply With Quote
11/26/18, 10:08 PM   #7
TheBunnynator1001
AddOn Author - Click to view addons
Join Date: Nov 2018
Posts: 4
And adding functions to a table causes them to become local? Like I can call them from within the lua file and all other lua files involved? I'm not fully grasping this, perhaps not as good of a grasp as I thought.
  Reply With Quote
11/26/18, 10:12 PM   #8
Rhyono
AddOn Author - Click to view addons
Join Date: Sep 2016
Posts: 659
Code:
GlobalTableWithUniqueName = {x=10}

local MyAddon = GlobalTableWithUniqueName

function MyAddon.Add(y)
MyAddon.x = MyAddon.x + y
end

MyAddon.Add(10)
d(MyAddon.x)
d(GlobalTableWithUniqueName.x)
  Reply With Quote
11/27/18, 10:50 AM   #9
Baertram
Super Moderator
 
Baertram's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 4,903
Local variables (a table is a variable too at the end) can be accesed only in this particular file where you write it into.
Global variables can be access in your different addon files (like in a lua + a xml file of your addon) AND in other addons as well.

From the example below:
1)
If you e.g. use the addon ZGOO and do something like /zgoo GlobalTableWithUniqueName into the chat you'll see the contents of this table in a an UI.
It will show you the variable x with value 10.

2)
If you do /zgoo MyAddon it won't work as it is local though! But as you have defined MyAddon to be a local variable poinetr to the global variable GlobalTableWithUniqueName , everything you have added to MyAddon will be in GlobalTableWithUniqueName as well.

So if you go back to 1) you'll also notice that the added function MyAddon.Add will be shown as
GlobalTableWithUniqueName.Add

If you use this into the chat:
/script GlobalTableWithUniqueName.Add(20) it will output the value x + y (20) = 30 into the chat.
  Reply With Quote
11/27/18, 11:29 PM   #10
Drummerx04
AddOn Author - Click to view addons
Join Date: Sep 2017
Posts: 54
Originally Posted by TheBunnynator1001 View Post
I'm brand new here, and I wanted to go ahead and voice this in my own words.
I've got a fairly decent grasp on Lua, and I wanted to start developing add ons for use on my client and potentially friends' clients as well. Unfortunately, I'm not entirely sure what I'm doing. I've read through some of the tutorials that are out there and they just don't compare to being able to ask questions and have them answered. My main question is this, is there somewhere I'm supposed to be putting functions within the code? Or can they be placed anywhere within the program? If you need more info on my add on to be able to answer, please feel free to PM me! I'd love to work with someone to learn more, and I could also use all the XML advice I can get! Thanks!
There is one thing to point out before I answer.

The Addon manifest (the .txt) file is used to specify a load order of the lua files and other assets of your Addon. So keep that in mind when you have multiple files and you use functions or tables between files.

A basic function declaration is in the global scope on all lines below the definition and in all files after its lua file is processed (and in game via /script)
Lua Code:
  1. function DoTheThings() return false end
This is only recommended for testing purposes to avoid collisions with other addons (unless that is your goal)

The best practice is to place functions within a Globally defined table unique to your addon. Declare the table in the first lua file specified in your manifest.
Lua Code:
  1. SpecialAddonTable = {}
  2. function SpecialAddonTable.DoTheThings() return false end

The "local" keyword will limit the scope of a declared variable/function to the scope in which it is declared
Lua Code:
  1. -- scoped to current file
  2. local function A()
  3.   -- scoped to function A
  4.   local function B(x) return x*x end
  5.   return B(5)
  6. end

The scope of a function can be limited to any block of code. Function names are just variables in Lua, so you can assign to or from them and they can also be nil.

That should be all you really need to know to get started. Start with one lua file (and the manifest). Load your addon into ESO and try calling the functions with /script use the d() function to print to the chat window.

Last edited by Drummerx04 : 11/27/18 at 11:32 PM.
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » Where to put functions?

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