Creating 2d array

Monkey Forums/Monkey Beginners/Creating 2d array

Xyle(Posted 2014) [#1]
In java I was able to do this...

		mLayer1 = new int[][]{
			{7,7,0,7,7,0,0,0,0,0,7,0,7,7,7,7,7,7,0,7,7,7,7,7,7,7,7,7,7,7},
			{0,0,0,0,0,0,0,0,0,0,7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7},
			{0,0,7,7,7,7,7,7,7,0,7,0,0,0,0,0,7,7,0,0,0,0,0,0,7,0,0,7,0,7},
			{0,0,7,0,0,0,0,7,7,0,7,0,0,0,0,0,7,7,0,0,0,7,0,0,0,0,0,7,0,7},
			{0,0,7,0,0,0,0,7,7,0,7,0,0,0,0,0,7,7,0,0,0,7,0,0,0,0,0,7,0,7},
			{0,0,7,0,0,0,0,7,0,0,0,0,0,0,0,0,7,7,0,0,0,7,0,0,7,0,0,7,0,7},
			{0,0,7,0,0,0,0,7,7,0,7,0,0,0,0,0,0,0,0,0,0,7,0,0,0,0,0,7,0,7},
			{0,0,7,0,7,0,0,7,7,0,7,0,0,0,0,0,0,0,0,0,0,7,0,0,0,0,0,7,0,7},
			{0,0,7,7,7,7,7,7,7,0,7,0,0,0,0,0,0,0,0,0,0,7,0,0,7,0,0,0,0,7},
			{0,0,0,7,7,0,0,0,0,0,7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7},
			{7,7,7,7,7,7,7,7,7,7,7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7},
			{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
			{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7},
			{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,7,0,0,7},
			{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,7,7,0,7},
			{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,7,0,7},
			{0,0,0,0,0,7,7,7,7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7},
			{0,0,0,0,0,7,7,7,7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7},
			{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
			{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,7,7,7,0,0,0,0,0,0},
			{0,7,7,7,7,7,7,7,7,7,0,0,0,0,0,0,0,0,0,0,7,7,7,7,0,0,0,0,0,0},
			{0,7,0,0,0,0,0,0,7,7,0,0,0,0,0,0,0,0,0,0,7,7,7,7,0,0,0,0,0,0},
			{0,7,0,0,0,0,0,0,7,7,0,0,0,0,0,0,0,0,0,0,7,7,7,7,0,0,0,0,0,0},
			{0,7,0,0,0,0,0,0,7,7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
			{0,7,0,0,0,0,0,0,7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
			{0,7,0,0,0,0,0,0,7,7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,7,7,7,0},
			{0,7,0,0,7,0,0,0,7,7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,7,7,7,0},
			{0,7,0,0,7,0,0,0,7,7,0,0,0,0,0,0,0,0,0,0,7,7,7,7,0,7,7,7,7,0},
			{0,7,7,7,7,7,7,7,7,7,0,0,0,0,0,0,0,0,0,0,7,7,7,7,0,7,7,7,7,0},
			{0,0,0,0,0,0,0,0,7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
		};


Which is great for creating tile maps, but with MonkeyX I don't see where this kind of declaration is available. I just wanted to make sure before I re-figured my map loading function.

Thanks for any help!


GW_(Posted 2014) [#2]
Monkey doesn't really support this method of static arrays.
You have 3 basic solutions.
1) define the array as 1d and just calculate the offsets.
2) You load a csv file, split each line and fill the array at runtime.
3) You write some other script (in java?) to spit out the array as a list of assignment statements in monkey syntax. i.e:
Function Loadlayer:void()
 mlayer[0][0]=7
 mlayer[0][1]=7
etc... 

Then just import this loading function into your project and call it at runtime.
I've done this for the maps in my game and it works fine. Although be warned that java has a limit of [i think] 65k statements per function.

I'm not quite sure why monkey doesn't properly support defining multi-dimensional arrays. If you just define a 2d array, the inner arrays will not be allocated.
The best solution I have found is a generic multi dim array function like:
Class tArray<T>
	Function Create2dArray:T[][](rows:Int, cols:Int)
		Local a:T[][] = New T[rows][]
		For Local i:Int = 0 Until rows
			a[i] = New T[cols]
		End
		Return a
	End
end
'// call with myarr[][] = tArray.Create2dArray(x,y)





OK, Ignore everything I just wrote. I need sleep..
See Danilo's answer.



Danilo(Posted 2014) [#3]
Replace { } with [ ]
Function Main:Int()
    Local mLayer1:Int[][] = [   [7,7,0,7,7,0,0,0,0,0,7,0,7,7,7,7,7,7,0,7,7,7,7,7,7,7,7,7,7,7],
                                [0,0,0,0,0,0,0,0,0,0,7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7],
                                [0,0,7,7,7,7,7,7,7,0,7,0,0,0,0,0,7,7,0,0,0,0,0,0,7,0,0,7,0,7],
                                [0,0,7,0,0,0,0,7,7,0,7,0,0,0,0,0,7,7,0,0,0,7,0,0,0,0,0,7,0,7],
                                [0,0,7,0,0,0,0,7,7,0,7,0,0,0,0,0,7,7,0,0,0,7,0,0,0,0,0,7,0,7],
                                [0,0,7,0,0,0,0,7,0,0,0,0,0,0,0,0,7,7,0,0,0,7,0,0,7,0,0,7,0,7],
                                [0,0,7,0,0,0,0,7,7,0,7,0,0,0,0,0,0,0,0,0,0,7,0,0,0,0,0,7,0,7],
                                [0,0,7,0,7,0,0,7,7,0,7,0,0,0,0,0,0,0,0,0,0,7,0,0,0,0,0,7,0,7],
                                [0,0,7,7,7,7,7,7,7,0,7,0,0,0,0,0,0,0,0,0,0,7,0,0,7,0,0,0,0,7],
                                [0,0,0,7,7,0,0,0,0,0,7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7],
                                [7,7,7,7,7,7,7,7,7,7,7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7],
                                [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                                [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7],
                                [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,7,0,0,7],
                                [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,7,7,0,7],
                                [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,7,0,7],
                                [0,0,0,0,0,7,7,7,7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7],
                                [0,0,0,0,0,7,7,7,7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7],
                                [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                                [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,7,7,7,0,0,0,0,0,0],
                                [0,7,7,7,7,7,7,7,7,7,0,0,0,0,0,0,0,0,0,0,7,7,7,7,0,0,0,0,0,0],
                                [0,7,0,0,0,0,0,0,7,7,0,0,0,0,0,0,0,0,0,0,7,7,7,7,0,0,0,0,0,0],
                                [0,7,0,0,0,0,0,0,7,7,0,0,0,0,0,0,0,0,0,0,7,7,7,7,0,0,0,0,0,0],
                                [0,7,0,0,0,0,0,0,7,7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                                [0,7,0,0,0,0,0,0,7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                                [0,7,0,0,0,0,0,0,7,7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,7,7,7,0],
                                [0,7,0,0,7,0,0,0,7,7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,7,7,7,0],
                                [0,7,0,0,7,0,0,0,7,7,0,0,0,0,0,0,0,0,0,0,7,7,7,7,0,7,7,7,7,0],
                                [0,7,7,7,7,7,7,7,7,7,0,0,0,0,0,0,0,0,0,0,7,7,7,7,0,7,7,7,7,0],
                                [0,0,0,0,0,0,0,0,7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] ]


    Print mLayer1[2][2]
    Return 0
End



Xyle(Posted 2014) [#4]
Awesome!!

Thanks for the help on that, I spent hours scouring the forums and couldn't find anything on that. I did quite a bit of trial and error, thought I did Danilo's solution, but must have had some typos.

I was getting ready to implement GW_'s advice since the majority of the forums said the same thing.

I really appreciate the help and the info on this!!

You guys rock!!! MonkeyX rocks!!!


navyRod(Posted 2014) [#5]
just to confirm my understanding of this -- I wrote a simple example with prints
not sure if this is the best / most efficient but it seems to work. correct ?

also checking to see if I add my code example below correctly

Strict 

Function Main:Int()
    Local i:Int,j:int
    Local str:string

	Local my2D:Int [][] = [[0,1,2,3],
	                       [4,5,6,7],
	                       [8,9,10,11],
	                       [12,13,14,15]]
	                            
	For  i = 0 To  3
	  For  j = 0 TO 3
	     str = "Array "
	     str += i + ":" + j + " = "
	     str += my2D[i][j]
             print str
	   Next
        Next
  	
   Return 0
End




Xyle(Posted 2014) [#6]
That definitely looks good to me!

I did the same thing. I have a map 30x30 tiles and I use layers. So I printed out each item in the double array to make sure it was all correct.

After spending a long time looking through these forums and a lot of trial and error, Danilo's answer was the first time I've seen the ability to declare a double array like that in MonkeyX.

I appreciate all the help!!

:)


Yoda(Posted 2014) [#7]
I created a map-editor in my game and when pressing a certain key, it outputs (prints) me the code that I copy and paste in my monkeycode.
I basically use a long string that I access and manipulate. It's quite easy and keeps the code short.


ziggy(Posted 2014) [#8]
This works very well too: https://code.google.com/p/lemongames/source/browse/collections/twodimarray.monkey It's an OO version of a 2 dim array that is not an array of arrays.


Gerry Quinn(Posted 2014) [#9]
If you don't care about efficiency and you want to put maps in code, strings are nice:

Local str:String[10] = New String[ 10 ]
str[ 0 ] = "....8888.."
str [ 1 ] = "...88..88."
' etc.

I mean you can make maps that are easy to see visually, and convert them to whatever data structure you want.


Samah(Posted 2014) [#10]
I miss DefData. :(