Using JME with the scala language

I’m currently trying to learn the scala language (http://www.scala-lang.org/)

Scala is the next big thing in language design - it is a mix between object oriented and functional programming.

Scala has the nice features of Ruby without the performance hit. It is also statically typed, which means less run time testing and error warnings directly in eclipse.



What better way to learn a new language than writing a game?

Scala compiles to java bytecode, which means that I can use all java libraries - including JME.

Here is TestSimpleGame written in Scala:



package test
import com.jme.app.SimpleGame
import com.jme.bounding.BoundingSphere
import com.jme.math.Vector3f
import com.jme.scene.shape.Box

object HelloWorld {
  def main(args: Array[String]) :Unit = {

    object MyGame extends SimpleGame {
      override def simpleInitGame() {
        this.display.setTitle("A Simple Test - written in Scala!")
        def box = new Box("my box", new Vector3f(0, 0, 0), 2, 2, 2)
        box.setModelBound(new BoundingSphere())
        box.updateModelBound()
        this.rootNode.attachChild(box)
      }
    }
    MyGame.setDialogBehaviour(2)
    MyGame.start()
  }
}



This is of course not very exciting. The main difference to the java version is that MyGame is a singleton object, not a class.
I had problems accessing AbstractGame.ALWAYS_SHOW_PROPS_DIALOG, because it is abstract and protected.

I will let you know when I do something that better demonstrates the awesomeness of scala.

Cheers,
Martin

Cool!  :slight_smile:



I'll have to take a look at Scala, sounds intriguing. I really do like the idea of statically types languages for use as the core infrastructure for a game, but still allowing for dynamic languages to be used for the more flexible areas.



I've also been messing around with using other languages to code up jME applications. Basically I wrote up some simple hooks to allow any JSR 223 compliant scripting language to access jME, and to easily create spatial controllers out of scripts. In the future I'm hoping multiple language options will let developers choose which programming paradigm fits which part of their game (maybe a little scheme for AI, a little haskell for concurrent simulations, some python for game rules and then some Java to glue it all together, or perhaps some Scala to put it all together. Haha or maybe that's just a recipe for a maintenance nightmare!)


dougnukem said:


JMonkey Engine Scripting


Have you looked into Haskell (see Jaskell for java implementation)? I read a presentation by Tim Sweeney (developer on the Unreal engine) about his take on the next mainstream programming language.

The Next Mainstream Programming Language:A Game Developer

OK, I spent some more time mucking around with Scala.



Here are my results: I built a minimal Mass Spring physics simulation. I tried using some of the functional aspects of Scala (although the applications are a bit strained).







The mesh is built and interconnected with a recursive function.  A recursive function creates a quad tree of mass points, then a second function recurses over that tree and interconnects the mass points with springs. I reimplemented StandardGame in scala (the code is nearly identical).



I think the main advantage of scala over java is support for anonymous functions and closures.

You decide: Which looks better?

Java:


game.runInGLThread(
  new Callable {
    public void run() {
      view.setLocalTranslation(position)
    }
  }
)



Scala:


game.runInGLThread( ()=>{
  view.setLocalTranslation(position)
})



Other nice features of Scala:
Type Inference:
Instead of

StandardGame game = new StandardGame("Test")

I can write

val game = new StandardGame("Test")


Scala figures out the variable type on its own.

Actor library: I haven't had time to play with this yet, but it seems to be really cool. AFAIK, actors can pass asynchronous messages. It is possible to run each actor in its own thread, or use some kind of event queue.
http://www.scala-lang.org/docu/files/haller07actorsunify.pdf

Unfortunately, Scala's integration into eclipse is not yet perfect. I had some weird error messages and compiler crashes. Also, many functions  of Java eclipse like "go to declaration" are not yet available in the scala plugin.

But all in all, coding in scala is a nice experience. Scala has a feeling to it that is similar to ruby (which is a good thing in my opinion):

("Scala" :: "is" :: "the" :: "shit" ::Nil).map(word => Console.println(word+ "!"))



A zip of the Mass Spring code: http://gonzo.uni-weimar.de/~scheffl2/jme/MassSpring_Scala_JME_Example.zip

Here's a small animation of the Mass Spring thing in action:

(Yes, the frame rate is abysmal. Too lazy to optimize... Also, the physics engine loops about ten times per frame. This is necessary because I use explicit euler integration, which is very unstable)

Very cool!!



I tried out the code using the Eclipse Scala plugin, and I got a couple errors about not being able to override methods because of the signatures, so I added the following ": Unit" to the signatures:



HelloWorld.scala


      def onKey(c :Char, i :int,b :Boolean):Unit = {



StandardGame.scala


  protected def initGame():Unit = {