Thread Tools Display Modes
06/10/16, 11:42 AM   #1
Provision
 
Provision's Avatar
AddOn Author - Click to view addons
Join Date: May 2015
Posts: 43
Multiple file

Hi,

I don't find documentation about that. How can I call function in another file ?

Thank.


This doesn't work :
Code:
--file 1 
function TeamFormation_createLAM2Panel ()

end
Code:
--file 2
local function TeamFormation_OnAddOnLoad(eventCode, addOnName)
    TeamFormation_createLAM2Panel()
end
  Reply With Quote
06/10/16, 11:50 AM   #2
haggen
 
haggen's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2015
Posts: 137
The order in which you load the files in the manifest (<add-on name>.txt) matters. So if you define `function TeamFormation_createLAM2Panel` without the `local` scope (as you presented here) in file A and load file A before the file that contains the call `TeamFormation_createLAM2Panel()` it should work just fine.

To make it clear, if your files are like this:

AddOn.txt
Code:
## ...

A.lua
B.lua
A.lua
Code:
function DoSomething() end
B.lua
Code:
local function SomethingElse()
    DoSomething()
end
It'll work. But I'd advise against spilling global functions like that.

Last edited by haggen : 06/10/16 at 11:53 AM.
  Reply With Quote
06/10/16, 12:00 PM   #3
Provision
 
Provision's Avatar
AddOn Author - Click to view addons
Join Date: May 2015
Posts: 43
I didn't see that I have 2 errors.

I read only the last error. My function was nil because I have an error in my function.

@haggen : Thanks for your reply.
  Reply With Quote
06/11/16, 11:11 AM   #4
Randactyl
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 251
I think the more accepted way to do this is by creating a "namespace" table and putting all of your stuff in it.

Lua Code:
  1. TeamFormation = TeamFormation or {}
  2.  
  3. local PTF = TeamFormation
  4.  
  5. function PTF.MyFunction()
  6.  
  7. end

That way you can have your globally available functions, but they're all wrapped up in your own little space.
  Reply With Quote
07/18/16, 06:54 PM   #5
Justinon
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 8
Originally Posted by Randactyl View Post
Lua Code:
  1. TeamFormation = TeamFormation or {}
What is the purpose of setting it equal to itself; i.e. checking if the namespace is nil? Is the same effect not achieved by simply typing:
Lua Code:
  1. TeamFormation = {}
My first instinct tells me that perhaps the addon information persists after the first time initializing the addon, and therefore makes it more efficient upon the next loading thereafter?
  Reply With Quote
07/18/16, 10:16 PM   #6
Randactyl
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 251
It's just safety and is not totally necessary.

Let's say you have two files, main.lua and settings.lua, and that main.lua is listed first in the manifest.

At the top of main.lua you could write

Code:
myNamespace = {}
because it is your namespace and this is the first time you're bringing it into the world.

Afterward, in settings.lua, you could either assume myNamespace exists since it should have been created in the first file, or you can chose to set it to a new table if it is still nil for some reason (the previous example).

However, if you were to write

Code:
myNamespace = {}
in settings.lua, you would be explicitly discarding anything that was done to the version of myNamespace that was set up in main.lua.

Now, a possible benefit of having the redundancy of

Code:
myNamespace = myNamespace or {}
is if somewhere down the line you decide you need to shuffle around the order in which files are loaded. This line could save you an error there if you forget to also shuffle where your namespace is initially declared.
  Reply With Quote
07/18/16, 10:18 PM   #7
Sasky
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 231
Originally Posted by Justinon View Post
What is the purpose of setting it equal to itself; i.e. checking if the namespace is nil? Is the same effect not achieved by simply typing:
Lua Code:
  1. TeamFormation = {}
My first instinct tells me that perhaps the addon information persists after the first time initializing the addon, and therefore makes it more efficient upon the next loading thereafter?
For a single file, yes it's identical. It's setup like that so that you don't have to worry about what order the files are loaded.

File A:
Lua Code:
  1. TF = TF or {}
  2. TF.a = 1

File B:
Lua Code:
  1. TF = TF or {}
  2. TF.b = 2

Either file can be loaded first here. Say A is loaded before B. In file A it'll create TF as an empty table. In file B it'll leave TF as it already is. If you're careful about the order that files are read, you don't need this. If you use a separate namespace table per file, you don't need this. However, if you didn't use a similar pattern and loaded the files out-of-order, you'd end up dereferencing NIL:
Lua Code:
  1. -- File B
  2. TF.b = 2 -- TF doesn't exist yet
  3.  
  4. -- File A
  5. TF = {}
  6. TF.a = 1

Personally, I find it easier to just use the pattern and ignore file order. Also I tend to load all the class files before the main file, and it fits better with that structure.
  Reply With Quote
07/18/16, 10:23 PM   #8
Justinon
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 8
Ah okay, thanks you two, that makes a lot of sense. I guess because I was always careful about the order it never really occured to me that that's the benefit. That's quite useful, and I appreciate the in-depth explanations
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » Multiple file

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