About Monkey 2 › Forums › Monkey 2 Programming Help › Import/Includes and globals?
This topic contains 21 replies, has 7 voices, and was last updated by
Richard Betson
2 years, 6 months ago.
-
AuthorPosts
-
October 5, 2016 at 7:30 pm #4282
It seems MonkeyX2 v1.0.5 doesnt like imported classes is there a way of doing the whole thing.
Main.monkey2 looks like
Monkey1234567891011121314151617181920212223242526272829303132Namespace myapp#Import "<std>"#Import "<mojo>"#import "Ultim"Using std..Using mojo..Class MyWindow Extends WindowMethod New()Ultim.Initialise()endMethod OnRender( canvas:Canvas ) Overridecanvas.DrawText( "Hello World",Width/2,Height/2,.5,.5 )EndEndFunction Main()New AppInstanceNew MyWindowApp.Run()EndAnd the ultim class import looks like this
Monkey12345678910111213'' Ultim.monkey2 - Copyright (C)EdzUp' Programmed by Ed 'EdzUp' Upton'Class UltimClassMethod Initialise:Int( )Return( 0 )endEndGlobal Ultim:UltimClass = New UltimClassWhat im trying to do is include the file in the main source like I could with all the other systems and call Ultim.whatever to call the relevant functions from the Ultim class so if I add a Audio class to the Ultim class that links to another AudioClass class with Audio:AudioClass … then I would use Ultim.Audio.x. This way I can build a framework to use in monkey2 and actually use some of its power.
I think a tut on including classes from other files would be a brilliant idea as this beasty is a lot different than any other BRL language to date.
October 6, 2016 at 12:31 am #4283Define a namespace at the top of ultim.monkey2 e.g:
Namespace ultim
In your main.monkey2 your import should be:
#Import "ultim.monkey2"
and then you need to put the following at the top:
Using ultim..
Alternatively, just delete the Namespace defined in main.monkey2 and fix the import statement, as above.
October 6, 2016 at 5:16 am #4288Ah thanks impixi will have a look at that
October 6, 2016 at 6:45 am #4290Here’s a little zip with a working example of multiple imports.
Runs fine with 1.0.7 but should be ok with earlier versions.main:
[/crayon]Monkey12345678910111213141516171819202122232425262728293031[crayon-5cba87ff3c453044989570 inline="true" ]Namespace myapp#Import "<std>"#Import "lib_a"'you could comment this line because lib_d imports lib_b but as I directly use lib_b I prefer to import it twice, won't be double after compil#Import "lib_b"#Import "lib_c"#Import "lib_d"Using std..Using lib_b..' I use lib_c. prefix and not "Using lib_c.." for demonstrationUsing lib_d..Function Main()'Glob_a is already defined in lib_a (=5)Glob_b=7lib_c.Glob_c=12 'using namespace prefixLocal foo:=New Class_b(21)Local foo2:=New Class_d(37)Print Glob_aPrint Glob_bPrint lib_c.Glob_cPrint foo.Get()Print foo2.Get()Print fooGlobal.Get()'defined in lib_dEndlib_a
[/crayon]Monkey123[crayon-5cba87ff3c45a884704702 inline="true" ]namespace myappGlobal Glob_a:=5lib_b
[/crayon]Monkey1234567891011121314[crayon-5cba87ff3c45f613045374 inline="true" ]Namespace lib_bGlobal Glob_b:IntClass Class_bField fi:IntMethod New(i:Int)fi=iEndMethod Get:Int()Return fiEndEndlib_c
[/crayon]Monkey1234[crayon-5cba87ff3c464243263258 inline="true" ]Namespace lib_cGlobal Glob_c:Intlib_d
[/crayon]Monkey123456789101112131415161718[crayon-5cba87ff3c468122105568 inline="true" ]Namespace lib_d'if lib_b is imported elsewhere via main you could miss the following but it would be bad practice, keep sure your dependecies are imported'anyway you can import this several times, it won't be duplicated after compilation#Import "lib_b"Using lib_b..Global fooGlobal:=New Class_d(51)Class Class_d Extends Class_bMethod New(i:Int) 'in monkey2 you must define the constuctor if you defined one in the super classSuper.New(i)EndMethod Set(i:Int)fi=iEndEndedit:added a global class declared outside of main so use import-1.monkey2
Attachments:
October 6, 2016 at 9:01 am #4296Here your code corrected and running..
[/crayon]Monkey123456789101112131415161718192021222324252627282930313233[crayon-5cba87ff43309974536406 inline="true" ]Namespace myapp#Import "<std>"#Import "<mojo>"#import "Ultim"Using std..Using mojo..Using Ultim..Class MyWindow Extends WindowField s:StringMethod New()s=UltimC.Init()endMethod OnRender( canvas:Canvas ) Overridecanvas.DrawText( "Hello World "+s,Width/2,Height/2,.5,.5 )EndEndFunction Main()New AppInstanceNew MyWindowApp.Run()Endthe Ultim.monkey2
[/crayon]Monkey123456789[crayon-5cba87ff4330f938295834 inline="true" ]Namespace UltimClass UltimClassMethod Init:String( )Return "Ultim"endEndGlobal UltimC:= New UltimClassNote that I renamed things called Ultim to Ultimsomething because you can’t have the same for a variable and a namespace/filename.
October 6, 2016 at 9:07 am #4297abakobo: thanks for that im still in newb land with MX2 so it will takke a while for mw to get upto speed but I will persevere with it
EDIT: I now have the namespace set to UltimEngine and instead of UltimC its Ultim which looks a bit more cleaner but thanks once again for the help, im new to all these namespaces didnt use them in C++ either as there it was std:: etc :).
October 6, 2016 at 5:24 pm #4299im new to all these namespaces didnt use them in C++ either as there it was std:: etc
I feel ya.. The last time I really used C/C++ was with Borland in the 90’s. For the last IDK 12-15 years I’ve just been using BRL products. But, once you get a hold of a few basic concepts Monkey 2 comes into focus pretty quickly.
-
AuthorPosts
You must be logged in to reply to this topic.