I'm making a falling sand game in Java. I want users to be able to write their own engine for the game and I thought a scripting language might work for that. I've tried out a small script with jython and it's many times slower than that java version.
I need a scripting language that has fast loops and/or fast array access since that's what the game will be doing a lot of. Or if you have any suggestions on another way to let users write their own engine for the game.
I'm also not entirely sure this can work (well). If you don't think it can please let me know why and maybe some possible alternatives.
JavaCC
You could write you're own simple game language with JavaCC. This does give's you all the flexibily you possibly want and native Java speed (well that actualy depends on you're implementation). But you need to keep you're syntax simple or else it will take some efford...
Java
Java? Yes Java! Why not let users extend you're game by Java code? In Java 6 there is a Java compiler API:
javax.tools API.
Beanshell vs Rhino
Some performace comparison between BeanShell and the Rhino (Javascript) interpreters (Rhino is the winner):
thoughtworker.in
Pankaj Kumar's Weblog
You might look at BeanShell for this. In my use it's been pretty much as fast as native Java (as that is what it becomes) and is pretty accessible as it is Java, albeit locked around Java 1.4.
RelProxy is Java compiler on the fly and an automatic hot class reloader, pure Java code can be executed like a scripting language.
Because Java is faaast Java is then maybe the fastest scripting language, optionally you can use .class files like a sort of compiled cache to avoid compiling on load.
Related
I am currently writing a very simple 2.5D game engine in java and game. This is basically just a fun project I am working on so the amount of time it will take to implement features is no problem, as I can always put it aside and work on it whenever. With that said, I was planning on implementing a custom sort of scripting system. Basically the engine would be a shell of a 2.5D game, which can be modified and extended through simple scripts. So far I have the following ideas of how to go about this but I am unsure of what is practical or not.
Write a very simplistic scripting language and a simplistic parser that checks a folder for the scripts and runs them.
Make a java library/API that allows you to modify parts of the game on client and server via just plain old java
Use an already created language that is very simple such as lua for extendable parts of the game
I really thought about option 1 as being what I wanted to do, however I have never really done such a thing, I was wondering if someone could recommend what I should do here. If I do go with writing a simplistic language, I am just looking for some material or documentation on such topics, would I just look at how people wrote basic compilers/interpreters?
The short answer would be 2, go with the API. It's the easiest way to add or change features to your game engine.
Creating your own scripting language and parser is tempting for every developer. It's a great learning experience, but it's also a downside. When you start to write the scripts and the result isn't what you expected, you don't know if the bug is in your script or in the parser. You will need some debugging info from the parser maybe some stacktrace, even a very simple scripting language may start to take over your project. When you start to write your scripts you will probably feel limited by the features missing, the ones you are used to from writing java code and maybe start to expand the scripting language.
Some of the downsides with creating your own language is also true for allready existing languages. If you really need a very free way to extend your game engine you can choose that road, but I would not recommend it. You would still risk shifting your focus from your game engine to the scripting implementation.
One way to think about it is also from the writer of the mods point of view. Would they want to write in a new script language, in a existing script language or in java? The answer will most likely be java and probably not a new language.
I am making some kind of sandbox-engine, the end-users are able to make scripts to make their world dynamic etc, currently I am only looking at LuaJava because I have quitte some experience with Lua and find it a very readable/easy language. But I also understand that it might be a bad idea to choose only based on personal preference, after all Lua is meant to be embedded into C so performance wont be the best I imagine.
But after a look at some of alternatives (Groovy, Clojure) I find the syntax just unreadable/too abstract, Lua was my first programming experience and even that was quite hard to 'get' at first, I'm afraid these languages would just have scared the crap out of me and I would never have looked at scripting again.
Are there scripting languages that can be embedded in Java that compete with Lua on simplicity?
Edit
My problem with JavaScript, JPython is all the braces etc, as a starting user symbols tend to look 'hard'. Also for python there is the concept of Object's that the user needs to comprehend and isn't that useful in this case.
func = function(arg)
print(arg)
end
Is so simple...
I think JavaScript is very simple, and it can be embedded in Java quite easily via Rhino. Scripts can be both pre-compiled, or compiled on-the-fly via the javax.script classes, which are used to connect to script engines for the Java platform (of which Rhino is one).
If you like Lua, though, there's a Lua for Java project called — *cough* — Kahlua. They list "Fast runtime of the most common operations" as one of their goals.
Edit: Re your edit, I'm not immediately seeing why this:
func = function(arg)
print(arg)
end
is substantially easier to understand than this:
func = function(arg) {
print(arg);
};
...which is the literal translation from Lua to JavaScript, pre-supposing a function called print exists on your platform. I would normally write that like this instead:
function func(arg) {
print(arg);
}
...but the other way is fine for most purposes.
But you should use what you're comfortable with.
You should try Jython and JRuby
I would like to know the best (fastest) scripting language for use in Java. I don't much care if it takes long to load (as long as it is a one time load), as opposed to how fast it runs. I am currently using Jython (python) but I would like something faster.
There's already a lot of benchmarks and discussions on this.
While I don't give a lot of credit (none) to benchmarking. The top 2 contenders are (listed in order of performance speed):
Scala
Groovy++
I've tried both and their not the same in my use cases. Scala came out much faster than groovy++ (again.. this is MY use cases not and may show differently in your use cases). Scala was almost native java speeds.
Groovy (not Groovy++), Closure, JRuby are all really slow.
Groovy and JRuby run approximately 8 times slower on simple algorithms compared to the java versions even after a decent amount of warmup.
I can't guarantee that you would get the same numbers that I did but this would be a decent order to try them in.
Groovy is a very good scripting language that plays very nicely with Java.
That being said, Java can run any scripting language via the command line, or it could run any other program if you wanted it to. So I would focus less on the Java side of it and more on the 'faster' side of it.
There's nothing from stopping you to writing a C++ program to compiled code and calling that.
Further, what profiling tests have you done with Jython/python? How slow is it that it doesn't meet your expectations? Is it causing problems? Is it perhaps in the python script?
Have you tried programming in Java?
You can write some of the performance critical functionality in Java, and leave most of the code in the scripting language of your choice.
Java since version 7 has support for normal compiling at runtime, if SDK is on the path, javax.tools.JavaCompiler
LuaJIT https://github.com/gareins/dynamic_benchmarks
The above benchmarks show LuaJIT to be very fast but it still qualifies as a dynamic language.
What is the best dynamic language to pair with Java on a large Java project?
We are considering using a dynamic language for tests, controllers, services. Some options are Groovy, JRuby or Jython. What are the pros and cons of each for this? Ideally we'd be able to call Java from the dynamic language as well as call the dynamic language from Java.
EDIT: If it helps, we're using Hibernate with PicoContainer and Webwork.
Thanks,
Alex
There are really three dynamic languages that offer a very seamless interop with Java - scala, groovy and clojure. From there, I'd ask your team which language they would rather work in or have them try a prototype in each language and see what they think.
If the team efficiency isn't important in the beginning, look to what problem each language attempts to solve:
Groovy is going to be very loose but natural to experienced Java developers and allows fast prototype development due to it's duck typing.
Scala is going to enable you to write DSLs making it a good for frameworks and tools where you want to solve the problem in a language more akin to how you would describe the problem.
Clojure is going to impose lisp's functional programming and immutable state concepts and could be a very natural fit for problems in AI, natural language processing, etc.
Finally, I've gone down the path of looking for the perfect language to base projects on and have found there is no perfect language. All of the languages I've mentioned above compile to native JVM byte code and are quite solid. Sometimes you just need to pick a language that might not be as cool as the others but gets you on the way to solving your problem.
I recommend Groovy, principally because it interoperates seamlessly with Java, and is almost a superset of Java, so very easy for a Java developer to learn. I have absolutely no evidence to support this, but based on hearsay, guesswork, and personal experience, I suspect the Groovy community is much larger than that of either JRuby or Jython.
Incidentally, Groovy++ is way too immature to consider for production use, in my opinion.
The answer is, of course, going to depend somewhat on matters of taste and flexibility. If there are folk who don't have experience with Ruby or Python then Groovy is going to have a syntax much closer to Java (in fact it is a superset of Java), and consequently be a much easier sell.
I can't really speak to JRuby as I haven't used it.
Groovy gives you probably the easiest interop with Java of the three you listed. It also has a very nice BDD library in EasyB which I like a lot. On the negative side I don't think the features or syntax of Groovy really hang together very well. It can kind of feel like a whole bunch of separate extensions to Java.
Jython is of course Python so the syntax is different, but also has all the consistency of Python. Interop is very good at the script level but at least used to be a bit awkward if you wanted to write pre-compiled classes in Jython that you call from Java. The other main pro over Groovy for me is that is that you have a real REPL to interact with the Java project.
I would also mention Clojure, the syntax is even more different but the Java interop is excellent, probably the best of all, and again you have a REPL. On the down-side if folk have trouble adjusting to Ruby or Python syntax then a Lisp is probably right out.
Clojure is probably the best dynamic language for controllers and services. (depending on what you mean with "services".
Scala and Groovy++ has the best java interop, but those are not dynamic (well in Groovy++ you decide for yourself what is typed.). Scala has the look and feel of a dynamic language. Scala has good testing frameworks http://www.scalatest.org/ and Specs and AKKA is very mature for services and also has Java APIs
I'd suggest going with Jython. The syntax is clean, and you get whatever additional power/conveniences that Python gives you.
For example, if you were to go with Groovy, you are basically limited to only what Java will give you. Jython would add the powers of Python to that as well.
If it helps any, I've used Jython with Hibernate, SOAP, Corba, and EJBs and it is much easier than doing the same with just plain Java.
Is it possible for a developer to use Scala without knowing Java?
Is it possible for a developer to use Clojure without knowing Java?
Note: For example I am a C# developer and I use .NET without knowing any VB (Of-course WF 4.0 uses VB for statements, so I refuse any projects involving WF 4.0 :) ).
Summary I: Thanks to all for your answers. Now I can tell one can use these languages without knowing Java (The Language). Now I've got another question: How can one start with Java (JVM) ecosystem? What are beginner, intermediate and advanced knowledge-bases (bags!)? To be honest Java ecosystem was always a bit confusing to me, so it would be very helpful to provide some "reference references" and "defacto tutorials" for learning (curve!) JVM.
(OK! I will post it as another question!)
Scala, and Clojure even more so, are totally different languages from Java. The only thing they have in common is that they run on the Java virtual machine. You do not need to know the Java programming language to program in Scala or Clojure. It would be useful, however, to know something about the Java ecosystem (the Java virtual machine and runtime environment).
Sure you can. You'll lack the knowledge of the standard library at first, but that's not a big problem when you're starting with a new languages anyway. Clojure & Scala, however, put a heavy emphasis on the Java interop and do not try to hide the raw Java stuff, so you'd probably have to learn more about Java(not the language - the Java Platform) at some point to make maximum use of the language and the underlying platform.
Speaking from the perspective of knowing Java and Clojure (but not Scala), you can use Clojure just fine without knowing Java. You have access to the core language, which is fully-featured, and libraries (e.g. clojure-contrib).
However, one of the big benefits of using Clojure (versus other lisps) is access to the Java ecosystem - libraries, debuggers, JVM, etc.
So, yes you can use Clojure effectively and I wouldn't let a lack of Java knowledge hinder you, but you'll add to your bag of tricks if you eventually start to learn enough Java to take advantage.
Well, Clojure has been ported to the CLR. Probably easier thant the whole jump to the JVM since you are a .NET developer. :)
http://github.com/richhickey/clojure-clr
yes, thats me!
Has not been much of a problem for me except for when I needed a stupid simple swing gui. thats when I had to go back and review java.
Scala is totally different language than java. So yes, you can of course use scala without knowing java ( but of course you have to learn scala first). Even though, knowing java (or any OO language) + some functional language makes it easier to understand scala.