Java game engine, writing own scripting language? - java

I'm planning to create a simple Visual Novel game engine out of Java. I've developed for Java before but I have experience with other languages. What I would like to do, is to create my own scripting language that people can use with the engine to make the complete game out of a script, since Visual Novels are simple enough to do it, I think.
What I don't know is, is Java suitable for making such a scripting language? The way I imagine it is, the engine reads a script file in the game folder, and takes in the input and executes each command linking up to a certain function or something like that. I'm not sure how difficult theoretically that would be to pull off.
As you can probably tell I'm a newbie when it comes to this.
Thanks.

It sounds like you need a domain-specific-language (DSL). Here's an article on DSLs within Java
DSLs are small, expressive programming languages custom designed for
specific tasks. In this four-part series, Venkat Subramaniam
introduces the concept of DSLs and eventually shows you how to build
them using Java
and there's a related question on SO.
You can use Scala to write DSLs and run them on the JVM. However I would warn you that without Scala knowledge it's a (IMO) non-trivial task to do this.
Alternatively you can use a Java implementation of a scripting language together with the Java Scripting API. This will allow you to use an existing language and have it compiled into bytecode. The advantag eis that you're using an existing language. The disadvantage is that it's not specific to your particular needs.

Don't do it. Writing a new language is surprisingly complex. Designing grammar, parsing, error reporting, performance.
It's already there: Creating meta language with Java
Consider groovy, lua or jruby first. People will love you for using familiar syntax.

While it's technically possible to do this, I don't know if Java would be your best bet. While I don't know the scope or purpose of your project, I'd recommend PyGame as a game/scripting language. (You said that you have experience with other languages, so I assume that switching to Python is ok)

I think the above answers give nice directions.
I would like to offer some more ideas:
A. Provide a java "GAME API", which can be exposed via REST/SOAP web services and can have client classes generated from (for example, take a WSDL, and generate client classes from it).
B. Provide the API as a jar file, and have you game implement a File class loader that will load external jars, and use the classes developers will develop using the API.
Here is an example of how to implement such a class loader.
C. Use the ScriptEngine provided by java, to invoke scripts.
See here an example of how to invoke a JavaScript script from within java.

This question is old and i am a bit late for the party (though this question is not answered yet), but it is possible. From what i understand, this sounds like a job for Just Another Framework. Full disclosure: I am the developer of the Framework i am describing. I had a similar problem and wrote this framework to reuse this system, which worked for this kind of issue.
TL;DR
Do not use JAF, if you want your own, fully functional, perfect language.
Use JAF, if you just want to create a simple scripting part, that decouples parts of your code or if you want to evaluate "user input" scripts at Runtime (like in the topic of this question).
What I don't know is, is Java suitable for making such a scripting language?
You can do it with nearly every programming language. You should consider to use Javascript or Lua or [...] and put those into a file instead of creating a custom scripting language. Java has a place for those languages. You should also consider to maybe read .java or .class files from a folder and take those into your "Game-Engine". If you however want to provide your own language and no one can change your mind about it (that was my issue), JAF can help you with that.
JAF is a framework, which allows you to define custom scripting languages within Java, that may be evaluated and executed at Runtime. You give the Parser some Functions, Rules as well as the raw script and the result is a fixed set of instructions (in the form of consumers), that can be executed as many times as you like.
This is by no means another language build on top of java! It is meant to be a foundation for your language. It is only meant for creating small parts though. An example would be some sort of logic that should be interchangeable without stopping the application. This logic should be executed via small scripts and provided via a Database or (like in your case) from files located in a folder.
You can use a Parser, to parse "raw-scripts" that are created by the users. This parser takes Functions and Rules in the form of a Strategy-/Command-Pattern, which basically define your language. After parsing, a "Script"-Object is returned, that can be executed as many times as you want. You can also set values of the Script and therefor connect this Script to your real and running java-code.
The real thing i loved about this however is, that you can provide custom script elements through Rules and Functions, which basically directly connect to java. Just an example would be the printline function. This calls System.out.println();. As i said, just an example, but you can imagine that you can call any existing code from your Functions and Rules.
A (very) simple example of how to use this would look like this:
String rawScript = ... //your users script here
Parser parser = Parser.create(); // used to parse the script
parser.add(IOModule.getPackage()); // add packages of Functions and Rules
Script script;
try {
script = parser.parse(rawScript); // Create the Script
} catch (ParsingFailedException e) {
// Handle an error while parsing the script
// Do not continue, there is no script!
}
script.setValue("yourKey", /* Whatever should be available in your script */); // Inject outside variables for your Rules/Functions
try {
script.run(); // Execute the script
} catch (ExecutionFailedException e) {
// Handle an error at the scripts execution
}
I do not want to write a book about how to use this framework. Take a look at the wiki, it is better explained there.
The advantage of this is, that you can simply define your own language, by providing simple commands that parse specific pieces of the raw script. They are just "applied"; All the magic is hidden within the framework. It's explained a bit within the frameworks-wiki, but i am bad at explaining. So you can create your own little scripting language by providing the building-blocks of your language without requiring deeper knowledge of theoretical computer science.
It has some issues though. It is currently not compatibly with the JSR 223. Also, it is (of course) slower than playn java.

Related

Making an API or scripting language for game engine?

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.

Calling Java methods and classes from Perl script

I have a quite huge Java class that has several imported packages and libraries (related to natural language processing). I want to call some specific methods of my Java classes and get results back using a Perl script. How should I do this? I guess the Inline::Java is not suitable for my purpose since the Java code is quite large to be incorporated at one place.
Why is Inline::Java not suitable? You don't need to provide access to all the bits of your Java API. You can write short methods that call into your Java stuff. A small Java adapter layer can show up in your Perl code to give you the access you need. Have you tried it yet?
How big is this Java code base? I've been on projects that easily integrated big Java SDKs (although I wasn't that one doing that part).
I had ran into similar situation lately. The best solution which worked for me was to use apache thrift service and expose the required methods through it. These method were then in turn consumed by the client written in perl.

How to write a script that's parseable in both Java and C++

We've got Java and C++ implementations of our product, which is a distributed messaging system.
I'm writing a framework to run a system test across multiple servers. I need our "test coordinator" process to send instructions to each server telling it what to do (e.g. send 100 sample messages, or wait for a message, etc).
I'd really like to implement this by sending a script to each server containing its instructions. This allows me to create dumb test servers, with all the intelligence embedded in the test instructions.
If all my servers were Java I'd write this in Groovy or a similar language. But I'd like our C++ implementation to parse the same script. I'm wondering if I could execute Javascript to call Java/C++ classes to send messages etc.
Is there a neat way of doing this?
If all else fails I'll create an XML format to declaratively contain the test parameters (rather than imperatively containing the test instructions). But this would require the test servers to contain more test-specific intelligence than I'd like.
Any advice gratefully received.
Maybe have a look at LUA. It is well supported in C++ and it seems like there is support for Java as well (see this question)
There are JavaScript parsers for C++ and Java available, so that's definitely something you could use; I don't think that it's a good idea however: the Javascript code can of course interact with your Java code and your C++ code; the problem is, if you want your Javascript code to be ignorant of whether it runs on the C++ or Java basis, it doesn't actually make much sense to call Java/C++ from it, since these calls are again language- (and even engine-)specific.
The simpler solution might be to use a declarative format as you say, but I wouldn't go for XML unless the data needs to be exchanged with a broader audience; instead I'd use e.g. JSON, which is supported very nicely on Java and C++
The best solution (albeit probably also the one involving the highest effort to develop) is probably to develop your own DSL (domain specific language), and parsers for it in both C++ and Java.
This is the kind of inter-operability WSDL was originally designed to facilitate. I'm not sure why you'd want to invent an XML format to do what SOAP already does. I mean, you could, but it'd be a maintenance nightmare for the next guy. Plus, introducing another programming language (which is client side, unless you want to add NodeJS into the mix) to act as a glue layer between the two increases the complexity of the system.
What I would do is:
define a high level language agnostic interface
create implementations for the interface in Java and C++
If you're sending commands to the servers, it may be appropriate to look into using the Command pattern to encapsulate the set of known commands (which I assume is enumerated in your requirements). You can use Batch Commands using Composite.
Then your "Test Coordinator" would build a batch command, send it to the server of choice using the declared service, and your implementations could process those commands in language specific ways. Depending on the types of commands your system has, it may be appropriate to have each implementation delegate to Ant or Make.
You could consider using CORBA too... :)

Java and Scripting languages

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

creating own scripting language in Java desktop app?

I have a simple web application testing desktop application. I am wondering how I can create my own scripting language for it (Domain Specific Language).
The purpose of this is to offer a very intuitive scripting even for non programmers, and able to define user scenario specific details.
I hope this is clear, basically the question is, how do I create a simple-to-use scripting language that can be translated into Java code.
Perhaps, they can use netbean's IDE to write the scripts.
For ex)
my script:
load "http://www.google.com";
click "Search" button;
java code:
browser.navigate("http://www.google.com";
browser.wait
browser.element("Search").click();
The best tool/framework for languages which extend beyond very simple grammars is to use ANTLR. Its a full featured lexer+parser combination, great Java integration, and tons of examples with good documentation.
Do realize that this isn't a trivial project to get right. You should use an existing language if your time is limited
The best solution would be to reuse one the existing scripting languages for Java; for example BeanShell. Implementing your own scripting language would be whole a lot of work. And if you've never done any language design and implementation work before the results are likely to be "less than stellar".
Perhaps, they can use netbean's IDE to write the scripts.
Expecting users of your application (especially non-programmers) to install and use a particular IDE sounds like a really bad idea.
... that can be translated into Java code.
Translating to Java code complicates things as well. It is probably better to interpret a scripting language and access Java APIs using a combination of staticly typed method calls and (only where necessary) the Java reflection APIs.

Categories