WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   General Authoring Discussion (https://www.wowinterface.com/forums/forumdisplay.php?f=20)
-   -   Storing frames into an array? (https://www.wowinterface.com/forums/showthread.php?t=46770)

infernosnow 07-04-13 12:48 PM

Storing frames into an array?
 
I need to create 50 different CheckButtons and wanted to do so like this:

Lua Code:
  1. local chkBxArPosX = {180, 250, 330, 410, 500};
  2.     local chkBxArPosY = {-30, -50, -70, -90, -110, -130, -150, -170, -190, -210};
  3.     for i=0,i<10 do
  4.         for k=0,k<5 do
  5.             chkBxAr[(i*5)+k] = CreateFrame("CheckButton", "LLcheckB_n", newRaidWin, "UICheckButtonTemplate")
  6.             LLcheck1aBL:SetPoint("TOPLEFT", newRaidWin, "TOPLEFT", chkBxArPosX[k], chkBxArPosX[i]);
  7.             LLcheck1aBL:SetWidth(22);
  8.             LLcheck1aBL:SetHeight(22);
  9.             LLcheck1aBL:Hide();
  10.         end
  11.     end

where chkBxAr is a global array. I seems to be giving me trouble and I wondered if this is even doable. I'm not familiar enough with methods of troubleshooting code, so it's difficult for me to diagnose why this code doesn't work, but I figured I would ask. I would rather not repeat the same createFrame() code 50 times over for all 50 of my CheckButtons that I need.

semlar 07-04-13 01:00 PM

First, your for loops should just look like for i = 0, 9 instead of i = 0, i < 10.

Secondly, you're naming all of your buttons the same thing (LLcheckB_n) which wouldn't break it but is probably not what you want to do.

But mainly you are referring to the button as LLcheck1aBL but never actually assign that variable to the frame you created.

I would also personally rename all of your variables to something more comprehensible because that's contributing to the difficulty of debugging this.

Oh also table indexes start at 1, so you'll need to add 1 to k or i when you look them up. You also have chkBxArPosX instead of chkBxArPosY in your SetPoint function.

Phanx 07-04-13 03:30 PM

Rather than storing a bunch of predictably increasing numbers in arrays, why not just do a little math inside your loop? For example, the code you posted theoretically creates 10 frames. Here's a cleaner way to do the same thing:

Code:

local frames = {}
for i = 1, 10 do
  local f = CreateFrame("Frame", "LLCheckButton"..i, newRaidWin, "UICheckButtonTemplate")
  f:SetPoint("TOPLEFT", newRaidWin, 180 + (i > 1 and (80 * floor(i / 2)) or 0), -30 - (20 * (i-1))
  f:SetSize(22, 22)
  f:Hide()
  frames[i] = f
end

Math may be faulty (I didn't test it at all) but the theory is sound, and a lot more flexible than storing fixed arrays of coordinates.

Vlad 07-04-13 03:49 PM

If it wasn't for UICheckButtonTemplate I'd probably not even name these frames anything. I assume that the template requires names for inherited scripts, if not, get those global names away! :D

SDPhantom 07-05-13 02:31 AM

Quote:

Originally Posted by Phanx (Post 280801)
Math may be faulty (I didn't test it at all) but the theory is sound, and a lot more flexible than storing fixed arrays of coordinates.

Fixed for you, still untested.

I'm noting the original X spacing was not uniform at (left-to-right) 70px, 80px, 80px, and 90px. The new spacing will remain 80px. The OP called for the creation of 50 buttons in a grid layout. I fixed this so the checkbuttons aren't staggered in X position while iterating down the Y position.

Code:

local frames = {}
for i = 1, 50 do
        local f = CreateFrame("Frame", "LLCheckButton" .. i, newRaidWin, "UICheckButtonTemplate")
        f:SetPoint("TOPLEFT", newRaidWin, -180 - ((i - 1) % 5) * 80, -30 - floor((i - 1) / 5) * 20)
        f:SetSize(22, 22)
        f:Hide()
        frames[i] = f
end



All times are GMT -6. The time now is 08:03 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI