Variable size arrays as a field

BlitzMax Forums/BlitzMax Programming/Variable size arrays as a field

If I have a field in a type that is a multidimensional array, how do I define and resize it?

Field grid:TMyType[n,n]

I want to be able to resize the grid[] array depending on the resolution I want. I want an array, not a list, so I can look up the values by xy position.

Method Resize(w,h)

	Local temp:TMyType[w,h]
	For Local x:Int = 0 Until w
		For Local y:Int = 0 Until h
			If grid[x,y]
				temp[x,y] = grid[x,y]
			Else
				temp[x,y] = New TMyType
			EndIf
		Next
	Next
	grid = temp
	
EndMethod


You can make an instance of a type and pass parameters to it all in one command?

Nope. Fredbords code is just constructing the new array.

To copy over the old stuff you'd have to create a new array using Fredborgs code, and then loop through the old array and assign the old values to the new array.

You can make an instance of a type and pass parameters to it all in one command?

Yep, but thats not what Fredbords code is doing

Global Bob:Atype = New Atype.Set(12,23)
Where set is a method of Atype

If you mean how do you define a multi dimensional array pointer, the answer is that

Field Blah[,]

Is analogous to the single dim version:

Field Blah[]

If you mean how do you get slices to work (for copying and resizing), as in the multi dim version of:

Local Nums[] = [ 1, 2, 3, 4 ]
Nums = Nums[..24] 'Resize to 24 elements

Then the answer is you can't, you have to roll your own, Fredborg's got the goods.

yeah I got stuck on this ago, assumed you could slice multidimensional arrays but alas you can't.

Well to make the array start at whatever size you want you only have to do this:

Field grid:TMyType[,]

thing.grid = new TMyType[n,n]

But of course if you want to change the size later you lost its data.

I've got a solution. Ever seen anything like this? Works for me :)

local array[][]

function resizearray2d(sizex,sizey,array[][] var)
   array=array[..sizex]
   for i=0 to sizex-1
      array[i]=array[i][..sizey]
   next
end function


Clever, eh?

How do I do this?:
Type foo
Field grid[,]
EndType

f:foo=New foo
foo.grid[]=grid[..22,..22]


Like this?
Type foo
Field grid[,]
Method Resize(w,h)

	Local temp:Int[w , h]
	If grid
		For Local x:Int = 0 Until w
			For Local y:Int = 0 Until h
				Print x + " " + y
				If grid[x,y]<>0
					temp[x,y] = grid[x,y]
				Else
					temp[x,y] = Null
				EndIf
			Next
		Next
	Else
		grid:Int=New Int[22,22]
	EndIf
EndMethod

EndType

f:foo = New foo
Print Len(f.grid)
f.resize(22 , 22)
Print Len(f.grid)