WoWInterface

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

Ereki 07-21-17 04:28 AM

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)

MunkDev 07-21-17 04:52 AM

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

Rainrider 07-25-17 10:16 AM

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)

MunkDev 07-25-17 12:44 PM

Quote:

Originally Posted by Rainrider (Post 324328)
(MunkDev's last example should have used either numeric for or ipairs instead of pairs for efficiency reasons)

That was just a typo.


All times are GMT -6. The time now is 02:46 PM.

vBulletin © 2020, Jelsoft Enterprises Ltd
© 2004 - 2020 MMOUI