Thread Tools Display Modes
07-29-12, 01:22 AM   #1
Aanson
A Flamescale Wyrmkin
Join Date: Aug 2009
Posts: 124
Question Looking for a dumbed down explanation of a couple of things!

Hey all. Cheers to anyone who is able to spare a moment or two to help me with this.

I've been self learning Lua and there's a couple of niggling things that I can't seem to get my head around without some sort of help.

I've been using the official manual, the Tutorial Directory at Lua-users wiki, and Phanx's Ace3 Getting Started guide on wowace.

Anyway, to the point.



1. I understand the below function, how it works and how to call it.

Lua Code:
  1. function MyFunction(arg1, arg2 arg3)
  2.   -- block
  3.   return arg1, arg2, arg3 -- (optional)
  4. end

But I don't understand this at all...

Lua Code:
  1. function MyFunction(...)
  2.   -- block
  3. end

What is the "..." for when creating/calling a function? I've read in the past that it's a string delimiter, but I'm unsure what it does/means. Any chance of a small example and an explanation of what it's doing.?



2. If I put the above function in a global functions table like...

Lua Code:
  1. EarthernFunctionTable = {
  2.                                     [1] = MyFunction,
  3.                                     [2] = MyOtherFunction
  4.                                   }

... am I correct in saying that these functions are then available to all frames (and/or even all loaded addons?)



3. I'd like to call a function with several arguments. But say I'd rather not pass the first arg... how do I skip the first argument and move on to the second?

Is it something like...

Lua Code:
  1. MyFunction(_, arg2, arg3)

... Then again, I've tried that and it's returned an error in the past.

4. Last one, I promise! I'm looking to create a function that can be called as follows...

Lua Code:
  1. MyFrame:MyFunction(arg1, arg2, arg3)

... I understand that this is done in the following way...

Lua Code:
  1. function obj:FunctionName(arg1, arg2, arg3)

... I'm not sure I understand the above instruction though. What should 'obj' be (keeping in mind that the aim is for any frame to be able to use it). Is it simply 'self'?

Regarding the examples: I think I've created all the above functions with 3 arguments. I know that a function can have several (or zero) arguments depending on what's required.


If you've got this far, thanks alot for taking the time to read . Phanx, if ever you read this, thanks very much for the Ace3 tutorial on wowace. It's been a great help so far.
__________________
__________________
  Reply With Quote
07-29-12, 02:04 AM   #2
Barjack
A Black Drake
AddOn Author - Click to view addons
Join Date: Apr 2009
Posts: 89
1.

You can learn a bit more about ... here: http://www.lua.org/pil/5.2.html

Essentially ... in a function definition represents a list of "all the rest" of the arguments that were supplied to that function. This allows a function to take in a variable number of arguments.

In the function body, you can use that ... in various ways. You can assign variables values out of it, as in "local a, b = ..." where a and b will receive the first two values in that list. You could also pull individual arguments out with select(), or loop over the list to get all the values with something like:

Lua Code:
  1. for i = 1, select('#', ...) do
  2.   local val = select(i, ...)
  3.   -- do whatever with val
  4. end

You can also turn it into a table with pack() or pass it to another function you want to call like "otherFunction(true, 1, ...)".

2.

Yes, you can put references to your function in a globally accessible place and they can be called by any other addon or another file in your own addon. Of course you need to make sure the functions and/or variable names in question are already defined before you put them in that table.

3.

In this case it's kind of up to you how you want to handle it, but basically there is no language feature to "skip" an argument. Rather you have to pass an argument and that function has to know how to deal with it. A good thing to pass might be nil, for instance, e.g. "MyFunction(nil, arg2, arg3)". But this means that MyFunction needs to know what to do if arg1 is "nil". So you'll have to code around that convention.

The basic idea is, you have to do all of this manually in your function, and there's no magic "blank" value you can use to skip an argument. Of course you can choose any value here: nil, false, -1, or any arbitrary value that MyFunction can check for to see if that argument was "skipped".

4.

This may be a little complicated but basically, it may help to understand that:
Lua Code:
  1. SomeTable = {}
  2. function SomeTable:SomeFunction()
  3.   print("Hello from " .. tostring(self))
  4. end
  5. SomeTable:SomeFunction()

is the same as

Lua Code:
  1. SomeTable = {}
  2. function SomeTable.SomeFunction(self)
  3.   print("Hello from " .. tostring(self))
  4. end
  5. SomeTable.SomeFunction(SomeTable)

which is also the same as

Lua Code:
  1. SomeTable = {}
  2. SomeTable.SomeFunction = function (self)
  3.   print("Hello from " .. tostring(self))
  4. end
  5. SomeTable.SomeFunction(SomeTable)

In both cases SomeFunction is just a function that's been assigned to a table key called SomeFunction, in a table called SomeTable.

The colon syntax is a syntactic shortcut that helps reduce clutter. What the colon in place of the period means depends on where it's used. In a function definition, it means the function will automatically assign the first argument it's given to the local variable "self". The manual version of this you can see in the second example.

When calling a function, the colon means to pass the table you're using to look up that function into the function call as the first argument. You can see the manual version of that in the second example too.

What that means is that when combined, using a : in both the definition and calling of a function means you never have to explicitly put self as the first parameter and never have to explicitly pass a value for self in when calling it. But under the hood, this is exactly what is happening.

Knowing that, all that's important for making your function visible to the outside world is making sure the table it's being assigned to is also accessible. If MyFrame is a global, then functions defined as MyFrame:SomeFunction or MyFrame.SomeFunction will all be globally accessible via MyFrame. So if that's what you want, all you need to do is make sure that MyFrame is a global already, or set explicitly to a global at some point. If MyFrame was local for example, you could do "_G.MyFrame = MyFrame" to make sure that other addons also have a globally accessible version of your table/frame.

Last edited by Barjack : 07-29-12 at 02:16 AM.
  Reply With Quote
07-29-12, 10:46 AM   #3
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,323
Originally Posted by Aanson View Post
3. I'd like to call a function with several arguments. But say I'd rather not pass the first arg... how do I skip the first argument and move on to the second?

Is it something like...

Lua Code:
  1. MyFunction(_, arg2, arg3)

... Then again, I've tried that and it's returned an error in the past.
In addition to the answer above, there is a lot of misconception going around about "_" being a special character to Lua that skips values. Truth is, Lua just sees it as another variable like "x" or "Y". Its use is more common for disregarding unneeded return values from a function or vararg expression that is faster than using select(). Lua does assign values to the variable "_", but further code usually does nothing with those values.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote
07-29-12, 01:49 PM   #4
Farmbuyer
A Cyclonian
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 43
Originally Posted by SDPhantom View Post
In addition to the answer above, there is a lot of misconception going around about "_" being a special character to Lua that skips values. Truth is, Lua just sees it as another variable like "x" or "Y". Its use is more common for disregarding unneeded return values from a function or vararg expression that is faster than using select(). Lua does assign values to the variable "_", but further code usually does nothing with those values.
Also, if you use "_" for this purpose, be sure to declare it as a local variable.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Looking for a dumbed down explanation of a couple of things!

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