Search code examples
javalualuaj

LuaJ (or any other Java LUA interpreters) - Can you call a LUA script from Java without waiting for it to return?


I've been writing an RPG game engine for Android, and I've gotten to the point that I think I need to add a scripting language for my Battle Engine. Its just too complex to get by on XML configuration driving the game content, like I've been doing previously. It seems like LUA is the scripting language of choice by the game development community, so I've been doing some research. Since the Engine is written in Java, I'm thinking about using LuaJ as my LUA interpreter, since I found some old threads that indicate it's probably the best Java LUA interpreter, but if anyone has a more current opinion on a better interpreter, I'm all ears.

Anyway, I need to be able to call a LUA script from Java, and continue game thread execution without waiting for the script to return. Is this possible with LuaJ or any other popular Java based interpreter? I know I could always put the script call in its own thread, but I could potentially have many scripts active at once, and it's a waste, since I don't need a return value anyway.


Solution

  • If you want anything to run parallel to your game thread you have to put it in its own thread.

    You can use some kind of Executor (http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Executor.html, e.g. the ThreadPoolExecutor) if you don't want to start a thread for every call.

    Like so: https://stackoverflow.com/a/14286452/3759173