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 Code:
--file 2 |
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:
## ... Code:
function DoSomething() end Code:
local function SomethingElse() |
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. |
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:
That way you can have your globally available functions, but they're all wrapped up in your own little space. |
Quote:
Lua Code:
|
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 = {} 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 = {} Now, a possible benefit of having the redundancy of Code:
myNamespace = myNamespace or {} |
Quote:
File A: Lua Code:
File B: Lua Code:
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:
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. |
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 :)
|
All times are GMT -6. The time now is 04:50 PM. |
vBulletin © 2024, Jelsoft Enterprises Ltd
© 2014 - 2022 MMOUI