About Monkey 2 › Forums › Monkey 2 Programming Help › Updating Class List in Class itself
This topic contains 9 replies, has 4 voices, and was last updated by
Pakz 1 year, 9 months ago.
-
AuthorPosts
-
June 20, 2017 at 7:49 pm #8832
I created a class called unit which I store in a list. I tried to create a method in this class to update all the units inside this class but I got an error calling this method.
I am not sure how to do something like this. Is it possible if the class is in a list? Do I need to use a stack for this and call the first item from the stack to run this method that updates all?
June 20, 2017 at 11:24 pm #8833I am not sure what you mean. Are you extending the list class with your own method or are you using it some other way?
if you declare the list Global and initialize it only once, it should be possible.June 21, 2017 at 12:49 am #8835Here is a example. I want to have methods in the class that update everything. Here below it does not seem to work. I can call the method that should update all the things in the list but it only updates 1.
Monkey1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950#Import "<std>"#Import "<mojo>"Using std..Using mojo..Class unitField x:Int,y:Intmethod New(mx:Int,my:Int)Self.x = mxSelf.y = myEnd MethodMethod moveallright()For Local i:=Eachin myuniti.x+=Rnd(2)NextEnd MethodMethod drawall(canvas:Canvas)canvas.Color = Color.WhiteFor Local i:=Eachin myunitcanvas.DrawCircle(x,y,50)NextEnd MethodEnd ClassGlobal myunit:List<unit> = New List<unit>Class MyWindow Extends WindowMethod New()myunit.AddFirst(New unit(10,10))myunit.AddFirst(New unit(110,110))End MethodMethod OnRender( canvas:Canvas ) OverrideApp.RequestRender() ' Activate this methodmyunit.First.moveallright()myunit.First.drawall(canvas)' if key escape then quitIf Keyboard.KeyReleased(Key.Escape) Then App.Terminate()End MethodEnd ClassFunction Main()New AppInstanceNew MyWindowApp.Run()End FunctionJune 21, 2017 at 1:19 am #8836I’m not a OOP master, but I don’t think you want to do it the way you’re doing it. Looks kinda fishy.
But the issue with your code is it keeps drawing the first circle and none of the others. Change the line to this so it references the objects in the loop:
Monkey1canvas.DrawCircle(i.x,i.y,50)June 21, 2017 at 1:41 am #8837As papgar said and that’s how it’s supposed to be.
also the method moveallright all and drawall should be functions that way there won’t be any confusion on its use. this way you won’t need to use miunit.first.etc.. all you need to do is use unit.moveallright() and unit.drawall()June 21, 2017 at 1:42 am #8838Ohh, I did not notice that
Seems to work now.
June 21, 2017 at 1:43 am #8839I am still a little bit confused on functions and methods. The difference between them.
June 21, 2017 at 1:49 am #8840treat a function as you would use any independent function. with the exception that if the function is inside a class you can only call it by the class instantiation or by using the class name.functionname(). With method you can only call it if it has been instantiated. and only through it’s instance.
June 21, 2017 at 6:47 am #8847As a sidenote you could do something like this to make the codebase a bit more modular.
Monkey1234567891011121314151617181920Class UnitMethod Do()EndEndClass UnitCollection AbstractGlobal Units:List<Unit> = New List<Unit>Function Init()'...'EndFunction Update()For Local unit := Eachin Unitsunit.DoNextEndEnd' usage:UnitCollection.Init()UnitCollection.Update()June 25, 2017 at 2:18 am #8895That look as how I usually use classes. Only what is the Unitcollection for?
btw – I just discovered a thing with functions and methods that took me a while to fix. I got a error when I had a function inside a class trying to use a list inside the same class. This is not possible. You need to make it a method. Methods have access to the data fields in the class it appears.
-
AuthorPosts
You must be logged in to reply to this topic.