WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   For Loop Issue (https://www.wowinterface.com/forums/showthread.php?t=43469)

venoabyss 05-24-12 03:54 PM

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

Ketho 05-24-12 04:34 PM

Quote:

Originally Posted by venoabyss (Post 256502)
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
Quote:

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

venoabyss 05-24-12 06:18 PM

Quote:

Originally Posted by Ketho (Post 256504)
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

Phanx 05-24-12 06:24 PM

Quote:

Originally Posted by venoabyss (Post 256507)
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.

venoabyss 05-29-12 11:28 AM

Quote:

Originally Posted by Phanx (Post 256509)
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!

Ketho 05-29-12 12:19 PM

Quote:

Originally Posted by Phanx (Post 256509)
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

Phanx 05-29-12 08:02 PM

Quote:

Originally Posted by venoabyss (Post 256561)
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.

Quote:

Originally Posted by venoabyss (Post 256561)
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.


All times are GMT -6. The time now is 05:22 PM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI