Quantcast
Help with ipairs - WoWInterface
Thread Tools Display Modes
07-21-17, 04:28 AM   #1
Ereki
A Fallenroot Satyr
AddOn Author - Click to view addons
Join Date: Apr 2008
Posts: 24
Help with ipairs

Hi I'm playing around with ipairs and DK runes trying to set the alpha of the individual runes using it but I can't figure out how to use it properly

Code:
local runeID = {1,2,3,4,5,6}
for index,value in ipairs(runeID) do
	local start, duration, runeReady = GetRuneCooldown(value)
	if runeReady == false then
		RuneFrame.Rune..value:SetAlpha(RHNoCombatAlpha)
	end
end
Is sort of what I want but it obviously doesn't work, how do I use the value in a frame name without breaking the entire thing?

(RHNoCombatAlpha is a variable set in another part of the lua file)
  Reply With Quote
07-21-17, 04:52 AM   #2
MunkDev
A Scalebane Royal Guard
 
MunkDev's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2015
Posts: 427
Frames are tables and references within them are just keys in that table. You access a specific object by concatenating the strings together in brackets. The concatenation precedes the table lookup. Also, you don't need ipairs for what you're trying to do.

Lua Code:
  1. for i=1, 6 do
  2.     local start, duration, runeReady = GetRuneCooldown(i)
  3.     if not runeReady then
  4.         RuneFrame['Rune' .. i]:SetAlpha(RHNoCombatAlpha)
  5.     end
  6. end

ipairs is useful for sorted arrays with a number key and some value in each slot, whereas you would use pairs to iterate over an entire table in no particular order. When you're just using number indices for API calls, you don't need a custom iterator.
In your case, the table you're making just looks like this internally:
Lua Code:
  1. local runeID = {
  2.     [1] = 1,
  3.     [2] = 2,
  4.     [3] = 3,
  5.     [4] = 4,
  6.     [5] = 5,
  7.     [6] = 6,
  8. }

As you can probably tell, this is rather pointless and just creates overhead.

ipairs/pairs would be useful if you design this differently so that your table makes sense. If you use your table to reference your frames instead, like this:
Lua Code:
  1. local runes = {}
  2. for i=1, 6 do
  3.     runes[i] = RuneFrame['Rune'..i]
  4. end

...you could use your runes table to store and access the individual runes in one go:
Lua Code:
  1. for i, rune in ipairs(runes) do
  2.     local start, duration, runeReady = GetRuneCooldown(i)
  3.     if not runeReady then
  4.         rune:SetAlpha(RHNoCombatAlpha)
  5.     end
  6. end
__________________

Last edited by MunkDev : 07-25-17 at 12:44 PM.
  Reply With Quote
07-25-17, 10:16 AM   #3
Rainrider
A Firelord
AddOn Author - Click to view addons
Join Date: Nov 2008
Posts: 453
For indexed arrays (like in your case) prefer using the numeric for-loop (you can use the # table operator as the limit here). Use ipairs if you have deeply nested arrays or if it makes the code more readable (the second part of this sentence is hardly an argument as you can use local values or upvalues here). Use either "next, t" or "pairs(t)" for associative arrays only (MunkDev's last example should have used either numeric for or ipairs instead of pairs for efficiency reasons)
  Reply With Quote
07-25-17, 12:44 PM   #4
MunkDev
A Scalebane Royal Guard
 
MunkDev's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2015
Posts: 427
Originally Posted by Rainrider View Post
(MunkDev's last example should have used either numeric for or ipairs instead of pairs for efficiency reasons)
That was just a typo.
__________________
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Help with ipairs

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