Thread Tools Display Modes
12-03-16, 01:58 PM   #1
Tesser
A Deviate Faerie Dragon
Join Date: Oct 2016
Posts: 15
creating frames with for loops

I've made this generic because the actual loop is 50+ lines and I'm just wondering if I'm missing some basics here.
Everything is working except that when I go to print some information about the frame in the "mouseover" event (in this case just "i"), it always prints the information from the last frame created in the loop.
It seems like the logic should have it printing information to it's particular frame, no?

So in this case, say there were 5 table entries - hovering over any of the 5 frames created would print "5".

Code:
for i=1, #table, 1 do
	frames[i]=CreateFrame()
	frames[i]:SetPoint('CENTER', UI, 'CENTER' x, y)
	frames[i]:SetScript("OnEnter", function()
			print(i)
		end)
	frames[i]:SetScript("OnLeave", function() end)
end
  Reply With Quote
12-03-16, 03:06 PM   #2
Clamsoda
A Frostmaul Preserver
Join Date: Nov 2011
Posts: 269
Post your actual function, or if it is too long, the file.
  Reply With Quote
12-03-16, 04:29 PM   #3
Banknorris
A Chromatic Dragonspawn
 
Banknorris's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2014
Posts: 153
Lua Code:
  1. for i=1, #table, 1 do
  2.     frames[i]=CreateFrame()
  3.     frames[i].i=i
  4.     frames[i]:SetPoint('CENTER', UI, 'CENTER' x, y)
  5.     frames[i]:SetScript("OnEnter", function(self)
  6.             print(self.i)
  7.         end)
  8.     frames[i]:SetScript("OnLeave", function() end)
  9. end
__________________
"In this world nothing can be said to be certain, except that fractional reserve banking is a Ponzi scheme and that you won't believe it." - Mandrill

Last edited by Banknorris : 12-03-16 at 05:22 PM. Reason: Missing self declaration, thansk to Kanegasi.
  Reply With Quote
12-03-16, 05:01 PM   #4
Kanegasi
A Molten Giant
 
Kanegasi's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2007
Posts: 666
Originally Posted by Banknorris View Post
Lua Code:
  1. for i=1, #table, 1 do
  2.     frames[i]=CreateFrame()
  3.     frames[i].i=i
  4.     frames[i]:SetPoint('CENTER', UI, 'CENTER' x, y)
  5.     frames[i]:SetScript("OnEnter", function()
  6.             print(self.i)
  7.         end)
  8.     frames[i]:SetScript("OnLeave", function() end)
  9. end

You forgot the self declaration.

Lua Code:
  1. frames[i]:SetScript("OnEnter", function(self)
  2.             print(self.i)
  3.         end)
  Reply With Quote
12-04-16, 12:02 AM   #5
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,322
The iteration variable in a for lop should be isolated between each iteration. I ran the following test as proof.

Lua Code:
  1. local t={};
  2. for i=1,5 do
  3.     t[i]=function() print(i); end
  4. end
  5. for _,f in ipairs(t) do f(); end

This prints the following output.
Code:
1
2
3
4
5


In conclusion, something in your code is contaminating the variable in your for loop. Without being able to look through all of your code, there's no way to tell what's going on exactly.
__________________
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 » Lua/XML Help » creating frames with for 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