Scripting in Java [closed]
Asked Answered
W

11

8

Me and some friends are writing a MORPG in Java, and we would like to use a scripting language to, eg. to create quests.

We have non experience with scripting in Java. We have used Python, but we are very inexperienced with it. One of us also have used Javascript.

What scripting language should we use? What scripting language should we not use?

Whoso answered 17/10, 2008 at 9:23 Comment(0)
A
5

Java supports a variety of (scripting) languages, some are listed in Wikipedia here and here. You probably should choose language with powerful DSL and metaprogramming capabilities, such as Clojure.

But if you need something simpler, JavaScript might be a viable alternative.

Affaire answered 17/10, 2008 at 9:38 Comment(0)
T
9

I'm responsible for a fairly large hybrid Java/Jython system. We use java for core API development, then wire Java objects together using Jython. This is in a scientific computing environment where we need to be able to put together ad-hoc data analysis scripts quickly.

If I were starting this system from scratch today, I would not choose Jython as the scripting language. I like Python fine, but I frequently encounter awkward mismatches between the Python type system and the Java type system. For example, if you just want a hashtable, should you use a Python dictionary or a Java HashMap? The decision might be different depending on whether you are just using the data structure locally in Python code or passing it across the Java boundary. Jython does a certain amount of type coercion for you, but it's not perfect. It's annoying to even have to think about issues like this when the purpose of using a scripting language in the first place is to enhance your productivity.

I assume JavaScript or JRuby would have similar issues. Today I would choose a scripting language that is specifically targeted to the JVM and leverages the Java type system. The obvious candidates are Groovy and Beanshell; Groovy seems to have been picking up momentum lately so I'd look most closely at it.

Twickenham answered 18/10, 2008 at 14:23 Comment(1)
If you were to start from scratch in 2012 would you approach the "glue components together" differently?Tolmach
L
7

I agree with Viktor's Jython suggestion. Other than that and JavaScript (which you've mentioned, and is built into Java 6+ via the javax.script package), Groovy and JRuby are also worth looking at too.

By the way, you should look at Wyvern, also an MMORPG written in Java and using Jython for scripting. Steve Yegge, its author, has much to say about it from time to time. :-)

Legislature answered 17/10, 2008 at 9:37 Comment(0)
A
5

Java supports a variety of (scripting) languages, some are listed in Wikipedia here and here. You probably should choose language with powerful DSL and metaprogramming capabilities, such as Clojure.

But if you need something simpler, JavaScript might be a viable alternative.

Affaire answered 17/10, 2008 at 9:38 Comment(0)
D
4

How about Jython?
http://www.jython.org/Project/

Debonair answered 17/10, 2008 at 9:30 Comment(0)
B
4

what about creating your own specialized scripting language? If your app is written with java, you can use ANTLR (http://www.antlr.org/) to create your language parsing code.

The reason I say this is because a general purpose scripting language may provide too much power (because the script it to be used for quests only i assume).

But if making your own language is too hard then any of the above suggestions works - you just have to figure out how to bind the game's runtime into the script. I also suggest Lua (http://www.lua.org/) as another choice that lots of games use.

Brahmi answered 17/10, 2008 at 10:19 Comment(0)
S
3

Short version

Don’t use a scripting language! Instead focus on configurability (which is something that a non-programmer can do well).

Longer version

One oft-used argument in favour of having a scripting language is that it allows for lesser programmers to more trivial tasks. Don't belive this, it will not save you any time, since trivial tasks are already accomplished by real programmers in no time. Aim for configurability instead of scripting, and you will have a much lower risk of bleeding over complex algorithms and concepts into the incapable hands of game designers. :)

Lack of hotswapping (edit-and-continue) would have been a reason to implement a scripting language in an MMOG (you don’t want to reload the whole game for a minor code change), but using Java, with built-in hotswap, you really have no reason for adding a scripting language on top.

I have spent years pondering these questions; in the day I implemented a complete scripting language, IDE, VM, debugger, etc for an MMOG myself. Since, I have grown wiser.

If you still choose to go down the infinitely crappy path of no return, keep the following in mind.

  • Pick a mature language which has been around for a while.
  • Auto testing, debugging and editing will suck bigtime until you make your own tools/plugins/start hacking around in the VM.

To date, I have never seen a DSL that improved the situation (getting a more maintainable product). Myself, I integrated Python into my indie game engine, but eventually came to my senses and ripped it out. "Stackless Python" is just a way of saying "unmaintainable but fast". Please, anyone correct me if I'm wrong?

Skysweeper answered 8/5, 2009 at 8:10 Comment(4)
There is one good reason for including a scripting language, namely where you need to be able to add new functionality (which hence cannot be configured) without redeployment.Tolmach
But Java has support for (re-)loading classes, and using some thought and reflection will take you a long way. If necessary.Khan
You still need to compile the revised Java classes.Tolmach
Compiling should an be infinitesimal issue compared to incorporating a scripting language, in all the ways I can think of.Khan
M
1

You have quite a few options:

Possibly even BeanShell (http://www.beanshell.org/)

I'm a fan of Python myself so I'd recommend Jython, but they're probably all reasonable options.

Multiplication answered 17/10, 2008 at 9:39 Comment(0)
B
1

I would have to recommend Javascript for this purpose. Mozilla Rhino http://www.mozilla.org/rhino/ is an excellent implementation that would fit your needs perfectly.

I recommend Javascript over Jython or JRuby because of familiarity. Trivial Javascript follows a very familiar syntax that anybody can use. However if someone wants to do something more intense, Javascript is a very powerful functional programming language.

I regularly use Groovy and Ruby professionally and believe that their purpose is best for writing parts of applications with particularly complex logic where Java is cumbersome to write. Javascript is a much better choice as an embedded, general scripting language to use in a game. I haven't used Python, but it's syntactically similar to Ruby and I would believe it's purpose would also be similar.

Blackpoll answered 17/10, 2008 at 16:21 Comment(0)
F
1

LuaJ seems to be a nice way to embed Lua into Java:

http://sourceforge.net/projects/luaj/

Ferrand answered 24/3, 2010 at 13:39 Comment(0)
S
0

I am a big fan of Python/Jython due to the clean syntax - which may suit you if you have some python experience.

Otherwise Groovy which is based on Java syntax and may be an easier learning curve if most of your developers are Java guys. It also has the advantage of closer ties with the Java language and libraries.

Beanshell is good if you have simple scripting in mind - it doesn't support classes. However I don't think it has had any support over the last few years (the JSR seemed to kill it off...) so is perhaps a bad choice if support is important to you.

Shulman answered 17/10, 2008 at 12:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.