How to import things?

Monkey Forums/Monkey Beginners/How to import things?

Impmaster(Posted 2014) [#1]
Hey all. I moved over from Java programming to this, because Monkey X seems like a fairly viable engine (oh god java is terrible). How do I import items? I have a main class where I have this:

Import Ship

Field player:Ship

Method OnCreate()
SetUpdateRate 60
player = New Ship()
End

And I have a different file which is called Ship. However, when I try to build and run I get this error message: Type 'Ship' not found.

What am I doing wrong?


AdamRedwoods(Posted 2014) [#2]
other modules should be lowercase and end with the ".monkey" extension. "ship.monkey". Not uppercase.


Goodlookinguy(Posted 2014) [#3]
oh god java is terrible

While I agree with you, there are two people on this forum who would strongly disagree.

As for the error and importing. With the code snippet you presented, I don't see a Ship type defined. If one was defined, then it would be having issues because you have a file name by the same name.

You really need to read up on how importing works: http://www.monkey-x.com/docs/html/Programming_Language%20reference.html#modules


Impmaster(Posted 2014) [#4]
Wait correct me if I'm wrong. I come from a Java background, where you hardly ever define multiple classes in one file. You generally have one file for each class. That's what I'm trying to do here. Is this possible in Monkey X, or do I have to make a new "module" thing each time?

What I'm asking is: Is it standard to have multiple classes all in the same file?


Goodlookinguy(Posted 2014) [#5]
Well, in Monkey you can do whatever you want. It's usually best practice to have one class per file though. As for the naming, file names define a sort-of scope and having a file name (case wise) be the same as a class name, the scopes will collide and have an error.

Another topic on this: http://www.monkey-x.com/Community/posts.php?topic=7947


Impmaster(Posted 2014) [#6]
Alright, so I changed the filename to a lower case and it works now. Thanks!


Gerry Quinn(Posted 2014) [#7]
Monkey filenames are case-sensitive, so you need to have a standard, and the de-facto standard has become lowercase for filenames, so I would advise sticking with that.
I think one class per file is fairly standard but not adhered to 100%. Mark for example has a bunch of classes such as IntList derived from List in the same file, which I think is pretty reasonable. And I usually put POD classes in the same file as the class that uses them (e.g. if I had a Particle class and a struct called ParticleParams that held initialisation information), they would go in the same file.