martin.kathur
martin.kathur

Reputation: 37

WOW addons, Lua, the meaning of T in local _, T =

Recently, I'm trying to look through a WOW addon called VenturePlan, which calculates the result of your command table quest.

It's a simple addon with almost pure '.lua' files. They are 'Collector.lua', 'Loader.lua', 'MissionList.lua', 'MissionView.lua', 'VenturePlan.toc', 'vs-spells.lua', 'vs.lua', 'Widgets.lua', 'Libs/Evie.lua'.

addon file structure

I have a little knowledge of programming, like python and c. But I haven't written addons for WOW. I got stuck at the very beginning of this addon.

Almost every file has a piece of code

local _, T = ...
local EV = T.Evie

To my understanding, those code at the very beginning, are usually some variable declaration, or module import. Here, seems the author passed some value to 'T', but I can't figure out where this happens, and it's not even a callable function, it's in a .lua file.

Also, I could not find the entry point. It seems 'Loader.lua' will be loaded first, but it never return anything interesting.

So, where does the 'T' come from? and what's the structure of it, where it's defined?

The addon is here https://www.townlong-yak.com/addons/venture-plan

Upvotes: 0

Views: 1377

Answers (3)

Eugene X
Eugene X

Reputation: 149

Simple usage example of lua result stack.

function stack_results()
    return 1, 2, 3, 4, 5, 6, 7, 8, 9 
end

-- skip 1st, then get 2nd result, then skip 2 results and get 5th result
-- calling stack_results function
local _, a, _, _, b = stack_results()

-- result will be 2, 5
print("Result: ", a, b)
Result:     2   5

Upvotes: 0

Piglet
Piglet

Reputation: 28974

Lua chunks in a file are loaded as a function. WoW will call those functions providing two arguments. First argument is the addon's name and the second one is a table shared among all files of your addon. That way you don't have to use the global environment to share data between your files.

local _, T = ... will store those two arguments in two local variables. _ is usually to name unused variables. luacheck for example won't raise warnings for unused variables named _

print(_, T) in all those files and you should get the same string and table.

Upvotes: 1

Alexander Mashin
Alexander Mashin

Reputation: 4537

It seems that WoW will open those *.lua files with C lua_load/luaL_dostring/luaL_dofile or Lua loadstring/loadfile; they convert Lua code into functions. ... is a list of arguments to a variadic function; and the multiple assignment local _, T = ... means that _ id the first argument passed to the function and T is the second.

Return is not really necessary for this kind of executing Lua code. It can send data back by altering the received arguments, since tables are passed by reference; as well as globals.

Upvotes: 1

Related Questions