Variable size arrays as a field

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

JoshK(Posted 2007) [#1]
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.


fredborg(Posted 2007) [#2]
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



ImaginaryHuman(Posted 2007) [#3]
You can make an instance of a type and pass parameters to it all in one command?


SculptureOfSoul(Posted 2007) [#4]
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.


H&K(Posted 2007) [#5]
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


Michael Reitzenstein(Posted 2007) [#6]
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.


Grey Alien(Posted 2007) [#7]
yeah I got stuck on this ago, assumed you could slice multidimensional arrays but alas you can't.


Curtastic(Posted 2007) [#8]
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.


ninjarat(Posted 2007) [#9]
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?


JoshK(Posted 2007) [#10]
How do I do this?:
Type foo
Field grid[,]
EndType

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



tonyg(Posted 2007) [#11]
Like this?