View Single Post
07-26-18, 06:38 PM   #1
Theroxis
A Fallenroot Satyr
Join Date: Jun 2018
Posts: 24
Having trouble understanding looped frame creation

Basically what I'm trying to do is learn how to do various things by creating UI elements that I desire in my personal gameplay. I've learned a lot, but there's a lot left to go. I want to create several extra action buttons along the bottom side of my minimap, squished tightly together. I can create one button successfully, using SecureActionButtonTemplate and SecureHandlerDragTemplate so that the buttons can have basically anything dragged onto/off of them at will.
But when I try to loop to create multiple buttons it does not work, saying that the local variable I'm using to store the array is nil (which it should be, as I haven't defined any buttons yet) The question is how am I supposed to handle this??
Thanks for the help


Code:
local numMapButtons = math.floor(Minimap:GetWidth() / 20)
	local mapButton;
for i=1, numMapButtons do

	mapButton[i]=DragExample or CreateFrame("Button", "DragExample", UIParent, "ActionButtonTemplate,SecureActionButtonTemplate,SecureHandlerDragTemplate")
	mapButton[i]:SetWidth(20)
	mapButton[i]:SetHeight(20)
	if i==1 then
	mapButton[i]:SetPoint("TOPLEFT",Minimap,"BOTTOMLEFT",5,-5)
	else
	mapButton[i]:SetPoint("LEFT",mapButton[i-1],"RIGHT",5)
	end

	mapButton[i]:SetAttribute("_onreceivedrag", [[
	  -- kind,value,... are returns of GetCursorInfo()
	  if kind=="item" or kind=="spell" or kind=="macro" then
	    self:SetAttribute("type",kind)
	    if kind=="item" then
	      value = "item:"..value
	    elseif kind=="spell" then
	      value = select(2,...) -- the raw spell id (4th return GetCursorInfo)
	    end
	    -- "item","item:1234" or "spell",1234 or "macro",1234
	    self:SetAttribute(kind,value)
	    return "clear"
	  end
	]])

	mapButton[i]:SetScript("OnAttributeChanged",function(self,attribType,attribDetail)
	  -- only changing texture. not dealing with cooldowns or item counts
	  if attribType=="item" then
	    self.icon:SetTexture((select(10,GetItemInfo(attribDetail))))
	  elseif attribType=="spell" then
	    self.icon:SetTexture((select(3,GetSpellInfo(attribDetail))))
	  elseif attribType=="macro" then
	    self.icon:SetTexture((select(2,GetMacroInfo(attribDetail))))
	  end
	end)
end
EDIT:
I'm an idiot, I need to declare the variable as an array:
local mapButton ={}; is sufficient to make that happen.

From there I've got other problems, but this makes more sense now. I obviously need to change the way I am using SetPoint now that I've progressed forward. PROGRESS THOUGH!

Last edited by Theroxis : 07-26-18 at 07:14 PM.
  Reply With Quote