Posted: 26th Jul 2022 14:41
I swear once a year I have to ask another dumb question about AppGameKit arrays. And forum search doesn't seem to find anything older than 2019 now.

I'm trying to set the dimension of a 2D array from within a function. When I display the array size it reports the correct length, yet when I try to access an index it tells me the array length is empty.

+ Code Snippet
Type DataTable
	x as float
	y as float
	width as float
	height as float
	cols as integer
	rows as integer
	data as String[-1,-1]
EndType



mytable as DataTable
mytable = createTable(400,300,3,7)

//mytable.data[1,2] = "asdfasdf"

do
   
    Print(mytable.data.length)
    Print(mytable.data[0].length)
    
   // print(mytable.data[1,2])
    
    Sync()
loop



function createTable(width, height, rows, cols) 
	t as DataTable
	
	t.width  = width
	t.height = height
	t.rows   = rows
	t.cols   = cols
	t.data.length = rows
	t.data[0].length = cols
	
	
	
	for i = 1 to rows-1
		for j = 1 to cols-1
			//t.data[i,j] = createText("")
		next j
	next i
endfunction t
Posted: 26th Jul 2022 14:59
You need to set the length for each row
+ Code Snippet
function createTable(width, height, rows, cols) 
    t as DataTable
     
    t.width  = width
    t.height = height
    t.rows   = rows
    t.cols   = cols
    t.data.length = rows

    for i = 1 to rows-1
       t.data[i].length = cols
        for j = 1 to cols-1
            //t.data[i,j] = createText("")
        next j
    next i
endfunction t


I would advise you use bucket zero as well. For me starting from1 always caused me grief