Working with multiple classes

Monkey Forums/Monkey Programming/Working with multiple classes

Soap(Posted 2011) [#1]
I know that you can have more than one class in a single document. How would you split the classes into multiple documents?


muddy_shoes(Posted 2011) [#2]
Just put them in files however you want and use the Import keyword to bring references in where needed.


Tibit(Posted 2011) [#3]
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.


Samah(Posted 2011) [#4]
I've never been a fan of partial classes. It just seems like an excuse not to refactor.


Tibit(Posted 2011) [#5]
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


Gerry Quinn(Posted 2011) [#6]
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.


Soap(Posted 2011) [#7]
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.


muddy_shoes(Posted 2011) [#8]
You're going to have to post code. "Initialize a variable" could mean many things.


Soap(Posted 2011) [#9]
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


muddy_shoes(Posted 2011) [#10]
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.


Soap(Posted 2011) [#11]
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


muddy_shoes(Posted 2011) [#12]
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.


Soap(Posted 2011) [#13]
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.


muddy_shoes(Posted 2011) [#14]
Check that your files are named all lower case and change the import to also be lower case "Import pet", not "Import Pet".


Soap(Posted 2011) [#15]
So the filenames can't be the same as classes? That's annoying. :-/ Thanks, that was the problem.