Thread Tools Display Modes
04-14-11, 02:08 PM   #1
Mischback
A Cobalt Mageweaver
 
Mischback's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2009
Posts: 221
SetScript and loops

Hello!

I'm kind of into archaeology atm, and writing a small addon about it to make it more comfortable to me (watching TV while digging simplifies everything!).

I'm encountering a problem, when I want to make a "solve-button".

I got a window with a statusbar of the current project of every race, next to the artifact icon. I want to be able to click on that icon, if I got enough artifacts, but my SetScript don't works:

lua Code:
  1. PredatorArchy:RegisterEvent('PLAYER_ENTERING_WORLD')
  2. PredatorArchy:SetScript('OnEvent', function()
  3.     local i, raceName, raceTex
  4.  
  5.     local _, _, hasArch = GetProfessions()
  6.  
  7.     if ( hasArch ) then
  8.         -- Setting up the main frame
  9.         PredatorArchy:SetBackdrop( {
  10.                 bgFile = solidTex,
  11.                 edgeFile = borderTex,
  12.                 tile = false,
  13.                 edgeSize = 8,
  14.                 insets = { left = 4, right = 4, top = 4, bottom = 4 }
  15.         } )
  16.         PredatorArchy:SetBackdropColor(0, 0, 0, 0.7)
  17.         PredatorArchy:SetWidth(300)
  18.         PredatorArchy:SetHeight(40)
  19.         PredatorArchy:EnableMouse(true)
  20.         PredatorArchy:RegisterForDrag("LeftButton","RightButton")
  21.         PredatorArchy:SetMovable(true)
  22.         PredatorArchy:SetUserPlaced(true)
  23.         PredatorArchy:SetScript("OnDragStart", function(self) if IsAltKeyDown() then self:StartMoving() end end)
  24.         PredatorArchy:SetScript("OnDragStop", function(self) self:StopMovingOrSizing() end)
  25.         PredatorArchy:SetPoint('CENTER')
  26.  
  27.         PredatorArchy.SkillRank = CreateFrame('StatusBar', nil, PredatorArchy)
  28.         PredatorArchy.SkillRank:SetPoint('TOPLEFT', 15, -10)
  29.         PredatorArchy.SkillRank:SetPoint('BOTTOMRIGHT', PredatorArchy, 'TOPRIGHT', -15, -30)
  30.         PredatorArchy.SkillRank:SetStatusBarTexture(barTex)
  31.         PredatorArchy.SkillRank:SetStatusBarColor(0, 0.7, 0)
  32.         PredatorArchy.SkillRank:RegisterEvent('ARTIFACT_COMPLETE')
  33.         PredatorArchy.SkillRank:SetScript('OnEvent', function()
  34.             core.UpdateSkill(hasArch)
  35.             core.UpdateArtifacts()
  36.         end)
  37.  
  38.         PredatorArchy.SkillRank.back = PredatorArchy.SkillRank:CreateTexture(nil, 'BACKGROUND')
  39.         PredatorArchy.SkillRank.back:SetAllPoints(PredatorArchy.SkillRank)
  40.         PredatorArchy.SkillRank.back:SetTexture(barTex)
  41.         PredatorArchy.SkillRank.back:SetVertexColor(0.3, 0.3, 0.3, 1)
  42.  
  43.         PredatorArchy.SkillRank.text = lib.CreateFontObject(PredatorArchy.SkillRank, 14, font)
  44.         PredatorArchy.SkillRank.text:SetPoint('CENTER', 0, 1)
  45.         PredatorArchy.SkillRank.text:SetText('INIT')
  46.         PredatorArchy.SkillRank.text:SetJustifyH('CENTER')
  47.  
  48.         PredatorArchy.List = {}
  49.         for i = 1, GetNumArchaeologyRaces() do
  50.             raceName, raceTex = GetArchaeologyRaceInfo(i)
  51.  
  52.             PredatorArchy.List[raceName] = {}
  53.             PredatorArchy.List[raceName].raceName = raceName
  54.             PredatorArchy.List[raceName].raceIcon = PredatorArchy:CreateTexture(nil, 'OVERLAY')
  55.             PredatorArchy.List[raceName].raceIcon:SetSize(36,36)
  56.             PredatorArchy.List[raceName].raceIcon:SetTexture(raceTex)
  57.             PredatorArchy.List[raceName].raceIcon:SetPoint('TOPLEFT', PredatorArchy.SkillRank, 'TOPLEFT', 0, -(i*25))
  58.  
  59.             PredatorArchy.List[raceName].artifactIcon = CreateFrame('Button', nil, PredatorArchy)
  60.             PredatorArchy.List[raceName].artifactIcon:SetSize(21, 21)
  61.             PredatorArchy.List[raceName].artifactIcon:SetPoint('RIGHT', PredatorArchy, 'RIGHT', -15, 0)
  62.             PredatorArchy.List[raceName].artifactIcon:SetPoint('TOP', PredatorArchy.List[raceName].raceIcon, 'TOP', 0, -1)
  63.             PredatorArchy.List[raceName].artifactIcon:SetScript('OnClick', function()
  64.                 lib.debugging(PredatorArchy.List[raceName].raceName)
  65.                 -- core.SolveArtifact(PredatorArchy.List[raceName].raceName)
  66.             end)
  67.  
  68.             PredatorArchy.List[raceName].progress = CreateFrame('StatusBar', nil, PredatorArchy)
  69.             PredatorArchy.List[raceName].progress:SetStatusBarTexture(barTex)
  70.             PredatorArchy.List[raceName].progress:SetPoint('TOPLEFT', PredatorArchy.List[raceName].raceIcon, 'TOPRIGHT', -10, -1)
  71.             PredatorArchy.List[raceName].progress:SetPoint('BOTTOM', PredatorArchy.List[raceName].raceIcon, 'TOP', 0, -22)
  72.             PredatorArchy.List[raceName].progress:SetPoint('RIGHT', PredatorArchy.List[raceName].artifactIcon, 'LEFT', -10, 0)
  73.             PredatorArchy.List[raceName].progress:SetMinMaxValues(0, 1)
  74.             PredatorArchy.List[raceName].progress:SetValue(0)
  75.  
  76.             PredatorArchy.List[raceName].progress.back = PredatorArchy.List[raceName].progress:CreateTexture(nil, 'BACKGROUND')
  77.             PredatorArchy.List[raceName].progress.back:SetAllPoints(PredatorArchy.List[raceName].progress)
  78.             PredatorArchy.List[raceName].progress.back:SetTexture(barTex)
  79.             PredatorArchy.List[raceName].progress.back:SetVertexColor(0.3, 0.3, 0.3, 1)
  80.  
  81.             PredatorArchy.List[raceName].progress.text = lib.CreateFontObject(PredatorArchy.List[raceName].progress, 14, font)
  82.             PredatorArchy.List[raceName].progress.text:SetPoint('LEFT', 10, 1)
  83.             PredatorArchy.List[raceName].progress.text:SetText('INIT')
  84.  
  85.             PredatorArchy:SetHeight(PredatorArchy:GetHeight()+26)
  86.         end
  87.  
  88.         PredatorArchy:UnregisterAllEvents()
  89.         PredatorArchy:RegisterEvent('CHAT_MSG_CURRENCY')
  90.         PredatorArchy:SetScript('OnEvent', core.UpdateArtifacts)
  91.  
  92.         core.UpdateSkill(hasArch)
  93.         core.UpdateArtifacts()
  94.     end
  95.  
  96. end)

Lines 63/64 are my problems: When I click the button, a chat message is displayed with the name of the "clicked" race, so is the plan at least.
I always get the name of the race, with the highest ID (on my german client, it's "Anderes", should be "others".

I'm kind of sure, this is related to setting the script-handler inside of that loop, which iterates all races, but on the other hand, it doesn't seem logic, because setting the right textures and stuff is working.

Can someone give me a hint please?
__________________
  Reply With Quote
04-14-11, 03:17 PM   #2
Vrul
A Scalebane Royal Guard
 
Vrul's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2007
Posts: 404
The way you have your OnClick handler set up PredatorArchy.List[raceName].raceName will always return the last entry because raceName does not get changed outside of that loop.

A quick fix would be to change line 50 to:
Code:
local raceName, raceTex = GetArchaeologyRaceInfo(i)
That would also allow you to remove line 3 (local i, raceName, raceTex).

To really just clean it up in general I would suggest something more along the lines of:
lua Code:
  1. PredatorArchy:RegisterEvent('PLAYER_ENTERING_WORLD')
  2. PredatorArchy:SetScript('OnEvent', function(self)
  3.     local _, _, hasArch = GetProfessions()
  4.     self:UnregisterAllEvents()
  5.     if not hasArch then
  6.         self:SetScript('OnEvent', nil)
  7.         return
  8.     end
  9.     local numArchRaces = GetNumArchaeologyRaces()
  10.  
  11.     -- Setting up the main frame
  12.     self:SetBackdrop( {
  13.         bgFile = solidTex,
  14.         edgeFile = borderTex,
  15.         edgeSize = 8,
  16.         insets = { left = 4, right = 4, top = 4, bottom = 4 }
  17.     } )
  18.     self:SetBackdropColor(0, 0, 0, 0.7)
  19.     self:SetWidth(300)
  20.     self:SetHeight(numArchRaces * 26 + 40)
  21.     self:EnableMouse(true)
  22.     self:RegisterForDrag('LeftButton', 'RightButton')
  23.     self:SetMovable(true)
  24.     self:SetUserPlaced(true)
  25.     self:SetScript('OnDragStart', function(self)
  26.         if not IsAltKeyDown() then return end
  27.         self:StartMoving()
  28.     end)
  29.     self:SetScript('OnDragStop', self.StopMovingOrSizing)
  30.     self:SetPoint('CENTER')
  31.  
  32.     local skillRank = CreateFrame('StatusBar', nil, self)
  33.     skillRank:SetPoint('TOPLEFT', 15, -10)
  34.     skillRank:SetPoint('BOTTOMRIGHT', self, 'TOPRIGHT', -15, -30)
  35.     skillRank:SetStatusBarTexture(barTex)
  36.     skillRank:SetStatusBarColor(0, 0.7, 0)
  37.     skillRank:RegisterEvent('ARTIFACT_COMPLETE')
  38.     skillRank:SetScript('OnEvent', function()
  39.         core.UpdateSkill(true)
  40.         core.UpdateArtifacts()
  41.     end)
  42.     self.SkillRank = skillRank
  43.  
  44.     local back = skillRank:CreateTexture(nil, 'BACKGROUND')
  45.     back:SetAllPoints()
  46.     back:SetTexture(barTex)
  47.     back:SetVertexColor(0.3, 0.3, 0.3, 1)
  48.     skillRank.back = back
  49.  
  50.     local text = lib.CreateFontObject(skillRank, 14, font)
  51.     text:SetPoint('CENTER', 0, 1)
  52.     text:SetText('INIT')
  53.     text:SetJustifyH('CENTER')
  54.     skillRank.text = text
  55.  
  56.     local function OnClick(self)
  57.         lib.debugging(self.raceName)
  58.         -- core.SolveArtifact(self.raceName)
  59.     end
  60.  
  61.     local list = { }
  62.     self.List = list
  63.     for index = 1, numArchRaces do
  64.         local name, texture = GetArchaeologyRaceInfo(index)
  65.  
  66.         local icon = self:CreateTexture(nil, 'OVERLAY')
  67.         icon:SetSize(36, 36)
  68.         icon:SetTexture(texture)
  69.         icon:SetPoint('TOPLEFT', skillRank, 'TOPLEFT', 0, index * -25)
  70.  
  71.         local artifact = CreateFrame('Button', nil, self)
  72.         artifact:SetSize(21, 21)
  73.         artifact:SetPoint('RIGHT', self, 'RIGHT', -15, 0)
  74.         artifact:SetPoint('TOP', icon, 'TOP', 0, -1)
  75.         artifact:SetScript('OnClick', OnClick)
  76.         artifact.raceName = name
  77.  
  78.         local progress = CreateFrame('StatusBar', nil, self)
  79.         progress:SetStatusBarTexture(barTex)
  80.         progress:SetPoint('TOPLEFT', icon, 'TOPRIGHT', -10, -1)
  81.         progress:SetPoint('BOTTOM', icon, 'TOP', 0, -22)
  82.         progress:SetPoint('RIGHT', artifact, 'LEFT', -10, 0)
  83.         progress:SetMinMaxValues(0, 1)
  84.         progress:SetValue(0)
  85.  
  86.         local back = progress:CreateTexture(nil, 'BACKGROUND')
  87.         back:SetAllPoints()
  88.         back:SetTexture(barTex)
  89.         back:SetVertexColor(0.3, 0.3, 0.3, 1)
  90.         progress.back = back
  91.  
  92.         local text = lib.CreateFontObject(progress, 14, font)
  93.         text:SetPoint('LEFT', 10, 1)
  94.         text:SetText('INIT')
  95.         progress.text = text
  96.  
  97.         list[name] = {
  98.             raceName = name,
  99.             raceIcon = icon,
  100.             artifactIcon = artifact,
  101.             progress = progress
  102.         }
  103.     end
  104.  
  105.     self:RegisterEvent('CHAT_MSG_CURRENCY')
  106.     self:SetScript('OnEvent', core.UpdateArtifacts)
  107.  
  108.     core.UpdateSkill(true)
  109.     core.UpdateArtifacts()
  110. end)
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » SetScript and loops

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