Quantcast Storing frames into an array? - WoWInterface
Thread Tools Display Modes
07-04-13, 12:48 PM   #1
infernosnow
A Defias Bandit
Join Date: Jan 2009
Posts: 2
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.
  Reply With Quote
07-04-13, 01:00 PM   #2
semlar
A Pyroguard Emberseer
 
semlar's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2007
Posts: 1,059
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.

Last edited by semlar : 07-04-13 at 01:05 PM.
  Reply With Quote
07-04-13, 03:30 PM   #3
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
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.
__________________
Author/maintainer of Grid, PhanxChat, oUF_Phanx, and many more.
Troubleshoot an addonTurn any code into an addonMore addon resources
Need help with your code? Post all of your actual code! Attach or paste your files.
Please don’t PM me about addon bugs or code questions. Post a comment or forum thread instead!
  Reply With Quote
07-04-13, 03:49 PM   #4
Vlad
A Molten Giant
 
Vlad's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2005
Posts: 792
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!
__________________
Profile: Curse | Wowhead
  Reply With Quote
07-05-13, 02:31 AM   #5
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 1,840
Originally Posted by Phanx View Post
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
__________________
ESOUI AddOns | WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote

WoWInterface » Developer Discussions » General Authoring Discussion » Storing frames into an array?

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