Arrays..

Monkey Forums/Monkey Programming/Arrays..

Paul - Taiphoz(Posted 2012) [#1]
how does monkey handle them , assuming I have or want a 2D array, to hold the tiles of a level.

Board[x,y]

some one point me in the right direction please, and thanks.


Paul - Taiphoz(Posted 2012) [#2]
nv, found it in the docs.


Paul - Taiphoz(Posted 2012) [#3]
ok
Field grid:Int[5][100] ' 5 tiles wide, and 100 tiles high.
gives me an error, do I need to declare the array as [][] first and then fill it ? cant we set it's size initially ?


ziggy(Posted 2012) [#4]
Monkey does not have multidimensional arrays. You have to create an array of arrays, wich is basically the same:

Board[x][y]

That's a brief example:
Function Main()
	'Declare the array of arrays:
	Local a:MyClass[][] 

	'Init the array of arrays:
	a = New MyClass[4][]
	
	'Init each array in the array of arrays:
	For Local i:Int = 0 until a.Length()
		a[i] = New MyClass[8]
	Next
	
	'Now we can init each item:
	For Local x:Int=0 until a.Length()
		For Local y:Int=0 until a[x].Length
			a[x][y] = New MyClass
		Next
	Next
End

Class MyClass
	Method New()
		Print "Item created!"
	End
End



Paul - Taiphoz(Posted 2012) [#5]
Screw all that faffing about im just doing this Array[x*y] saves all the faffing about.


Gerry Quinn(Posted 2012) [#6]
I was going to suggest that for the purpose of maps in a dungeon game or something, a single-dimensional array is probably best, but you seem to have decided on that anyway.


ziggy(Posted 2012) [#7]
Ok course, but as you asked how a multidim array was suposed to be implemented on Monkey....