Welcome, Guest. Please login or register.
Did you miss your activation email?
February 09, 2010, 02:14:40 pm
advanced search




Pages: [1]
  Print  
Author Topic: Using JME with the scala language  (Read 4523 times)
wooyay
Jr. Member
**
Offline Offline

Posts: 63



View Profile
« on: April 04, 2007, 05:04:15 pm »

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:

Code:
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
Logged
dougnukem
Jr. Member
**
Offline Offline

Posts: 205



View Profile WWW
« Reply #1 on: April 04, 2007, 06:26:47 pm »

Cool!  smiley

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!)


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’s Perspective

He likes Haskell for similar reasons you like Scala, static types and dependent types, and he also discusses the benefits of using Haskell for concurrent programming because of it's functional nature makes it easier to encapsulate mutability (you have less complex synchronization policies when you can guarantee immutability, and have well defined places of mutability, this also allows for better concurrent scaling meaning more CPU's equals more ).
Logged

wooyay
Jr. Member
**
Offline Offline

Posts: 63



View Profile
« Reply #2 on: May 11, 2007, 10:36:14 pm »

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:
Code:
game.runInGLThread(
  new Callable {
    public void run() {
      view.setLocalTranslation(position)
    }
  }
)

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

Other nice features of Scala:
Type Inference:
Instead of
Code:
StandardGame game = new StandardGame("Test")
I can write
Code:
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):
Code:
("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)
« Last Edit: May 12, 2007, 05:27:17 pm by wooyay » Logged
dougnukem
Jr. Member
**
Offline Offline

Posts: 205



View Profile WWW
« Reply #3 on: May 15, 2007, 08:01:49 am »

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
Code:
      def onKey(c :Char, i :int,b :Boolean):Unit = {

StandardGame.scala
Code:
  protected def initGame():Unit = {
Logged

Tags:
Pages: [1]
  Print  
 
Jump to: