Working with multiple classes
Monkey Forums/Monkey Programming/Working with multiple classes
| ||
I know that you can have more than one class in a single document. How would you split the classes into multiple documents? |
| ||
Just put them in files however you want and use the Import keyword to bring references in where needed. |
| ||
I don't think you can. There is no "partial" classes in Monkey that I'm aware of. However you can always just refactor your class into several smaller classes and then put those into files of their own. |
| ||
I've never been a fan of partial classes. It just seems like an excuse not to refactor. |
| ||
In C# if you have code that is auto generated by a program, it would be a pain to modify that source, since it would be overriden all the time, this is one occation where partial classes shine. Though I don't see much point in having them in Monkey yet :P |
| ||
It's nice to have in C#, but not essential by any means. Apart from auto-generated code, another advantage may be that in the absence of a separate header file, it's hard to get an overview of the methods etc. in a given class, unless the IDE does it for you. Partial classes would allow you to split a large class into more manageable chunks. |
| ||
I mean I have a class called Pet that I have saved in Pet.monkey. How would I bring this into another class in a completely different file i.e. Main.monkey. I tried importing it but that won't work. There's no error at the import but when I want to initialize a variable it errors out even with the import. |
| ||
You're going to have to post code. "Initialize a variable" could mean many things. |
| ||
MainClass.monkey #Rem 'buildopt: html5 'buildopt: run #End Import mojo.app Import mojo.graphics Class MainClass Extends App Field pet:Pet Method OnCreate() ' Startup code goes here 'pet = new Pet("picts/temp_pet.png") SetUpdateRate 60 End Method OnUpdate() ' Game code goes here End Method OnRender() ' Drawing code goes here End Method OnSuspend() End Method OnResume() End End Function Main() New MainClass End Pet.monkey Import mojo.graphics Class Pet Field x:Float, y:Float Field pict:Image Method New(url:String) pict = LoadImage(url) End Method Draw() DrawImage pict, 40, 40 End End Function Main() New Pet End |
| ||
Remove the Main function from pet.monkey and add "Import pet" to mainclass.monkey. Edit: Actually, it may not be necessary to remove the Main function, but I don't know why you have it unless you're using it to test the pet class in isolation. |
| ||
This doesn't work even still. MainClass.monkey #Rem 'buildopt: html5 'buildopt: run #End Import mojo.app Import mojo.graphics Import Pet Class MainClass Extends App Field pet:Pet Method OnCreate() ' Startup code goes here pet = new Pet("picts/temp_pet.png") SetUpdateRate 60 End Method OnUpdate() ' Game code goes here End Method OnRender() ' Drawing code goes here End Method OnSuspend() End Method OnResume() End End Function Main() New MainClass End Pet.monkey Import mojo.graphics Class Pet Field x:Float, y:Float Field pict:Image Method New(url:String) pict = LoadImage(url) End Method Draw() DrawImage pict, 40, 40 End End |
| ||
You aren't telling us what problem you're seeing. "This doesn't work" isn't very informative. What's the error? Are your files in the same directory? Are you compiling the mainclass.monkey file? By the way, it's generally preferable to name the files all lower case to avoid clashes with the class names. |
| ||
They're in the same directory. The MainClass is called something different but I just renamed it when I posted it. This is what error I get: TRANS monkey compiler V1.15 Parsing... Semanting... /Users/bob/Documents/Monkey/Projects/MainClass/MainClass.monkey<14> : Error : Class 'Pet' not found Thanks for your help everyone. |
| ||
Check that your files are named all lower case and change the import to also be lower case "Import pet", not "Import Pet". |
| ||
So the filenames can't be the same as classes? That's annoying. :-/ Thanks, that was the problem. |