Page 1 of 1

table dimensioning...

Posted: Sat Apr 11, 2026 9:34 pm
by phipslk
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?

Re: table dimensioning...

Posted: Sat Apr 11, 2026 10:43 pm
by plouf
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

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))

Re: table dimensioning...

Posted: Sun Apr 12, 2026 7:36 am
by Bugala
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:

Code: Select all

Player = {}
Player[1] = {}
Player[1].Row = {}
Player[1].Row[1] = {x = 100, y=199}
Now you can use:

Code: Select all

Player[1].Row[1].x = 200
Player[1].Row[1].y = 300
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)

Code: 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
next
Now you would have players 1-4, and each of them contains rows 1-10, with X and Y inside that table

Re: table dimensioning...

Posted: Sun Apr 12, 2026 2:40 pm
by phipslk
cool, thank you both! I think I got it now.