I want to initialize a table that looks like this:
player[number].row[number].x=100
player[number].row[number].y=199
but I get errors, if I do
player={}
and then add values like
player[1].row[2].x=100
anyone who can point me to my error? better to use dim? but how?
table dimensioning...
Re: table dimensioning...
thats because you can have table inside table (multidimensional tables) but tables are anonymous, only pointing by its position
check this, simulates what you want creating 3 player with 3 "rows" and x and y to each "row"
just keep in mind that "row" is the second dimension
check this, simulates what you want creating 3 player with 3 "rows" and x and y to each "row"
just keep in mind that "row" is the second dimension
Code: Select all
Dim player[2]
For i=0 To 2
player[i]= {{x=i+10,y=i+20} , {x=i+30,y=i+40} , {x=i+50,y=i+60}}
Next
DebugPrint(player[0][0].y)
DebugPrint(player[0][0].x)
DebugPrint("Now see entire table how it looks")
DebugPrint(SerializeTable(player))
Christos
Re: table dimensioning...
Plouf explained correctly, but might be easier to understand it this way.
The point here is that you can't just make Table = {} and then use something like Table.x{1].sub[2], because you have not made any tables beyond "table", as in Table.x doesn't exist yet, you have to first do it using Table.x = {}
However, if you start by directly doing Table.x = {}, without doing table = {} first, then it would fail again, since Table wouldn't exist yet.
To show the easier-to-understand way how your case would work is to do:
Now you can use:
But do realize that Player[2] or player[3] doesn't exist yet, neither do Player[1].Row[2], or Player[1].Row[3] exist yet, you have to make them into existence too, which you could do, for example, in the following way (easier to understand way perhaps)
Now you would have players 1-4, and each of them contains rows 1-10, with X and Y inside that table
The point here is that you can't just make Table = {} and then use something like Table.x{1].sub[2], because you have not made any tables beyond "table", as in Table.x doesn't exist yet, you have to first do it using Table.x = {}
However, if you start by directly doing Table.x = {}, without doing table = {} first, then it would fail again, since Table wouldn't exist yet.
To show the easier-to-understand way how your case would work is to do:
Code: Select all
Player = {}
Player[1] = {}
Player[1].Row = {}
Player[1].Row[1] = {x = 100, y=199}
Code: Select all
Player[1].Row[1].x = 200
Player[1].Row[1].y = 300Code: Select all
Player = {}
for p = 1 to 4
Player[p] = {}
Player[p].Row = {}
for r = 1 to 10
Player[p].Row[r] = {X = 1, Y = 2}
next
nextRe: table dimensioning...
cool, thank you both! I think I got it now.