Thread Tools Display Modes
10-22-13, 04:26 PM   #1
Mayron
A Frostmaul Preserver
 
Mayron's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 275
Got an issue with using a For loop to create frames

I know there is something wrong with this code but I cannot figure out how to make it work. I want to create 4 frames that are nearly identical so I thought I could create them in a for loop but then I need to add different text to each frame as well as some script handlers like an OnClick script and more.. so I thought of making a for loop would be the right way to go but now I'm not sure. Should I just duplicate the code in the for loop 4 times for each frame and forget about the for loop or is there a way to make this work? The issue is that when I try to "SetScript("OnShow", function(self)" on a newly created frame from the for loop, it says it tries to index it as a nil value which does not surprise me either.

Thank you for reading

Lua Code:
  1. local infoSet = {
  2.     infoFrame = {info1, info2, info3, info4},
  3.     imageAnchor = {Image1, Image2, Image3, Image4},
  4.     button = {button1, button2, button3, button4},
  5. }    
  6.  
  7. for i = 1, 4 do
  8.     local infoBox
  9.     infoBox = infoSet.infoFrame[i]
  10.     infoBox = CreateFrame("Frame")
  11.     infoBox:SetSize(420, 90)
  12.     infoBox:SetPoint("BOTTOMLEFT", infoSet.imageAnchor[i], "BOTTOMRIGHT", -12, 3.5)
  13.     infoBox:SetFrameStrata("TOOLTIP")
  14.     infoBox:SetFrameLevel("16")
  15.     infoBox:SetBackdrop(Artwork_Import.infoBox)
  16.    
  17.     local button = infoSet.button[i]
  18.     button = CreateFrame("Button")
  19.     button:SetSize(80, 24)
  20.     button:SetPoint("BOTTOMRIGHT", infoBox, "BOTTOMRIGHT", -12, 10)
  21.     button:SetFrameStrata("TOOLTIP")
  22.     button:SetFrameLevel("17")
  23.     button:SetBackdrop(Artwork_Import.enableButton)
  24.     button:SetBackdropBorderColor(0.2,0.2,0.2)
  25. end
  26.  
  27. infoSet.infoFrame[1]:SetScript("OnShow", function(self)
  28.     self:SetScript("OnShow", nil)
  29.     self.text = self:CreateFontString(nil, "OVERLAY")
  30.     self.text:SetPoint("CENTER", self, "CENTER")
  31.     self.text:SetFont(FONT, 10)
  32.     self.text:SetText(TEXT.BottomChat)
  33.     self.text:SetTextColor(classcolor.r, classcolor.g, classcolor.b)
  34. end)

Last edited by Mayron : 10-23-13 at 03:24 AM.
  Reply With Quote
10-22-13, 07:42 PM   #2
kurapica.igas
A Chromatic Dragonspawn
Join Date: Aug 2011
Posts: 152
Lua Code:
  1. local infoSet = {
  2.     infoFrame = {info1, info2, info3, info4},
  3.     imageAnchor = {Image1, Image2, Image3, Image4},
  4.     button = {button1, button2, button3, button4},
  5. }  
  6.  
  7. for i = 1, 4 do
  8.     local infoBox
  9.     infoBox = infoSet.infoFrame[i]
  10.     infoBox = CreateFrame("Frame")
  11.     infoBox:SetSize(420, 90)
  12.     infoBox:SetPoint("BOTTOMLEFT", infoSet.imageAnchor[i], "BOTTOMRIGHT", -12, 3.5)
  13.     infoBox:SetFrameStrata("TOOLTIP")
  14.     infoBox:SetFrameLevel("16")
  15.     infoBox:SetBackdrop(Artwork_Import.infoBox)
  16.     infoBox:Hide()
  17.  
  18.     infoSet.infoFrame[i] = infoBox
  19.    
  20.     local button = infoSet.button[i]
  21.     button = CreateFrame("Button")
  22.     button:SetSize(80, 24)
  23.     button:SetPoint("BOTTOMRIGHT", infoBox, "BOTTOMRIGHT", -12, 10)
  24.     button:SetFrameStrata("TOOLTIP")
  25.     button:SetFrameLevel("17")
  26.     button:SetBackdrop(Artwork_Import.enableButton)
  27.     button:SetBackdropBorderColor(0.2,0.2,0.2)
  28.  
  29.     infoSet.button[i] = button
  30. end
  31.  
  32. infoSet.infoFrame[1]:SetScript("OnShow", function(self)
  33.     self:SetScript("OnShow", nil)
  34.     self.text = self:CreateFontString(nil, "OVERLAY")
  35.     self.text:SetPoint("CENTER", self, "CENTER")
  36.     self.text:SetFont(FONT, 10)
  37.     self.text:SetText(TEXT.BottomChat)
  38.     self.text:SetTextColor(classcolor.r, classcolor.g, classcolor.b)
  39. end)
  40.  
  41. infoSet.infoFrame[1]:Show()

Don't know what info1, info2, button1, button2 are, but seems you want keep the created frames in the table, local button = infoSet.button[i] don't make the infoSet.button[i] point to the button, you should save it to the table after you create it.

And you must hide the frame first, then show it to make sure the OnShow handler be called.

Last edited by kurapica.igas : 10-22-13 at 07:45 PM.
  Reply With Quote
10-22-13, 07:51 PM   #3
kurapica.igas
A Chromatic Dragonspawn
Join Date: Aug 2011
Posts: 152
Lua Code:
  1. local infoSet = {
  2.     infoFrame = {info1, info2, info3, info4},
  3.     imageAnchor = {Image1, Image2, Image3, Image4},
  4.     button = {button1, button2, button3, button4},
  5. }
  6.  
  7. local function CreateInfoFrame()
  8.     local i = #(infoSet.infoFrame) + 1
  9.  
  10.     local infoBox
  11.     infoBox = CreateFrame("Frame")
  12.     infoBox:SetSize(420, 90)
  13.     infoBox:SetPoint("BOTTOMLEFT", infoSet.imageAnchor[i], "BOTTOMRIGHT", -12, 3.5)
  14.     infoBox:SetFrameStrata("TOOLTIP")
  15.     infoBox:SetFrameLevel("16")
  16.     infoBox:SetBackdrop(Artwork_Import.infoBox)
  17.  
  18.     infoSet.infoFrame[i] = infoBox
  19.    
  20.     local button
  21.     button = CreateFrame("Button")
  22.     button:SetSize(80, 24)
  23.     button:SetPoint("BOTTOMRIGHT", infoBox, "BOTTOMRIGHT", -12, 10)
  24.     button:SetFrameStrata("TOOLTIP")
  25.     button:SetFrameLevel("17")
  26.     button:SetBackdrop(Artwork_Import.enableButton)
  27.     button:SetBackdropBorderColor(0.2,0.2,0.2)
  28.  
  29.     infoSet.button[i] = button
  30.  
  31.     return infoBox
  32. end
  33.  
  34. -- 1
  35. local infoBox = CreateInfoFrame()
  36.  
  37. do
  38.     infoBox.text = infoBox:CreateFontString(nil, "OVERLAY")
  39.     infoBox.text:SetPoint("CENTER", infoBox, "CENTER")
  40.     infoBox.text:SetFont(FONT, 10)
  41.     infoBox.text:SetText(TEXT.BottomChat)
  42.     infoBox.text:SetTextColor(classcolor.r, classcolor.g, classcolor.b)
  43. end
  44.  
  45. -- 2
  46. local infoBox = CreateInfoFrame()
  47.  
  48. do
  49.     infoBox.text = infoBox:CreateFontString(nil, "OVERLAY")
  50.     -- more
  51. end

If you want create a things like template, you could create a factory function first, use it to create those infoboxs, don't know why you want using OnShow, just create those fontstring directly after you create each infobox should works ok
  Reply With Quote
10-23-13, 02:38 AM   #4
Mayron
A Frostmaul Preserver
 
Mayron's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 275
Thank you very much for your help, I will try out the code later today. Read through it and it definitely looks like the right steps to take. Ii did think I would need to use "return" at some point but was far too confused at the time. I used an "OnShow" as the frames being created usually always need to be hidden and only shown when needed which is not often so they do not need to be loaded each time the addon is executed. I thought it would safe unneeded load time but I'm no expert so I'm not sure if that is true or not.

Thanks again!
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Got an issue with using a For loop to create frames


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