WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Ellipses assignment? (https://www.wowinterface.com/forums/showthread.php?t=58952)

cosmic_kid 10-13-21 02:39 PM

Ellipses assignment?
 
I've been getting into WoW addon development and for the moment, I'm mainly looking through other people's code to understand what's going on. I keep seeing this type of thing all over the place and I can't, for the life of me, figure out what it means.

local _, ns = ...

They then go on to use the ns variable, but I don't know how it has any data in it. What are they assigning it to exactly?

Ketho 10-13-21 02:51 PM

It's a table that gets passed to all files of an addon

https://wowpedia.fandom.com/wiki/Usi...ddOn_namespace

cosmic_kid 10-13-21 03:06 PM

Sweet, that makes a lot of sense. Thank you!

Kanegasi 10-13-21 10:42 PM

For a more in-depth explanation, ... in Lua is called a vararg, short for variable arguments. It is used when defining a function when the number of arguments being passed to it is unknown.

Lua Code:
  1. function fooBar(...)
  2.     local test1, test2 = ...
  3.     print(test1, test2)
  4. end
  5.  
  6. fooBar("apple")
  7. fooBar("orange", "banana")

There's another level to WoW's coding we don't see, which is written in C++. When this internal structure loads an addon, each file in the addon is actually one big function.

We see:

Lua Code:
  1. local _, ns = ...
  2. -- rest of our file

What's actually happening:

Lua Code:
  1. function addonFile(...)
  2.     local _, ns = ...
  3.     -- rest of our file
  4. end
  5.  
  6. addonFile("Name of AddOn", privateTable)

This privateTable is shared across all loaded files within an addon, basically each .toc file gets its own table.


All times are GMT -6. The time now is 07:03 AM.

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