Multi dimensional arrays

BlitzMax Forums/BlitzMax Programming/Multi dimensional arrays

Ash_UK(Posted 2006) [#1]
Hi people. I need to know how to define an array within a type, using a function (within the type) which takes the arrays dimensions which are passed to it.


Type gameBoard

	Global x,y,z,width,height,trans;
	Global layer;
	Global data[1,1];  <-----THIS IS THE ARRAY I WOULD LIKE TO SPECIFY THE SIZE OF FROM CALLING A FUNCTION

	Global cellWidth = 10;
	Global cellHeight = 10;
	
		
	Function createBoard:gameBoard(getWidth,getHeight)
		layer:+1;
		width = getWidth;
		height = getHeight;
		data=data[getWidth,getHeight]	
		'use slice to resize array
		Return New gameBoard;
	End Function
	
	Method drawBoard()
		'For Local repx=EachIn data
		For repx = 0 To (width-1)
			For repy = 0 To (height-1)
				xpixel = repx*cellWidth;
				ypixel = repy*cellHeight;
				data[repx,repy] = 1;
				If data[repx,repy] = 1 Then DrawOval(x+xpixel, y+ypixel, cellWidth, cellHeight);
			Next
		Next
	End Method
	
End Type




I would like to know how to accomplish this without using lists :)

Thankyou for any help


H&K(Posted 2006) [#2]
field data:Int [,]     'Or Global
...
...
gameboard:data = new Int [getWidth,getHeight]
or
Field data:Int [][]    'Or Global
...
...
gameboard:data = New Int [getWidth][GetHeight]
(But wait till someone who knows what they are talking about posts ;)

Edit: Ash, arent you normaly a Blitz3d poster?


Perturbatio(Posted 2006) [#3]


You can't resize a multi-dimensional array with slices, but you can use an array of arrays for this. (a 1D array of 1D arrays).


Perturbatio(Posted 2006) [#4]


Or you can re-assign the array via a temp one when creating.


Ash_UK(Posted 2006) [#5]
Thanks Perturbatio :o) But H&K's method worked fine. Thankyou H&K and thank you Perturbatio for your help. I really do appriciate it :o)


Perturbatio(Posted 2006) [#6]
Thanks Perturbatio :o) But H&K's method worked fine. Thankyou H&K and thank you Perturbatio for your help. I really do appriciate it :o)


Yeah, I really should refresh the page before replying to threads :)