lua

Lua bindings


Keywords
library, binding, development, application, desktop, game, gamedev
License
MIT
Install
dub fetch lua --version 0.5.0

Documentation

luaexd

Lua bindings for DLang with extra wrappers.

Creating a new LuaState

To create a new LuaState you do newState(), this will invoke the bindbc backend to bind lua as well return a new LuaState. If lua is already bound, it will just return a new LuaState.

LuaState state = newState();

//create table
LuaTable table = state.newTable("MyGlobalTableName");

// Set "item" in table to "value"
table.set!string("item", "value");

state.executeString(q{

local val = MyGlobalTableName.item;
print(val);

});

(WIP) binding to classes

classes/structs that gets bound NEEDS an function with the signature void instantiate(Variant[] params).

Example (ManagedUserData!T):

class MyBoundClass {

    // Pretty much the constructor.
    // THIS WILL CHANGE IN THE FUTURE.
    void instantiate(Variant[] params) {

        // Empty constructor.
        return;
    }

    @Expose
    int meaningOfLife = 42;

    @ExposeFunction
    int getMeaningOfLife() {
        return meaningOfLife;
    }

    @ExposeFunction
    int getMeaningOfLifeTimes(int val) {
        return meaningOfLife * val;
    }
}

void main() {
    LuaState state = newState();

    ManagedUserData!MyBoundClass boundInstance = state.bindUserData!MyBoundClass;

    state.executeString(q{
        local classInstance = MyBoundClass()
        // get value.
        print(classInstance.meaningOfLife);

        // call function.
        print(classInstance.getMeaningOfLife());

        // call function with parameters.
        print(classInstance.getMeaningOfLifeTimes(2));
    });
}

Example (mixin):

class MyBoundClass {

    // Insert bindings.
    mixin luaImpl!Element;

    // Pretty much the constructor.
    // THIS WILL CHANGE IN THE FUTURE.
    void instantiate(Variant[] params) {

        // Empty constructor.
        return;
    }

    @Expose
    int meaningOfLife = 42;

    @ExposeFunction
    int getMeaningOfLife() {
        return meaningOfLife;
    }

    @ExposeFunction
    int getMeaningOfLifeTimes(int val) {
        return meaningOfLife * val;
    }
}

void main() {
    LuaState state = newState();

    MyBoundClass classInstance = new MyBoundClass();

    // Bind to lua.
    classInstance.bindToLua(state);

    state.executeString(q{
        local classInstance = MyBoundClass()
        // get value.
        print(classInstance.meaningOfLife);

        // call function.
        print(classInstance.getMeaningOfLife());

        // call function with parameters.
        print(classInstance.getMeaningOfLifeTimes(2));
    });
}