Something like Dynamic List ?
Monkey Forums/Monkey Programming/Something like Dynamic List ?
| ||
Hello :) I write my first game in monkey and stuck on a problem ... i need to create a custom Type/Structure like this.... : Class Character Field X:Int Field Y:Int end class Now i would to create a list of characters and access this : Global Chars:List<Character> How i now add Items to this list... is this possible ? like this ... T:Charater = New Character Character.X=10 Character.Y=20 Chars.AddLast T Is this possible anyway ? |
| ||
Sure. First you have to create that new list (because you only have the declaration done but no instance of that list object yet!):Chars = New List<Character> Then you can create your characters and add them: Local c:Character = New Character() c.X = 10 c.Y = 20 Chars.AddLast( c ) |
| ||
Wow! Thanks :D I Love Monkey ;P Really Powerfull ! Can i Place a method in the Class for Scanning for the highest ? like this ... Class Character Field X:Int Field Y:Int Method FindHighest() ' ... Here Scans the List for the Highest X or something... end method end class what i mean is ... can i crawl the list from out the class self ? thats where very nice :D |
| ||
Use EachIn to iterate through the list, and return the highest value found once you've gone through the whole list. |
| ||
what i mean is ... can i crawl the list from out the class self ? Yes: Class CharacterList Extends List<Character> Method FindHighest:Character() For Local Ch:Character = EachIn Self '''do comparison etc here, store the highest Next ''' return the result of the find operation End End That's how object oriented works. Need a list that has to do something special? Get the list class, and extend it with the functionality you need Now: Global Chars:CharacterList Chars = New CharacterList |
| ||
uhhh great thanks .... i seee i have a much to learn in monkey =) ( coming from purebasic :-/ ) Wow and i see ... OOP is really very powerfull ;D |
| ||
:( Dont unterstand ... Class My Field Y:Int Method New(y:Int) Y=y End End Class xMy Extends List<My> Public Method PrintMe:My() Print Self.Y End Method End Class 'Global A:List<My>=New List<My> Global A:xMy=New xMy Global c:My Function Main() A.AddLast(New My(22)) A.AddLast(New My(33)) A.AddLast(New My(13)) For c = Eachin A c.PrintMe() Next End whats wrong ? :-/ |
| ||
I can easily give you the answer but I rather make you think about it. does class "My" have a method PrintMe? doe the class "xMy" have a "Y" field? in your case "List<My>" allows you to store " abjects of "My" to "xMy" class(extended List). in order to access the objects from the "xMy"(extended List) in your case you use the For/EachIn so the objects to access are of type "My" that you now placing in "c" but "My" does not have a method "PrintMe" so why are you trying to access "PrintMe" from a class that does not contain the method "PrintMe"? class "xMy" does not have a "Y" field so when you use "Self.Y" you are saying to access the variable(Field) Y from the class "xMy" but xMy does not contain a variable(Field) Y. Think it through and see if you can figure it out. also public and private are not part of methods or functions they just separate public code from private code. you can use one Public for all of your Public methods and functions with in a class and one Private for all of your private methods and functions with in a class. I sugest it's best not put it in the same line with a method. |
| ||
Darky, I think this is what you're trying to do: [monkeycode]Class My Field Y:Int Method New(y:Int) Y=y End Method PrintMe() Print Self.Y End End Global A:List<My> = New List<My> Function Main() A.AddLast(New My(22)) A.AddLast(New My(33)) A.AddLast(New My(13)) For c := Eachin A c.PrintMe() Next End[/monkeycode] You don't need to extend List in this situation because you're adding functionality to My, not List. Also notice the colon I added in the For statement. This tells Monkey that you want the variable c to be declared as whatever type of object is in A (in this case, My). @Jesse: ...you can use one Public for all of your Public methods and functions with in a class and one Private for all of your private methods and functions with in a class... You can use multiple. I normally do that to separate fields and methods. [monkeycode]Class Foo Private ' private fields Public ' public fields Private ' private methods Public ' properties ' constructors ' public methods End[/monkeycode] |