Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Luaj attempt to index ? (a function value)

Tags:

java

lua

luaj

I am trying to compile Lua code that has two functions which I want to invoke and get some information from but when I use invokemethod on the LuaValue object, I get this error

LuaError: attempt to index ? (a function value)

The code is inside a LuaScript class I created for convenience

This method is first called to compile the file

public void compile(File file) {
    try {
        Globals globals = JmePlatform.standardGlobals();
        compiledcode = globals.load(new FileReader(file), "script");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

And then this is used to invoke the function getSameTiles from my lua script

public Object invoke(String func, Object... parameters) {
    if (parameters != null && parameters.length > 0) {
        LuaValue[] values = new LuaValue[parameters.length];
        for (int i = 0; i < parameters.length; i++)
            values[i] = CoerceJavaToLua.coerce(parameters[i]);
        return compiledcode.invokemethod(func, LuaValue.listOf(values));
    } else
        return compiledcode.invokemethod(func);
}

The error LuaError: attempt to index ? (a function value) occurs at the line return compiledcode.invokemethod(func); where "getSameTiles" is passed as the string for func

This is my Lua code

function getSameTiles()
    --My code here
end
like image 962
Nicolas Martel Avatar asked Aug 27 '26 05:08

Nicolas Martel


1 Answers

There are a couple of issues that needed fixing.

Firstly, in lua, load() returns a function which you'd then need to call to execute the script.

Secondly, what the script does is add a function to the global table _G. In order to invoke that function you'll need to get the function from the Globals table and call that.

The following code does this

Globals globals = JmePlatform.standardGlobals();

public void compile(File file) {
    try {
        globals.load(new FileReader(file), "script").call();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

public Object invoke(String func, Object... parameters) {
    if (parameters != null && parameters.length > 0) {
        LuaValue[] values = new LuaValue[parameters.length];
        for (int i = 0; i < parameters.length; i++)
            values[i] = CoerceJavaToLua.coerce(parameters[i]);
        return globals.get(func).call(LuaValue.listOf(values));
    } else
        return globals.get(func).call();
}
like image 88
Alex Avatar answered Aug 29 '26 18:08

Alex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!