Arrays..
Monkey Forums/Monkey Programming/Arrays..
| ||
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. |
| ||
nv, found it in the docs. |
| ||
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 ? |
| ||
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 |
| ||
Screw all that faffing about im just doing this Array[x*y] saves all the faffing about. |
| ||
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. |
| ||
Ok course, but as you asked how a multidim array was suposed to be implemented on Monkey.... |