WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   How can I partition a Lua file? (https://www.wowinterface.com/forums/showthread.php?t=59064)

Firesong 03-10-22 07:15 AM

How can I partition a Lua file?
 
Lua has a nice "require" system to allow long files to be partitioned. WoW disables this but I can see many addons have multiple files. What I can't work out is how they do it.

For example, if I have a behavoiur that is different for Tank and DPS, I'd love to be able to have the Tank stuff in a file called "Tank.lua"

How do I do this? Sorry if it's a really noob question.

LudiusMaximus 03-10-22 07:34 AM

You list all your lua files in your addon's TOC file.

Sharing data between your lua files works by putting this at the beginning of every lua file:

Code:

local folderName, Addon = ...
The "Addon" table is then shared by all your lua files.

If you do not need your Addon's folder name you can also just do:

Code:

local _, Addon = ...

Dridzt 03-10-22 07:40 AM

WoW addons use a Table of Contents <addonName>.toc file to instruct the client to load files.

If you open an existing addon you will see a number of files referenced by their filenames in the .toc file.

That's the way addons can load multiple files in the general case.
There's some nuance there, xml files can load other xml or lua files themselves using some special xml tags/directives, so some addon might reference an xml file in the .toc and that xml then loads other files.

Now about sharing information between different files comprising an addon.

There's two ways to do that, either
- put some methods or data in the global namespace that all addons share. This is not recommended and aside for very specific situations not required.
- use the vararg passed by the wow client to all lua files loaded by an addon .toc that provides those files with the addon name and a shared table.
You will typically see a line such as
Code:

local addonName, addonTable = ...
at the top of each lua file.
You can then put stuff (methods or data) that you want accessed from another file into that addonTable.

Firesong 03-10-22 08:05 AM

I made a small addon and the toc is:
## Interface: 90200
## Title: Testbox
Testbox.lua
Tank.lua

So my main file in all it's glory reads:

local Testbox, addonTable = ...

addonTable.SayHi()

Tank.ua is:
local Testbox, addonTable = ...

local function SayHi()
print("Hi")
end

When I try to run it I get "Attempt to call field 'SayHi" a nil value.

LudiusMaximus 03-10-22 08:37 AM

:)

OK, first thing: if you declare a function as "local", it will only be known within its scope, which is at max the current lua file.

So in Tank.lua, declare the function as:

Code:

addonTable.SayHi = function()
    print("Hi")
end


Second thing, your lua files are processed in the order they are stated in the TOC file.
So if you try to execute addonTable.SayHi() before your Tank.lua was processed, the function does not yet exist.

Firesong 03-10-22 10:43 AM

Thanks
 
Success - thanks very much.


All times are GMT -6. The time now is 06:49 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI