How to set up many classes?

Monkey Forums/Monkey Beginners/How to set up many classes?

Pakz(Posted 2015) [#1]
I was reading a book(ai game programming wisdom 2) about map generation for creating islands. It basically worked by creating points that grew on the map and stopped if they got within two tiles of another island.

I tried to create lists of classes (one list for a island) using a array but it did not work in the ways I tried. Is there anyone who knows how to create a variable amount of lists of classes?


MikeHart(Posted 2015) [#2]
You ment a list that stores several lists? List are objects too. So you can store them like any other object.


Pakz(Posted 2015) [#3]
This is what I tried to do. I created a array and tried to create a list in one dimension on it. The compiler says that it expects a declaration.

Import mojo

Class test
	Field x:Int,y:Int
	Method New(x:Int,y:Int)
		Self.x = x
		Self.y = y
	End Method
End Class

'this is what does not work.
Global a:Int[] = New Int[10]

a[0]:List<test> = New List<test>

Class MyGame Extends App

    Method OnCreate()
        SetUpdateRate(60)
    End Method
    Method OnUpdate()        
    End Method
    Method OnRender()
        Cls 0,0,0 
        SetColor 255,255,255
    End Method
End Class


Function Main()
    New MyGame()
End Function


This is what I always used. One variable per list. But I want to put the Lists in a array or something like that.
Global a:List<test> = New List<test>
Global b:List<test> = New List<test>


I am not that experienced with Objects yet. I am learning about that since I started with Monkey a few months ago.


MikeHart(Posted 2015) [#4]
You tried to run code outside metods or functions. Anyway, I find storing lists in arrays weird but try this:




Pakz(Posted 2015) [#5]
Yeah, thats it. Thanks!


Gerry Quinn(Posted 2015) [#6]
MikeHart has answered your question, but I'll just note that if you want list-flexibility everywhere you can also have things like:

Global a:List< List< test > > = New List< List< test > >()
Local island:List< test > = New List< test >()
a.AddLast( island )

Of course, you may want a fixed number of islands, and the array makes it slightly easier to check for the last island so far if you are comparing it to previous ones - so there's nothing wrong with using an array.

Another thing you might like to do is use List.ToArray() on each island after your islands are made and store them in a new array, as in your game it will probably be more efficient to store each island as an array once it's done.

Edit: Left out brackets on New's...


Pakz(Posted 2015) [#7]
@gerry

Interesting lines of code. I wil experiment with it and see if I can use it for something. Monkey can get pretty complicated with all those Possibilities.

I did read something on this forum not so long ago About the k.i.s.s principle. (Keep it simple stupid) I wil try to keep that in the back of my mind when figuring out how to do things.

Great forum people here btw, always helping out! I think I spend my money on monkey pro right :)