Thread Tools Display Modes
03-10-22, 07:15 AM   #1
Firesong
A Deviate Faerie Dragon
Join Date: Jan 2022
Posts: 19
Question 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.
  Reply With Quote
03-10-22, 07:34 AM   #2
LudiusMaximus
A Rage Talon Dragon Guard
 
LudiusMaximus's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2018
Posts: 320
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 = ...
__________________
~ Be the change you want to see in the world... of warcraft interface! ~
  Reply With Quote
03-10-22, 07:40 AM   #3
Dridzt
A Pyroguard Emberseer
 
Dridzt's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2005
Posts: 1,359
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.
  Reply With Quote
03-10-22, 08:05 AM   #4
Firesong
A Deviate Faerie Dragon
Join Date: Jan 2022
Posts: 19
Unhappy

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.
  Reply With Quote
03-10-22, 08:37 AM   #5
LudiusMaximus
A Rage Talon Dragon Guard
 
LudiusMaximus's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2018
Posts: 320


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.
__________________
~ Be the change you want to see in the world... of warcraft interface! ~

Last edited by LudiusMaximus : 03-10-22 at 08:39 AM.
  Reply With Quote
03-10-22, 10:43 AM   #6
Firesong
A Deviate Faerie Dragon
Join Date: Jan 2022
Posts: 19
Exclamation Thanks

Success - thanks very much.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » How can I partition a Lua 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