Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Ziphnor

[java] Java as a scripting language: using the JNI

This topic is 6126 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, im considering using Java as a scripting language for AI, dialogue and probably effects from spells etc. in a RPG(well, i will probably never finish it, you get the idea In the game ill have the main game logic which manages a bunch of GameObjects, which can be a character or a door for example. All intelligent GameObjects will call an AI script once in a while. Having that script as a Java class isnt a problem as far as i can see when using the JNI. But this this script will want to query the gamelogic back for information, it might want get a handle(i dont want to bother "shipping" objects back and forth) to the nearest hostile GameObject, determine its type calling another method with the handle, and set an AI state. ie, something like this: int handle = getNearestEnemy(); String type = getType(handle); if(type == "DRAGON") setAIState(AI_FLEE); Im a bit confused as to how i expose these methods(int getNearestEnemy() & String getType(int handle)) to the script. Im looking for good places to find info on the JNI, but java.sun.com seems to have some slightly obsolete information, im not sure its tutorials have been updated, and anyway, they confuse me a little So anyone know a place to find a tutorial for something like this?(i already read the old Gamasutra article) Also, for very good reasons i only want the script to have access to a few select of the core java classes(String, Vector etc, but not AWT/Swing or anything like that!). Is such restrictions possible?

Share this post


Link to post
Share on other sites
Advertisement
IIRC, JNI still requires a JVM/JRE. I think you might be better served by using an alternative that you can bundle with your program as a library/dll file. java-script is a good candidate - the js string class is more versatile than java, js arrays are really vectors and the other default classes provide other standard functionalities. The source code is available too - check out the Spider Monkey project at mozilla.org. Other implementations are available too. Look up ECMA script at google.

Share this post


Link to post
Share on other sites
I''m currently developing a complete scripting system using the JNI and Java.

Ignore the above post, java is faster than any other scripting language you can use, and your scripters have countless references to learn java for their programming. Plus you don''t need to distribute the JVM with your game because if you have windows (Internet Explorer) then the system already has a JVM. (although you probably should ''cause you will probably have a better JVM than the IE one).


Anyways, Java put out a book on using the JNI. It''s very good refernece. Otherwise just keep searching the web. Eventually I will put out a tutorial on using the JNI for a scripting engine, but I haven''t gotten around to it. But you are not incorrect in thinking JAva would make a great scripting engine, because it does.

Share this post


Link to post
Share on other sites
quote:
Original post by shaft
I''m currently developing a complete scripting system using the JNI and Java.


I would be interested in seeing this. Have you got a demo?
quote:
Original post by shaft
Ignore the above post, java is faster than any other scripting language you can use, and your scripters have countless references to learn java for their programming.


Java isn''t a scripting language. My suggestion will get you up and running a lot sooner. If you don''t like js, use python, lua, etc. Why reinvent the wheel?

Share this post


Link to post
Share on other sites
quote:
Original post by LessBread
Java isn't a scripting language. My suggestion will get you up and running a lot sooner. If you don't like js, use python, lua, etc. Why reinvent the wheel?



True, Java is a compiled language, but the context in which we're talking about a scripting language here is a language that is not the primary language the app is written in but is used to extend the functionality of the application. And how is using an existing language, API, & VM re-inventing the wheel?

That said, Java is an excellent option thanks to its speed and large industry support. However, using JNI can complicate the design of your system. Here are some suggestions:
- Minimize the amount of JNI code you have to write. I would say keep the majority of the AI sub-system in Java and minimize the interacton with native code.
- If you are able to utilize Java 1.4, do so. The New I/O package offers ways to share memory between Java code and Native code, and can give you some definate speed increases over the pre-1.4 API.
- A descent tutorial is here, although I too recommend buying a book.

As for restricting what classes can be used, there might be some way to do this with a SecurityManager, but I have never tried so don't quote me on that.




"There is no reason good should not triumph at least as often as evil. The triumph of anything is a matter of organization. If there are such things as angels, I hope that they're organized along the lines of the mafia." -Kurt Vonnegut


Edited by - wayfarerx on February 22, 2002 5:20:32 PM

Share this post


Link to post
Share on other sites
quote:
Original post by WayfarerX
True, Java is a compiled language, but the context in which we''re talking about a scripting language here is a language that is not the primary language the app is written in but is used to extend the functionality of the application. And how is using an existing language, API, & VM re-inventing the wheel?


Looks like we have slightly different conceptions of scripting and scripting languages. This approach is not a complete reinvention of the wheel, but it does seem like extending java in a way that it wasn''t necessariy designed for. That''s an obstacle. Plus the jvm dependency also presents an obstacle. These obstacles must be great, otherwise I would expect to find more references to using java in this fashion. I suggested an alternative with fewer obstacles. That''s all. There are other languages expressly written for scripting - languages for which the engine source code is available which would make embedding them easier - as well as restricting their capabilities. I pointed out java-script, but there''s python too and CScript among others.

Ziphnor mentioned the sun tut seemed obsolete and was creating some confusion. I agree that a JNI book would be a worthwhile investment.

A security manager might work, but I would think it more likely that you will have to set up a base class that scripts must use in order to run in your app. Robocode does it this way - but robocode doesn''t appear to use JNI.

Share this post


Link to post
Share on other sites
As far as i know, usually creating a scripting language means creating a virtual machine and compiler etc.
Interpreting directly is supposedly very slow(im currently reading the book "Programming language processors in Java : compilers and interpreters", not that im intending to implement the whole thing myself, im just interested in how its done).
Isnt java-script interpreted directly? But perhaps its still fast enough? Anyway, i havent the faintest idea of how to go about embedding java-script in a C++ application, any links would be much appreciated. The big question is of course how easy it is to make script methods that calls back to where the script was started to query the game logic(those methods cant just be declared seperatly as they extract information from the running C++ app).

The good thing with Java, is that the syntax is very easy for me, and most others(Java is the language of choice for introducing programming to students it seems), but the same goes for java-script i guess. I dont think the JVM dependency is that much of a problem, arent you allowed to distribute the Sun JVM with your program?(all you need is jvm.dll or what ever its called as far as i understand).

I also had a look at Simkin, but that scripting language has an odd syntax, and it seems much more oriented towards xml and small mobile devices.
Btw, what about the Java micro edition, does that include JNI?

Regarding limiting the scripts to specific core classes, i was also intending to force the use of a specific base class, but that still doesnt prevent users from using weird parts of the java classes in the methods i call.

Anyway, thanks for answering.

Share this post


Link to post
Share on other sites
quote:
Original post by Ziphnor
As far as i know, usually creating a scripting language means creating a virtual machine and compiler etc.


That''s what I was thinking.
quote:
Original post by Ziphnor
myself, im just interested in how its done).


Me too. That''s why I wanted to see a demo of shaft''s implementation.
quote:
Original post by Ziphnor
Isnt java-script interpreted directly? But perhaps its still fast enough?


AFAIK, yes. IIRC some early Netscape Foundation Classes used Java to provide a means to "compile" the script the first time it ran so that it could be repeated more quickly. I don''t know where that stands viz current browser implementations. There are also server side implementations - so it must not be too glacial.
quote:
Original post by Ziphnor
embedding java-script in a C++ application, any links would be much appreciated.


FESI - Free EcmaScript Interpreter
SpiderMonkey (java-script-C) Engine
quote:
Original post by Ziphnor
The big question is of course how easy it is to make script methods that calls back to where the script was started to query the game logic(those methods cant just be declared seperatly as they extract information from the running C++ app).


A Bare Bones Tutorial There are three snippets of code demonstrating how to start up a vm, how to call C from js, and how to call js from C.
quote:
Original post by Ziphnor
The good thing with Java, is that the syntax is very easy for me, and most others(Java is the language of choice for introducing programming to students it seems), but the same goes for java-script i guess.


Don''t get me wrong. I think Java is great. java-script is easier because it''s weakly typed - "var" could be a number, a string or an object. java-script is more procedural than Java, but it has primative oop capabilities. As an embedded language the browser tricks wouldn''t be there of course - unless you built them and I guess that''s the goal. java-script offered regular expression string parsing before Java did - but Java does that now. Looking over the spider monkey stuff - I see that scripts can be compiled. Cool.
quote:
Original post by Ziphnor
I dont think the JVM dependency is that much of a problem, arent you allowed to distribute the Sun JVM with your program?(all you need is jvm.dll or what ever its called as far as i understand).


I don''t know about Sun''s redistribution policy. I would assume it is conservative and that they would rather distribute their JVM themselves. If all it takes for a jvm is one dll, I think I will have to rethink my understanding. I might be slightly off base.
quote:
Original post by Ziphnor
Anyway, thanks for answering.


Sure.

Share this post


Link to post
Share on other sites
I''d never even heard of using Java as a scripting language before this post.

I have some questions: When you do this, can the Java code do basically anything it could do in a regular Java application? In other words, can your C++ program limit/determine what the Java code does, or does it have no control over it? That would seem to me to be a major disadvantage.

Also, would it still use the standard Java library or whatever they call it?

And, how does the "script" interact with your program, in terms of things like having the script call an external function located in your C++ program?

~CGameProgrammer( );

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!