multidimension array help-- no dynamic arrays?
Monkey Forums/Monkey Programming/multidimension array help-- no dynamic arrays?
| ||
Class CMinimap
Field map:Int[][]
Field width:Int
Field height:Int
Function Create:CMinimap(w:Int, h:Int)
Local minimap:CMinimap = New CMinimap
Local j:Int, k:Int
minimap.width = w
minimap.height = h
minimap.map = [New Int[w], New Int[h]]
For k =0 To h-1
For j = 0 To w-1
minimap.map[j][k] = 0
Next
Next
Return minimap
End
End
Class Game Extends App
Method OnCreate()
Local minimap:CMinimap = CMinimap.Create(50,40)
End
End
Compiles, but errors on run: array index out of range. I keep banging my head. Any ideas? Something obvious? I've tried putting actual numbers in for j and k. J only allows up to the number 1 before erroring out. K works for any numbers. |
| ||
| See this thread: http://monkeycoder.co.nz/Community/posts.php?topic=822 People keep tripping over this. It might be an idea to add some array utility functions to the standard library. |
| ||
| Thank you. I had done a search, and it came up with the wrong answer (there is a post that is wrong out there). http://www.monkeycoder.co.nz/Community/posts.php?topic=162 My other thought is, why isn't Array.Allocate() not in the core functions? Array.Resize() is. |
| ||
That post isn't wrong, it's just a poorly chosen example as it's easy to confuse the declaration of Global myarray:=[New Int[2],New Int[2]] As being a template for declaring the desired array width and height where the '2's are when they are actually declaring the two rows of length 2 in the 2x2 array. |
| ||
| People keep tripping over this. It might be an idea to add some array utility functions to the standard library. To start with, there should be a bit more explanation about Arrays in the documentation. I find myself going back to this website and enter "array" as a search term in order to find an example on how to create an array, which is not in the docs. |