1

So, I don't really know how to formulate this correctly but I'll do my best.

I am writing a game engine (not really for anything, I am trying a lot of ways to get certain things working to practice with ways of programming). And for this I want scripting. And I know how to add scripts from within Java but I have seen game engines that use multiple languages.

So what I want is to add python scripts that are run from with the Java process and can interact with Java object.

Like this Java Object that has some parameters (just an example)

public class Entity {

    Script script = new Script ( "example.py" );

    loc_x = 0;
    loc_y = 0;
    loc_z = 0;

    public void update () {
        script.run ();
    }
}

With the python script being this

loc_x += 1
loc_z = loc_x

or

entity.loc_x += 1
entity.loc_z = entity.loc_x

I just have no way how to implement this. If this question has already been asked than please show me. If Runtime.getRuntime ().exec ( "example.py" ); is my best shot for this than that's fine. In that case I just want to know how to share those parameters.

Also, if another language (for example; LUA) is better for something like this then that's also fine. I am just completely blank on this subject.

4
  • It's more usual to integrate Python into programs written in C, or other languages that can naturally interface with the C API. With Java, the options for embedding Python are more clunky or outdated (like Jython). Commented Oct 9, 2018 at 19:54
  • So would you recommend me using another language than Python or switch () from Java to another language like C# or so? Commented Oct 9, 2018 at 19:58
  • I don't know what people do these days when they want scripting in a Java program. Maybe they just write more Java. Maybe they use Scala or some other JVM language. Maybe they use Jython. Maybe they embed CPython with something like JNI or JNA. There are a bunch of options, and I haven't tried most of them. Commented Oct 9, 2018 at 20:21
  • Scala is a new language to me. I looked it up and has amazing integration with java but it didn't really make me as excited as python does... After some further searching I found something real easy and nice. Sometimes saying something out loud or (on the internet) posting a question seems to make the difference for one's brain to start acting. I'll post my own answer soon in case anyone else needs this in there project. Commented Oct 10, 2018 at 22:01

1 Answer 1

2

So actually this is quite simple to do with Java having this built in from the box.

Java has this thing that is called 'ScriptEngineManager'. To use it you just do the following:

ScriptEngineManager sem = new ScriptEngineManager ();
ScriptEngine se = sem.getEngineByName ( "python" );

Now there are a few ways to runa script. Simple call the se.eval () method. You can give this either a String or a Reader and this way it will run the script.

Now to make it have some variables simply use the se.put method. You need to give this two parameters; a String and an Object.

For example:

se.put ( "entity", entity ); // with entity being defined earlier

The onlt thing to keep in mind is that this script manager does not have built in python support. You need to either create your own ScriptEngine for this or use third party software. I found jython and this seems to be working prety well. If you download the standalone jar and put that in your classpath it works. No need for any function calling.

Now in the Script you can call any public member of entity. All the objects, values and those sub-objects get passed through to the script.

My end-code:

Entity class

public class Entity {

    String source =
            "entity.loc_x += 1\n" +
            "entity.loc_z = entity.loc_x";
    ScriptEngine se;

    loc_x = 0;
    loc_y = 0;
    loc_z = 0;

    public Entity () {
        ScriptEngineManager sem = new ScriptEngineManager ();
        se = sem.getEngineByName ( "python" );
        se.put ( "entity", this );
    }

    public void update () {
        se.eval ( source );
    }
}

I hope I helped anyone with this. It was pretty fun tinkering with all of this.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.