Sign in to follow this  
HellCreator

[java] Scripting engine in Java - Do We Really Need Them?

Recommended Posts

HellCreator    133
Question to Java Guru! Need some lightweidth script engine like lua to embed into java applet and app OR may be java have one ? I know about using javac via System at run time and then loadin via reflection but it is imposible in applet. Realy do I need this stuff ? Just because recompilation less - YES! Found this http://www.beanshell.org comments if any? [Edited by - HellCreator on February 15, 2005 2:33:47 AM]

Share this post


Link to post
Share on other sites
markr    1692
Well, you never really "need" a scripting engine because it's possible to do anything you can with a scripting engine, with precompiled code.

You might find it advantageous under some circumstances to use a scripting engine though.

Regarding using javac in an applet, no you can't, but you *can* run a server-side script which runs javac and returns the class file(s) (perhaps in a jar).

You should look at Rhino (javascript engine written in Java, uses reflection) to achieve what I think you're trying to.

Mark

Share this post


Link to post
Share on other sites
Shabadoo    131
Having scriptable events/code makes the software development cycle easier. You can adjust a script without having to recompile the entire application. Once you have finalised the script you can either hard code it or leave it scripted. Scripting also allows for a lot of extensibility in a system as well.


The best scripting engine I have found for use in Java is Beanshell (which you already found), as it integrates seamlessly.

[edit] spelling

Share this post


Link to post
Share on other sites
HellCreator    133
Thanks for all replays
I found verry interesting Rhino and Beanshell.
The mission is - do not force the end user to dowload and ah to install libraries required to app or applet and do not doing any server side action because engine may be oriented for runing on client mashine only without installing any servers localy.

Share this post


Link to post
Share on other sites
HellCreator    133
Quote:
Original post by CaptainJester
Or you could just use XML. You can store anything you want in XML, so it would be a good replacement for a scripting engine. Plus Java provides an API for loading XML already.

but - do java provide proccessing stored data as class object or class function
it is what the scripting do?

Share this post


Link to post
Share on other sites
Luctus    584
Quote:
Original post by HellCreator
The mission is - do not force the end user to dowload and ah to install libraries required to app or applet and do not doing any server side action because engine may be oriented for runing on client mashine only without installing any servers localy.

In case you decided against jython because of those reasons you might want to reconsider. Jython doesn't put any prerequisies on client or server, you just have to pack a 700k library together with your app/applet.

Besides, python is an awesome language =)

Share this post


Link to post
Share on other sites
CaptainJester    523
Quote:
Original post by HellCreator
but - do java provide proccessing stored data as class object or class function
it is what the scripting do?


I believe you can do that with reflection. However, I am not sure I understand what you are saying. Can you explain a bit more?

Share this post


Link to post
Share on other sites
HellCreator    133
Quote:
Original post by CaptainJester
Quote:
Original post by HellCreator
but - do java provide proccessing stored data as class object or class function
it is what the scripting do?


I believe you can do that with reflection. However, I am not sure I understand what you are saying. Can you explain a bit more?


i mean the maner just how it is lua does for C

Share this post


Link to post
Share on other sites
CaptainJester    523
Quote:
Original post by HellCreator
Quote:
Original post by CaptainJester
Quote:
Original post by HellCreator
but - do java provide proccessing stored data as class object or class function
it is what the scripting do?


I believe you can do that with reflection. However, I am not sure I understand what you are saying. Can you explain a bit more?


i mean the maner just how it is lua does for C


I have never used Lua or any other scripting engine. You would have to tell me what Lua does.

I assume you want to provide data to the game that affects what objects are placed where, what enemies are around, the abilities and locations of those objects and enemies, etc...

If this is the case, you can do all that with XML and Reflection. Plus, this way you don't have to add extra libraries, you can just use what is provided with the Java core libraries.

Share this post


Link to post
Share on other sites
HellCreator    133
To CaptainJester
Let say in app You have

int a=1;
int b=2;
int c;
scriptengineVM.populate(a);
scriptengineVM.populate(b);
scriptengineVM.populate(c);
//and script
String script="c=a+b";
//after call to
scriptengineVM.execute(script);
//here I expect to c=3


second variant

String script="int d=1,e=2,f;f=d+e;"
scriptengineVM.execute(script);
//then in my app I can do some like this
int f=scriptengineVM.getglobal("f");


I think idea is simple
the script can be placed in some text file
variables can be objects and even classes "Lightweight classes"

Lua may be compiled with Your app or just linked

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
The short answer is that Beanshell does everything you want. Trust me: use bsh, and ignore the others. When you've used bsh for a while, and are much more aware of what you are trying to do, and have time to spare, you might want to consider other alternatives. OR if you hit a bug in the current version of bsh (although a major new version is due soon, so you might find your bug fixed soon anyway).

The one problem is that you might hit the performance bug in the current version of bsh. Thousands of people are using it without being bitten by this, but it caused aggravation in a game project we did once - we had to convert two scripts to java class files (took 30 seconds of copy/paste!) to workaround it.

Quote:
Original post by markr
Well, you never really "need" a scripting engine because it's possible to do
anything you can with a scripting engine, with precompiled code.


Many commercial games *could not have been released* without using scripting langs. Whilst I appreciate your point, I count that firmly as "needing" them. They are essential in very many game projects.

Quote:

Regarding using javac in an applet, no you can't, but you *can* run a server-side script which runs javac and returns the class file(s) (perhaps in a jar).


It's a nice idea, and one I would suggest to the OP if bsh didn't exist.

Quote:

You should look at Rhino (javascript engine written in Java, uses reflection) to achieve what I think you're trying to.


Rhino is fine, like Jython, but both of them suck for this particular scenario. In gross over-simplification they are for non-java programmers who want to interface with java libraries, or for java programmers who want to branch out into other languages gradually. Bsh is much much more efficient use of programmer time and much less dangerous in the long run (can be compiled to java class files trivially in most cases if yuou ever need more speed).

DOn't touch XML with a bargepole for scripting - this is a flagrant abuse of XML and is a curse that we should all do our best to stamp out as rapidly as possible :P. I've seen it done on many projects and it nearly always leads to either tears or the unanimous conclusion "well, this was a really stupid idea" by the end of the project. If you wake up and smell the roses you'll always find there are 3 or 4 (or 20) better ways to do any scripting that you were tempted to do in XML...

All MHO, of course, but I've used most of the above options in games, several times each, and ditto in business logic situations.

redmilamber

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
If you don't need any esoteric type features (or even if you do) why not do something like:



public class GameManager(
public GameManager() {}

public int accessMethod1(..) {}
public int accessMethod2(..) {}
public int accessMethod3(..) {}
public int accessMethod4(..) {}
public int accessMethod5(..) {}
public int accessMethod6(..) {}
public int accessMethod7(..) {}
}

public class Scripts {
public static void script1(GameManager gm, ...) {}
public static void script2(GameManager gm, ...) {}
public static void script3(GameManager gm, ...) {}
public static void script4(GameManager gm, ...) {}

}



Then just compile all your classes, stick in a jar and upload to your webserver.

Share this post


Link to post
Share on other sites
HellCreator    133
To Anonymous Poster
Thanks for supporting my vew
Finaly I will stoping at Beanshell :) For my current project
but to the future I think to write one mine may be just to get some experience.
About Your suggestion its seems like you need foreach variant wrap function
and foreach posible script function tons of presets or combinations hardcoded anyway,it is seems to me like BV or TK tecnique of "incapsulatin private variables or functions or may be predefined event driven stile" in some cases You right it may be enougth well but in real engine this tip can be aplied only to adjust and procceed KNOWN at compile time variables and functions.
correct me if I missed something.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
I use janino. http://janino.net/

It eats usual java code and compiles it on-the-fly down to byte code.

The pros are:
-java syntax
-you can just compile the stuff at the end... no need to ship the lib
-compilation is damn fast
-execution is as fast as it can get
-very active project (bugs are often fixed within 2 weeks or even faster)

The cons are:
-rather bogus error messages
-no 1.5 language features
-it's NOT a reference compiler (eg it doesn't check "definite assignment" (JLS2 16))
-it often has problems with bigger classes

It's not that bad. Ignore the messages and just look at the line number. There is no real need for 1.5's stuff...

Well, the last con can be a bit annoying, but usually you don't want to compile a complete game that way (it works if it's small one). However, there aren't problems with usual size script chunks (<800 lines).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this