Thread Tools Display Modes
05-24-12, 03:54 PM   #1
venoabyss
A Deviate Faerie Dragon
Join Date: Apr 2011
Posts: 13
For Loop Issue

Ok so I made up my button template, and I can call it and set each field with no problem, but I'm having issues setting the fields through a for loop.

Code:
GRFrame.rosterSlot..i.level:SetText("35");

or

GRFrame.["rosterSlot"..i].level:SetText("35");
Neither of these are working.

GRFrame is my base frame, it has the following line of code after making my roster button:
Code:
frame.rosterSlot1 = rosterSlot1;
frame is a local variable for my frame.
and my template uses level as the parentkey for a fontstring, and i is my index from my for loop. I know that the following code works:

Code:
	GRFrame.rosterSlot1.level:SetText(03)
Any tips on where I missed the boat would be wonderful
V
  Reply With Quote
05-24-12, 04:34 PM   #2
Ketho
A Pyroguard Emberseer
 
Ketho's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,026
Originally Posted by venoabyss View Post
Code:
GRFrame.["rosterSlot"..i].level:SetText("35")
Maybe try to remove that red dot


http://www.lua.org/manual/5.1/manual.html#2.1
The language supports this representation by providing a.name as syntactic sugar for a["name"]

http://www.lua.org/manual/5.1/manual.html#2.5.7
Code:
t.x = 1            -- t["x"] = 1

Last edited by Ketho : 05-24-12 at 05:29 PM. Reason: I'm an edit freak
  Reply With Quote
05-24-12, 06:18 PM   #3
venoabyss
A Deviate Faerie Dragon
Join Date: Apr 2011
Posts: 13
Originally Posted by Ketho View Post
Maybe try to remove that red dot


http://www.lua.org/manual/5.1/manual.html#2.1



http://www.lua.org/manual/5.1/manual.html#2.5.7
Code:
t.x = 1            -- t["x"] = 1
Unfortunately it didn't work so I set the button objects to be referenced in a table and called them from the table.

Code:
table[1] = rosterSlot1;

table[1].level:SetText(level);
it seems to be working, but I bet its not the best way. any tips would be wonderful.

V
  Reply With Quote
05-24-12, 06:24 PM   #4
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by venoabyss View Post
Code:
table[1] = rosterSlot1;

table[1].level:SetText(level);
it seems to be working, but I bet its not the best way. any tips would be wonderful.
Without seeing your actual, entire code it's basically impossible to tell you what you should be doing differently, but generally you'd want to do something like this:

Code:
for i = 1, 10 do
	GRFrame["rosterSlot" .. i].level:SetText("35")
end
or this:

Code:
for name, object in pairs(GRFrame) do
	if type(object) == "table" and type(object.level) == "table" and object.level.SetText then
		object.level:SetText("35")
	end
end
Again, though, without knowing what your code looks like (especially how the GRFrame table is structured) it's hard to know what's relevant. In the second example above, for instance, there are three checks that may not need to be run, but have to be run in the example because I have no idea what kind of values are stored in the table I'm looping over.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
05-29-12, 11:28 AM   #5
venoabyss
A Deviate Faerie Dragon
Join Date: Apr 2011
Posts: 13
Originally Posted by Phanx View Post
Again, though, without knowing what your code looks like (especially how the GRFrame table is structured) it's hard to know what's relevant. In the second example above, for instance, there are three checks that may not need to be run, but have to be run in the example because I have no idea what kind of values are stored in the table I'm looping over.
GRFrame isn't a table its my frame object I've been setting objects to it.

ie
Code:
 GRFrame.rosterButton = createframe("button", "rosterButton", GRFrame, "UIbuttonTemplate");
(there's more to how I did it but I don't have the code in front of me but basically that's what I was doing.)
the idea was to keep it all clean similar to several posts I've seen of yours I liked how clean it was and easy to read, but I added a table of all the "rosterbuttons" and it seems to be functioning as intended as the table just references the button objects allowing me to call functions etc from it.

I tried the code you included but wasn't able to make it work right for some reason, and I don't have the code from that section anymore since the rewrite... Still love the tips and will try it next time I run across this issue. Ty lots!

Last edited by venoabyss : 05-29-12 at 11:29 AM. Reason: clarification
  Reply With Quote
05-29-12, 12:19 PM   #6
Ketho
A Pyroguard Emberseer
 
Ketho's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,026
Originally Posted by Phanx View Post
Without seeing your actual, entire code it's basically impossible to tell you what you should be doing differently
^ this. show us your actual code
  Reply With Quote
05-29-12, 08:02 PM   #7
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by venoabyss View Post
GRFrame isn't a table its my frame object I've been setting objects to it.
Frame objects in WoW are tables. They just have userdata and a metatable attached, giving them special behavior vs a regular table.

Lua Code:
  1. local aFrame = CreateFrame("Frame") -- creates a new frame object
  2. local aFrameMT = getmetatable(aFrame) -- gets the metatable attached to the frame object
  3.  
  4. local anotherFrame = CreateFrame("Frame")
  5. local anotherFrameMT = getmetatable(anotherFrame)
  6.  
  7. print(aFrameMT == anotherFrameMT) -- prints "true" because all frames share the same metatable, like strings
  8.  
  9. local aTable = {} -- creates a new table object
  10. setmetatable(aTable, aFrameMT) -- attach the frame metatable to the table object

At this point, aFrame and aTable are both tables with exactly the same methods; you can add members to both, and you can call :SetPoint, :CreateTexture, or another frame function on either one.

However, since aTable is just a table, and not a frame, it doesn't have the userdata, and the method calls will return errors because they're trying to manipulate a frame object in C code that doesn't exist.

Aside from frame-specific methods that operate on the userdata attached to a frame, though, frames in WoW are really just nothing but tables.

Originally Posted by venoabyss View Post
I tried the code you included but wasn't able to make it work right for some reason, and I don't have the code from that section anymore since the rewrite...
Again, can't really help you with specific issues without seeing some code. If you need help debugging in the future, please post the code you're working with so we can help you fix it.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » For Loop Issue


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