Thread Tools Display Modes
04-25-09, 09:14 AM   #1
Foxthorn
A Deviate Faerie Dragon
 
Foxthorn's Avatar
Join Date: Apr 2009
Posts: 16
Creating a two dimensional array?

Okay, another problem...I'm trying to use a two dimensional array to store some settings, like this:

Code:
someArray[Name][Value] = 4
But if I just type that into the editbox, I get this:

Code:
[string "someArray["Name"]["Value"] = 4"] line 1:
   attempt to index global 'someArray' (a nil value)
Halp?
  Reply With Quote
04-25-09, 09:29 AM   #2
xConStruct
A Chromatic Dragonspawn
 
xConStruct's Avatar
AddOn Author - Click to view addons
Join Date: May 2008
Posts: 199
You first have to create the array/table, as well as the table in the first dimension:
Code:
someArray = {}
someArray[Name] = {}
__________________
« Website | GitHub »

Oh hai!
  Reply With Quote
04-25-09, 09:45 AM   #3
Foxthorn
A Deviate Faerie Dragon
 
Foxthorn's Avatar
Join Date: Apr 2009
Posts: 16
Ah, that worked. Thanks.

Another question, is there any way to loop through every FontString that's parented to a specific frame, and do something to each one?

Last edited by Foxthorn : 04-25-09 at 10:37 AM.
  Reply With Quote
04-25-09, 12:14 PM   #4
Foxthorn
A Deviate Faerie Dragon
 
Foxthorn's Avatar
Join Date: Apr 2009
Posts: 16
New problem...Still working on that two-dimensional array. Here's some of the code:
Code:
function WXP_Draw(WXP_otherXP,WXP_otherMax,WXP_Name,WXP_Level)
WXP_Old["otherXP"][WXP_Name] = WXP_otherXP
And I get this error:
attempt to index field 'otherXP' (a string value)
Even after I do
Code:
WXP_otherXP = tonumber(WXP_otherXP)
I still get the same error.
WXP_otherXP is a number (76957) and I can perform arithmetic on it (before doing tonumber).

Hmm...if WXP_Old["otherXP"] is a number, will trying to use WXP_Old["otherXP"][WXP_Name] even work? (first it was a number/string, and now you're trying to make it another array thing)

Last edited by Foxthorn : 04-25-09 at 12:18 PM.
  Reply With Quote
04-25-09, 12:25 PM   #5
lilsparky
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Oct 2007
Posts: 117
Originally Posted by Foxthorn View Post
New problem...Still working on that two-dimensional array. Here's some of the code:
Code:
function WXP_Draw(WXP_otherXP,WXP_otherMax,WXP_Name,WXP_Level)
WXP_Old["otherXP"][WXP_Name] = WXP_otherXP
And I get this error:

Even after I do
Code:
WXP_otherXP = tonumber(WXP_otherXP)
I still get the same error.
WXP_otherXP is a number (76957) and I can perform arithmetic on it (before doing tonumber).

Hmm...if WXP_Old["otherXP"] is a number, will trying to use WXP_Old["otherXP"][WXP_Name] even work? (first it was a number/string, and now you're trying to make it another array thing)
how are you defining WXP_Old? is there a "otherXP" index that contains a table? that is, did you ever do a "WXP_Old["otherXP"] = {}" or similar?

also, WXP_Old["otherXP"] can be written as WXP_Old.otherXP which might be a little easier on the eyes. written this way, it's also clear that it's not really a multidemensional array like it is in C so much as a table (WXP_Old) that contains another table (otherXP).
  Reply With Quote
04-25-09, 12:26 PM   #6
xConStruct
A Chromatic Dragonspawn
 
xConStruct's Avatar
AddOn Author - Click to view addons
Join Date: May 2008
Posts: 199
Originally Posted by Foxthorn View Post
Another question, is there any way to loop through every FontString that's parented to a specific frame, and do something to each one?
You could fetch all regions of the frame with :GetRegions() and check if they're font strings.
Code:
local regions = { frame:GetRegions() }
for _, region in pairs(regions) do
  if(region:IsObjectType("FontString")) then
    doSomething()
  end
end
__________________
« Website | GitHub »

Oh hai!
  Reply With Quote
04-25-09, 01:45 PM   #7
Foxthorn
A Deviate Faerie Dragon
 
Foxthorn's Avatar
Join Date: Apr 2009
Posts: 16
Originally Posted by lilsparky View Post
how are you defining WXP_Old? is there a "otherXP" index that contains a table? that is, did you ever do a "WXP_Old["otherXP"] = {}" or similar?

also, WXP_Old["otherXP"] can be written as WXP_Old.otherXP which might be a little easier on the eyes. written this way, it's also clear that it's not really a multidemensional array like it is in C so much as a table (WXP_Old) that contains another table (otherXP).
Yeah, originally it was basically if WXP_Old["otherXP"] = nil, set it to 0.

So I changed it to set it to {} instead of 0. I have to restart my game and clear out the old SavedVariable to fix it since it's still saving it as 0, but I'm questing with a friend so I'll have to do it later.

Originally Posted by Cargor View Post
You could fetch all regions of the frame with :GetRegions() and check if they're font strings.
Code:
local regions = { frame:GetRegions() }
for _, region in pairs(regions) do
  if(region:IsObjectType("FontString")) then
    doSomething()
  end
end
Thanks! I'll use that instead of the longer loop-through-party-and-use-each-member's-name method I'm using now

Last edited by Foxthorn : 04-25-09 at 01:48 PM.
  Reply With Quote
04-25-09, 07:26 PM   #8
Foxthorn
A Deviate Faerie Dragon
 
Foxthorn's Avatar
Join Date: Apr 2009
Posts: 16
Yet another problem...here's the code:

Code:
_G["WXP_Text_"..WXP_Name]:SetText(WXP_TextString..WXP_Name.."("..WXP_Level..")")
And here's the resulting error...

attempt to index field '?' (a nil value)
WXP_TextString, WXP_Name, and WXP_Level are all strings, and I think WXL_Text_(WXP_Name) does exist at that point, it may not though. If it didn't, is that what would be causing this error? And if not, then what is?

Note: When you first log in to a character, you get this error, and the FontString that this modifies doesn't exist. However, when you do a ReloadUI, you don't get the error, the FontString appears, and all works fine. So obviously there's something that's not happening when you first log in, but does happen after a ReloadUI.

If someone would be willing to take a look at the full source, say so and I can get it to you, it'd help a lot if I could get this sorted out.

Last edited by Foxthorn : 04-25-09 at 08:20 PM.
  Reply With Quote
04-30-09, 09:31 PM   #9
tsadok
An Aku'mai Servant
 
tsadok's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2007
Posts: 32
Originally Posted by Foxthorn View Post
is that what would be causing this error?
The font string does not exist yet, so the global you're looking for ("WXP_Text_"..WXP_Name) is nil. To perform a method (SetText()), Lua expects the object to be a table with all its methods and other stuff. So it tries to do a table lookup for ["SetText"] but there isn't a table yet.

It may only be happening on login because the fontstring is waiting on certain events to be created which could take longer during login than during a /reloadui.

Hope this makes sense...
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Creating a two dimensional array?


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