I understand for loops in Lua but I have a question about what 'in pairs' means. I've googled it so many times and all I could find is complicated documentation. Lets use this as an example from [URL]http://wiki.garrysmod.com/?title=Guide_to_Derma[/URL]
[lua]
for k,v in pairs(player.GetAll()) do
DermaListView:AddLine(v:Nick(),v:Frags())
end
[/lua]
Here's some info:
~ So since player.GetAll() returns a table of players, I know a table is supposed to go in parentheses after 'pairs'.
~ Another quick question, if you don't define k or v what happens? Since they're obviously not defined in this, does the loop just keep going until there's nothing left in the table?
~ If you don't understand, every time this loops it adds a new row to the Derma List (you can see it in the link to where this code is from) and enters the player name and number of kills into the two columns.
So what does the 'in pairs' thing do?
[b]EDIT:[/b]
One more question. When you write the function of what happens when someone clicks a button, can you do:
[lua]
function DermaButtonOpen:DoClick()
RunConsoleCommand( "open" )
end
[/lua]
Instead of:
[lua]
DermaButtonOpen:DoClick = function()
RunConsoleCommand( "open" )
end
[/lua]
The second one is how the tutorials do it so I was just wondering. When I use the second method like in the tutorials my compiler gives me an error.
Right, okay, here's a quick crash-course in tables and pairs()
When you define a table, as thus: [lua]myTable={}[/lua] and then you add an entry into the table, like so: [lua]myTable["fish"] = "are good"[/lua] Then what you just did was insert a value of "are good" into table "myTable", with a key of "fish."
So basically:
[lua]TABLE[key] = value[/lua]
Now, with this in mind, the "for" operator iterates through every key in the table you provide. The "k,v" in the function are not explicit, meaning they don't [b]have[/b] to be the letters "k" and "v". However, they both [b]do[/b] have to be assigned. The first value ("k") refers to the key of the table; the second value ("v") refers to the value.
So, for example, if we did something like this:
[lua]myTable = {}
myTable["fish"] = "good"
myTable["pickles"] = "nasty"
for k,v in pairs(myTable) do
print("Gmod4ever thinks "..k.." are "..v)
end[/lua]
Then it would print the following: [code]
Gmod4ever thinks fish are good
Gmod4ever thinks pickles are nasty[/code]
Now, if we took the same code, but changed the "k,v" to something like so:
[lua]myTable = {}
myTable["fish"] = "good"
myTable["pickles"] = "nasty"
for food,taste in pairs(myTable) do
print("Gmod4ever thinks "..food.." are "..taste)
end[/lua]
Then it prints the exact same thing.
I hope this quick crash-course helps a bit. :buddy:
Thanks. The only part I really learned from your "crash course" was that k = key and v = value and that explains a lot. Thanks. (By the way, about the same time you posted this I edited my first post so if you can, try and answer my edited-in question).
The pairs statement basically crawls through each key and value(the pair) in a table and does something. This is useful where the key isn't a number, and you can't just do something like a valued loop. Essentially, it's a way of looping through non-integer data.
Okay that makes sense. Thans NicatrongTg.
[editline]08:29PM[/editline]
Now this is the only question left to be answered.
[quote]
One more question. When you write the function of what happens when someone clicks a button, can you do:
[lua]
function DermaButtonOpen:DoClick()
RunConsoleCommand( "open" )
end
[/lua]
Instead of:
[lua]
DermaButtonOpen:DoClick = function()
RunConsoleCommand( "open" )
end
[/lua]
The second one is how the tutorials do it so I was just wondering. When I use the second method like in the tutorials my compiler gives me an error.
[/quote]
I don't do much with Derma, but I'd assume so. I'm a bit rusty from my break from Lua.
In terms of your derma question, the important thing there is the use of a colon versus the use of a period. Your second example would error -- it's supposed to look like this:
[lua]DermaButtonOpen.DoClick = function() end[/lua]
With a period. So basically, "DoClick" is a function called somewhere in the code of the DermaButtonOpen, in this case when you click on it. It's just a variable, like a string or a number, except it's a function. So you can set the DoClick variable equal to a function using the example above, or you can define an [b]object-oriented function[/b] using a colon.
In Lua, functions with a colon are functions you perform on objects, in this case a DButton object. Further, functions you define with a colon have an implicit argument, or a variable that's there without you having to do anything, called "self". "self" refers back to the button itself. So,
[lua]function DermaButtonOpen:DoClick()
self:SetVisible(false)
end[/lua]
Would set the button so it's invisible.
Does that clear things up a bit?
[QUOTE=Entoros;22942050]In terms of your derma question, the important thing there is the use of a colon versus the use of a period. Your second example would error -- it's supposed to look like this:
[lua]DermaButtonOpen.DoClick = function() end[/lua]
With a period. So basically, "DoClick" is a function called somewhere in the code of the DermaButtonOpen, in this case when you click on it. It's just a variable, like a string or a number, except it's a function. So you can set the DoClick variable equal to a function using the example above, or you can define an [b]object-oriented function[/b] using a colon.
In Lua, functions with a colon are functions you perform on objects, in this case a DButton object. Further, functions you define with a colon have an implicit argument, or a variable that's there without you having to do anything, called "self". "self" refers back to the button itself. So,
[lua]function DermaButtonOpen:DoClick()
self:SetVisible(false)
end[/lua]
Would set the button so it's invisible.
Does that clear things up a bit?[/QUOTE]
Yes it does. Thanks.
pairs() is actually a function that returns the key and value of the next key in the table. The loop sets k,v to those returned values. It gets run once per key in the table.
Sorry, you need to Log In to post a reply to this thread.