Import
Monkey Forums/Monkey Programming/Import
| ||
Firstly, could anybody tell me how I find the documentation for Monkey? Usually when I hit F1 I just get a blank page. Anyway, I'm having issues using Import and I believe there's no Include command? What do I need to know about Import, or can somebody point me to the documentation, because I'm getting some odd behaviour. For example, when I put my class inside an import file and then try to access it, I'm getting null object errors. But then I paste the code back into the main file, and it works fine. E.g When I want to draw my aliens, I can't do 'Alien.Draw etc' if the class is in the imported file, otherwise I get a null error. E.g Method OnRender() For Local Alien:=Eachin AlienList Alien.Draw(width,height,Player.X,Player.Y) Next End This causes the null object error if my class is in the import file, so I have to put the draw commands directly inside the OnRender loop. However, if I then paste the class back directly into my main file, I can use the code above without the error. By the way, is there any way to get that entire loop inside the class? For example in BlitzMAX I'd do that loop inside the Draw method, i.e the method would look something like.... Type Alien Function UpdateAliens() If Not AlienList Then Return For Local A:Alien=EachIn AlienList A.Draw() A.Move() Next End Function End Type So then I could call the whole thing with just a single line in the OnRender loop, e.g just 'Alien.UpdateAliens'. Sorry about the noobishness here, I'm coming from BMAX and struggling a bit with the syntax, especially with the documentation. I'm also still struggling slightly with OOP, although I'm starting to get it, very, very slowly xD |
| ||
For example, when I put my class inside an import file and then try to access it, I'm getting null object errors. But then I paste the code back into the main file, and it works fine. You must import your class file in the main file, and in the class file the main file. Monkey supports cyclic imports, which BMax doesn't. By the way, is there any way to get that entire loop inside the class? Class Alien Global Alienlist:TList<Alien>=new TList<Alien> Function UpdateAliens:Void() For Local A:Alien=EachIn Alien.AlienList A.Draw() A.Move() Next End Function End Type This should work (untested). |
| ||
I'm getting the error, 'Class TList not found' on the line, Global Alienlist:TList<Alien>=new TList<Alien> Also, I did put the import line at the top of both files, e.g the class file in the main file and the main file in the class file. So I don't think that was the problem. |
| ||
remove "T" and it should work |
| ||
Oop, TList was BMax. It's just "List".Class Alien Global Alienlist:TList<Alien>=new TList<Alien> Function UpdateAliens:Void() For Local A:Alien=EachIn Alien.AlienList A.Draw() A.Move() Next End Function End Class Firstly, could anybody tell me how I find the documentation for Monkey? Usually when I hit F1 I just get a blank page. If I start monk.exe the docs are shown AND I can select help from the menu. For your other problem please post the content of the files. |
| ||
Still not working though, because then I have no way of jumping into the code. For example, in BMAX I could do 'Alien.UpdateAliens' and then put the loop in the Update method, but if I try that in Monkey, it gives an error. It means I have to put the entire loop code in the OnRender section, which I'd prefer to move into functions. Indeed, since I'm having the same problem with Import, I seem to be forced to put almost my entire code inside the OnUpdate and OnRender sections and it's very messy - would much prefer to move as much code as possible into their own functions. I'll put together a small program demonstrating in the next post. |
| ||
Hmm typical, ripping my code apart and rewriting it, also appears to have fixed it :p Well at least, the Imports now work correctly. Still can't get the loops to work inside the Update section though. Ugh, I've just seen what the problem was. I'd capitalised the name of the import file - didn't realise capitalisation was important even in file names :s |
| ||
Still can't get the loops to work inside the Update section though. Remember that you can call draw commands like DrawImage etc. only in the OnRender() method of the app. |
| ||
Yeah, it's in the right section, but I think the problem is that you can't call into a class method without a errr... sorry, don't know the terminology. For example, in BMAX I could do this ..... Alien.UpdateAliens() ' Anywhere in the program. Then in the Alien class I could do.... Type Alien Function UpdateAliens() If Not AlienList Then Return For Local A:Alien=EachIn AlienList A.Draw() A.Move() Next End Function End Type But I can't do that in Monkey, because if I just put 'Alien.UpdateAliens', it doesn't know what 'Alien' is (it's just the name of the class) - it just says 'Identifier Alien not found'. So I need some sort of instance of Alien to even go to the method? |
| ||
With Alien.UpdateAliens() you call the FUNCTION UpdateAliens() in the class Aliens. This works in Monkey as in BMax. Of course you can't call a METHOD this way, because for this you need a already created object of the class. Working code: Import mojo Class MyApp Extends App Method OnCreate() SetUpdateRate 15 Local alien:Alien=new Alien End Method Method OnUpdate() End Method OnRender() Cls Alien.UpdateAliens() DrawText("Test",10,10) End End Function Main() New MyApp End Class Alien Global AlienList:List<Alien>=new List<Alien> Method New() AlienList.AddLast(Self) End Method Function UpdateAliens:Void() For Local A:Alien=EachIn Alien.AlienList A.Draw() ' don't call this in update! ' A.Move() Next End Function Method Draw:Void() DrawText("Aliens coming !!",100,100) End Method End Class |
| ||
Normally I split up logic and rendering:Strict Import mojo Function Main:Int() New MyApp Return 0 End Class MyApp Extends App Method OnCreate:Int() SetUpdateRate 15 Local alien:Alien = New Alien Return 0 End Method OnUpdate:Int() Alien.UpdateAliens() Return 0 End Method OnRender:Int() Cls Alien.DrawAliens() DrawText("Test",10,10) Return 0 End End Class Alien Global alienList:List<Alien> = New List<Alien> Field x:Float, y:Float Method New() alienList.AddLast(Self) End Method Move:Void() x+=1 y+=1 End Function UpdateAliens:Void() For Local a:Alien = Eachin Alien.alienList a.Move() Next End Function DrawAliens:Void() For Local a:Alien = Eachin Alien.alienList a.Draw() Next End Method Draw:Void() DrawText("Aliens coming !!", x, y) End End |
| ||
Oops yeah, my bad, getting confused between functions and methods again. Thanks for the help :) |