How do I do it in MX2?

Community Forums/Monkey2 Talk/How do I do it in MX2?

Richard Betson(Posted 2016) [#1]
Hi,

I thought I would start a thread to help me and others converting code from MX1 to MX2. If you can help please do. I'm sure I'll have more questions like these and if I can't find it searching the forums, reading the doc's and so on I'll ask here and hope someone comes to the rescue. :) Thanks.

So how do I return an array in MX2 (shown here in MX1 code):
Global var1:Float=1.0 
Global var2:Float=2.0

Method Test:Float[]()
     Return[var1,var2]
End Method

'Currently my workaround is this:
Method Test:Float[]()
     Local tmp:= New Float[2]
     tmp[0]=var1
     tmp[1]=var2
     Return tmp
End Method


.

Also how do I extract (slice) part of a string in MX2 as shown here (again shown in MX1 code). As I recall; slices are not supported?
Local my_string:String="12345"
Print my_string[0..3]   'Which prints 123



marksibly(Posted 2016) [#2]
Return New Float[]( var1,var2 )

...and...

Print my_string.Slice( 0,3 )


Richard Betson(Posted 2016) [#3]
Ding! Awesome. Thanks!

Is that MX1 to MX2 conversion guide still up? Link?


dawlane(Posted 2016) [#4]
The MX1 to MX2 is HERE. I think that Mark should add this to the links on the language section of the new site for reference. One or two have said it's a bit hard to find.

One thing that I do find irritating is that must have a CR/FL on the last statement in the last line of a file. I would have thought that the parser would have checked for an EOF.


Richard Betson(Posted 2016) [#5]
^Thanks


Playniax(Posted 2016) [#6]
There is no array resize yet I think so you can do something like this:

Function Resize<T>:T[]( arr:T[],len:Int )
Local out:=New T[len]
arr.CopyTo( out,0,0,Min( len,arr.Length ) )
Return out
End


Richard Betson(Posted 2016) [#7]
Before I report this as a bug I thought I would check and make sure I'm doing this right. Below is a modification of Marks hello world sample. In it I add a class and a field with a variable defined as an integer. When I call the method within that class that sets a value to that variable the build crashes (terminate called after throwing an instance of 'bbException*'). Am I doing this right? Thanks.

Edit- Well If I change Field test_obj:Test to Field test_obj:= New Test it works. I have had MX2 though work in other instance without defining the object as New so I thought it was implicitly creating a new instance of an object, but I guess not.

Latest github V.0008



therevills(Posted 2016) [#8]
Its a bug that the build crashes... but you haven't initialised the Test class.

Try this:
Field test_obj:Test = New Test



Richard Betson(Posted 2016) [#9]
I just modified my post while you posted. :) I thought defining an object (Field myobj:Test) was implicitly creating the object. Wrong. :)


Richard Betson(Posted 2016) [#10]
I guess what was confusing me is the way the New constructor is working now. Before (MX1) having a New constructor method in a class was ignored when initializing a class object, but now it's not and I get an error. So something like this does not seem to work.
Class Test1
Field myobj:Test2=New Test2
End Class

Class Test2
Field var1:Int
     Method New(myvar:Int)
           Local new_obj=:Test2= New Test2
           new_obj.var1=myvar
     End Method
End Class



Richard Betson(Posted 2016) [#11]
I think this is how to do it now in MX2.

Class Test1
Field myobj:Test2=New Test2
Field myobj2:Test2=New Test2(1)
End Class

Class Test2
Field var1:Int
     Method New()
     End Method

     Method New(myvar:Int)
           Self.var1=myvar
     End Method
End Class

'*** Changed and corrected see post #15



marksibly(Posted 2016) [#12]
You need to add a 'default constructor', eg:

Method New()
End

Monkey1 always added a default ctor (if you hadn't already) to every class, but monkey2 will only add a default ctor to classes that don't have ANY ctors at all.


Richard Betson(Posted 2016) [#13]
Thanks and the way I probably should have been doing it in MX1. I'm converting the GUI portion of my framework which is about 6k lines of code and it should give me a good handle on MX2 when I get through it. :) I can't wait to see it work on MX2.


therevills(Posted 2016) [#14]
I think this is how to do it now in MX2.

What are you trying to do? That code has mistakes ( Local new_obj=:Test2= New Test2 should be Local new_obj:= New Test2), but even if it works the code in post #11 will make a local instance of Test2 (new_obj) within Test2's ctor which can never be accessed due to the scope of local.


Richard Betson(Posted 2016) [#15]
Just an example to help me understand the way New works and my mistake (in a sea of code conversion :/). It should be Self.var1=myvar (corrected it). Anyway Mark set me straight.

While I am here I was using Null as a default when initializing a list when calling a method in MX1 as shown here but in MX2 this does not work. Is there an alternative?
Class Test
     Method my_test(my_list:List<Test>=Null)
     End Method
End Class


Edit: This maybe. Edit.. nope.
Class Test
     Method my_test(my_list:List=Null)
     End Method
End Class



impixi(Posted 2016) [#16]
Put a space in between the > and = like so:

Class Test
     Method my_test(my_list:List<Test> = Null)
     End
End


Yes, I'd call that a parsing bug. Mark?


marksibly(Posted 2016) [#17]
That needs fixing...somehow...


Danilo(Posted 2016) [#18]
'>=' is probably tokenized/lexed as 'greater than or equal' operator.


Richard Betson(Posted 2016) [#19]
@impixi - Thanks and at least a workaround for now. One of the pluses of converting a large code base... finding bugs. :)


Richard Betson(Posted 2016) [#20]
Hi,

I am working with Scissors, Vec2, Rect and Recti. Some of this is new to me so I'm trying to get a handle on it.

When I do this with a canvas/window of 640x480 it will return "Vec2(640,480)".
Local Myr:Recti=Rect
Print Myr.BottomRight.ToString()


How do I extract just the X (640) and Y (480) for this (not "Vec2(640,480)")?

Thanks.

Edit- I'm assuming here that I don't need to convert to a string and can get the integers for X and Y. ;)


marksibly(Posted 2016) [#21]
Print Myr.BottomRight.X

...or...

Print Myr.max.x

...or even just...

Print Myr.Width

There are also Width and Height properties in View that just return Rect.Width and Rect.Height, so...

Print Width

Have a look at the 'docs' for std.app.View and std.geom.Rect for more.


Richard Betson(Posted 2016) [#22]
Print Myr.BottomRight.X

Aww I see. Thanks.

Have a look at the 'docs' for std.app.View and std.geom.Rect for more.

Ya, I have been checking it out. With getting ' BottomRight.X ' it totally seems pretty cool.


marksibly(Posted 2016) [#23]
Also, rect.Right and rect.Bottom...


Richard Betson(Posted 2016) [#24]
^LOL.. I was just going to edit my post. :)

Print Rect.BottomRight.X . Pretty sweet! I am liking the property aspect for sure. :)


Richard Betson(Posted 2016) [#25]
It's important to 'Extend' the Window class to whatever class you use these properties. Had me stumped for a minute. :)


Richard Betson(Posted 2016) [#26]
"It's important to 'Extend' the Window class to whatever class you use these properties."

Well I do not think that is the way to go for imported code files. When I extend 'Window' to an imported class (a class in a separate imported file) I get 2 windows on the desktop, which I think would be expected because of two 'Window' Instances. So if I want to use the mojo properties in a class outside of the main source and in an imported file of code and 'not' 'Extends Window' how would I do it? I would like to avoid extending 'MyWindow' (shown in main source) as well.

Basically, I want to use mojo properties in a class without using 'Extends Window'. So using the hello world sample something like this.

Main source.


The import file.



Richard Betson(Posted 2016) [#27]
Is it 'Extends View' because I think that works.
Edit not sure thats right but it compiles. Does not seem to get say Rect.BottomRight.X, so hmmm.


Richard Betson(Posted 2016) [#28]
Got it. It looks like I needed to Extends View on the import Test class (in the import above) and pass along the Rect instance to the method Get_X(). I think I'm getting this. :) So the method would be Get_X(rect:Recti).


marksibly(Posted 2016) [#29]
'Rect' is a property of Window (actually inherited from View - but don't think about views right now!) not a global, so code inside a window method can access it using plain 'Rect' (same as 'Self.Rect' really) whereas code outside window methods will need to use 'window.Rect' or something (and you'll have to pass that code 'window' somehow too).

The 'Rect' window property is actually of type 'Recti', which means 'rect of ints'. There's also 'Rectf' for float rect, and Rect<T> for rects of whatever type you want.

Some examples...

Function GetRectX:Int( r:Recti )   'note: Recti, not Rect!
   Return r.X
End

Function GetWindowRectX:Int( w:Window )
   Return GetRectX( w.Rect )
End

Class MyWindow()

   Method Blah()
      Print GetRectX( Rect )
      Print GetWindowRectX( Self )
   End
End 



Richard Betson(Posted 2016) [#30]
Got it. Thanks!


Richard Betson(Posted 2016) [#31]
OK, I have nearly 6k lines of code converted and completed and using the hello world sample as a main source and only just importing (not using an classes, functions or methods) the code I get this. I'm not sure yet whats gone wrong but there are no error line numbers for sure. :)

Edit- Using v.0008




Richard Betson(Posted 2016) [#32]
OK... I got it to compile. Looking over the error dialog in console I could make out about where the compiler was failing. The issue was some double pluses ie: ++ . While adding two variables somehow I had doubled up the plus signs (probably from cut and paste) so var++var should have been var+var. So I guess thats a bug that it crashed?

The good news is all that code just compiled and although not tested yet is a big step forward for me. :)


Richard Betson(Posted 2016) [#33]
How do I do it in MX2? Pretty frickin well. :P I ported my GUI framework and already have a GUI window running and working and displaying images. Sweet! I'm still working the kinks out but I can tell already it should port fine. Thats really incredible! That I can port 6k lines of code over to MX2 from MX1 and have it starting to come together in 2 days. I could not have done it without Mark tutoring me through and thanks buddy! I really am impressed by MX2 and I am enjoying the discovery of all the neat features.