LuaJ

I’m not super well-versed in many things, but I’ve been trying to wrap my head around LuaJ so I can use it for my next project. I want to be open to modding extensively and have a lot of flexibility with what can be done, since it’s incredibly varied. It’s an open-world game that focuses on you surviving and taking care of your family while also managing resources in preparation for the next winter. There are lots of things to do and it will be very involved with towns, factions, bounty hunters that will hunt you down if you get on their bad side, etc.

So I looked to LuaJ. My intention was to be able to expose some of the methods inside of my programming while also calling and in part relying on what is written inside of the lua script. However, I have been having trouble even figuring out how to make this work at all. Or even figuring out how in the world to make LuaJ work aside from printing Hello, World onto the console. I’ve been battling with this for so long I’ve lost track, and it’s seriously holding me back from actually being able to work on my game at all.

So I guess my question would be: how do I interweave LuaJ into my project so they work in sync?

Any assistance would be greatly appreciated.

So I’ve been doing some Lua integration too. I use Kahlua and I’ve never used or even looked at LuaJ but hopefully the functionality will be similar. I mainly integrate through adding “libraries” (tables of functions) to the Lua environment. Example - A (heavily shortened) library for programming ship behaviour. I know the casting is bit messed up but I couldn’t see a better way of doing it:


public class ShipLib {
    private final Ship ship;
    
    public ShipLib(Ship ship) {
        this.ship = ship;
    }
    
    /**
     * 
     * @param callFrame
     * @param nArguments
     * @return 
     */
    public int Ship_thrust(LuaCallFrame callFrame, int nArguments) {
        if(nArguments < 2) {
            throw new IllegalArgumentException("Less Than 2 Arguments");
        }
        int index = (int) (double) (Double) callFrame.get(0);
        float thrust = (float) (double) (Double) callFrame.get(1);
        ship.getThruster(index).setThrust(thrust);
        return 0;
    }
    
    /**
     * m
     * @param callFrame
     * @param nArguments
     * @return 
     */
    public int Ship_position(LuaCallFrame callFrame, int nArguments) {
        callFrame.setTop(3);
        callFrame.set(0, Double.valueOf(ship.getPosition().getTranslation().getX()));
        callFrame.set(1, Double.valueOf(ship.getPosition().getTranslation().getY()));
        callFrame.set(2, Double.valueOf(ship.getPosition().getTranslation().getZ()));
        return 3;
    }
    
    /**
     * m/s
     * @param callFrame
     * @param nArguments
     * @return 
     */
    public int Ship_velocity(LuaCallFrame callFrame, int nArguments) {
        callFrame.setTop(3);
        callFrame.set(0, Double.valueOf(ship.getVelocity().getX()));
        callFrame.set(1, Double.valueOf(ship.getVelocity().getY()));
        callFrame.set(2, Double.valueOf(ship.getVelocity().getZ()));
        return 3;
    }
    
    public KahluaTable getLib() {
        KahluaTable table = Computer.getTable();
        table.rawset("thrust", new JavaFunction() {
            @Override
            public int call(LuaCallFrame callFrame, int nArguments) {
                return Ship_thrust(callFrame, nArguments);
            }
        });
        table.rawset("nthrusters", new JavaFunction() {
            @Override
            public int call(LuaCallFrame callFrame, int nArguments) {
                return Ship_nthrusters(callFrame, nArguments);
            }
        });
        table.rawset("position", new JavaFunction() {
            @Override
            public int call(LuaCallFrame callFrame, int nArguments) {
                return Ship_position(callFrame, nArguments);
            }
        });
        table.rawset("velocity", new JavaFunction() {
            @Override
            public int call(LuaCallFrame callFrame, int nArguments) {
                return Ship_velocity(callFrame, nArguments);
            }
        });
        return table;
    }
}

So I call getLib() and add that table to the runtime for scripts that involve doing things with a ship. Then that script can call these methods.

Getting functionality programmable in Lua is even easier. Just a matter of running a script ehen you want to do something.

I’m not sure of the similarities between the two, so I couldn’t say. I guess I’ll check out Kahlua and see if I can’t compare them.

I suspect that using JavaScript will get you to a better place in the long run.

Cas :slight_smile:

(This is a justification of my using Lua over JavaScript in the hope that it will help to enlighten the OP, however similar or dissimilar his or her situation is. Please, users of JavaScript do not read in any offence. Please users of Lua, do not read in any offence that I felt I had to put this warning message. etc. etc. etc.)

I used Lua over JavaScript for, primarily, two reasons.

  1. I personally dislike the syntax of JavaScript and my understanding is that I am not entirely alone in holding this opinion.

  2. My requirements required me to rewrite several of the underlying standard libraries in whichever scripting language (it is a scripting language in that I would have used it for scripting) I used.

Firstly (I believe that) Lua has a much smaller standard library than JavaScript meaning less work for me.

Secondly I guessed that any implementation of Lua would allow me greater access to modify the standard libraries (due to Lua’s “everything’s a table” nature) than any implementation of JavaScript, meaning easier work for me. Although I never tried this in JavaScript, I feel vindicated in my view since when I went into Kahlua, it was very easy.

I don’t know if you’re willing to consider other scripting languages, but I’ve used JRuby with Slick and libGDX, and it works really well for me. It’s got very similar syntax to lua, and you get all of the great Ruby stuff. A little before libGDX 1.0, I wrote a short tutorial on creating a platformer in libGDX with JRuby. Here is the repository: (https://github.com/kabbotta/LibGDX-and-Ruby). I’m now using JRuby with libGDX 1.0.1, and it is great.

I think you should really go with Lua. It’s a -very- fast language, very simple, and from my experience using it with Java is really easy.