Thread Tools Display Modes
11-26-14, 07:14 PM   #1
MoonWitch
A Firelord
AddOn Author - Click to view addons
Join Date: Sep 2007
Posts: 455
Trying to understand a few concepts

I am working through oBindings by haste -- making sense of it... And I got to this little bit which even my googleFu can't understand

Basically I get lost on the
Lua Code:
  1. for key, action in next, tbl do
I have NO idea what the function of the next there is. Normally it should be a keyword/function to go to the next part of the table, but in this I just don't get what it does.
-- Note the comments are mine and may not make sense.
Lua Code:
  1. function _NS:RegisterKeyBindings(name, ...)
  2.   local bindings = {}
  3.  
  4.   for i=1, select('#', ...) do -- for i=1, number of entries in the ellipsis (which are the possible tables passed via registerkeybindings)
  5.     local tbl = select(i, ...) -- pushes the possible tables into tbl
  6.     for key, action in next, tbl do -- puts [x] into key, the value into action of tbl
  7.       if(type(action) == 'table') then -- ??
  8.         for mod, modAction in next, action do -- (['CTRL-V'] = 'Paste' - CTRL = mod, 'paste' is modAction)
  9.           if(not bindings[key]) then -- if no key is listed, it's just a spell, so increment and bind it to the number keys
  10.             bindings[key] = {}
  11.           end
  12.  
  13.           bindings[key][mod] = modAction
  14.         end
  15.       else
  16.         bindings[key] = action -- normal keybind
  17.       end
  18.     end
  19.   end
  20.  
  21.   _BINDINGS[name] = bindings
  22. end

For those of you willing to see the entire code instead of just a part :
https://github.com/haste/oBindings/b...r/bindings.lua
__________________

Last edited by MoonWitch : 11-26-14 at 08:00 PM.
  Reply With Quote
11-26-14, 07:34 PM   #2
Lombra
A Molten Giant
 
Lombra's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 554
"t/l" is a typo? Either way, doing that should, for all intents and purposes, be the same as using pairs, because basically pairs returns a function, whereas here you just provide a function directly. Not completely sure of the details and too tired to elaborate right now!
__________________
Grab your sword and fight the Horde!
  Reply With Quote
11-26-14, 08:00 PM   #3
MoonWitch
A Firelord
AddOn Author - Click to view addons
Join Date: Sep 2007
Posts: 455
Originally Posted by Lombra View Post
"t/l" is a typo? Either way, doing that should, for all intents and purposes, be the same as using pairs, because basically pairs returns a function, whereas here you just provide a function directly. Not completely sure of the details and too tired to elaborate right now!
Yep, typo.. Fixed now -- sorry

Sooo
Code:
for key, action in next, tbl do
is just a more confusing way to say
Code:
for key, action in pairs(tbl) do
edit : tbl isn't a typo, I typed tbl and it changed after saving the post....
__________________

Last edited by MoonWitch : 11-26-14 at 08:03 PM.
  Reply With Quote
11-27-14, 02:16 AM   #4
Duugu
Premium Member
 
Duugu's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 851
Originally Posted by MoonWitch View Post
is just a more confusing way to say
Code:
for key, action in pairs(tbl) do
Exactly. From http://www.lua.org/manual/5.2/manual.html#pdf-pairs:
If t has a metamethod __pairs, calls it with t as argument and returns the first three results from the call.
Otherwise, returns three values: the next function, the table t, and nil
  Reply With Quote
11-27-14, 11:04 AM   #5
MoonWitch
A Firelord
AddOn Author - Click to view addons
Join Date: Sep 2007
Posts: 455
Ah thanks, this helps tremendously.

Yes, this is how I learn, by picking apart addons and walk myself through the code in my own language.
__________________
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Trying to understand a few concepts


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