How to call Java function in lua coroutine?

I am developing a luajava game. When I call a java function in a lua coroutine, I get the error "Invalid method call. There is no such method." Here is the code

package com.soyomaker;

import org.keplerproject.luajava.LuaException;

import org.keplerproject.luajava.LuaState;

import org.keplerproject.luajava.LuaStateFactory;

public class Main {

    public static void main(String[] args) {
        LuaState luaState = LuaStateFactory.newLuaState();
        luaState.openLibs();
        try {
            luaState.pushObjectValue(new People());
            luaState.setGlobal("people");
        } catch (LuaException e) {
            e.printStackTrace();
        }
        luaState.LdoFile("res/script.lua");
    }
}

//=============================================================================

package com.soyomaker;

public class People {

    public void sayHello(String name) {
        System.out.println("hello " + name);
    }

}

//=============================================================================

print(people)

print(people.sayHello)

people:sayHello("Bill")

function run()

  print("========run========")

  print(people)

  print(people.sayHello)

  people:sayHello("Jobs")

end

local co=coroutine.create(run)

print(coroutine.resume(co))

      

Here's the result:

hello Bill

userdata: 040256B8

<b>function: 040252C0</b>

========run========

userdata: 040256B8

<b>function: 04026650</b>

false   Invalid method call. No such method.

      

I type "people.sayHello" twice, Suddenly I get different results.

How can I get it to work? Thanks in advance!

+3


source to share


1 answer


The colon unties you. There is no sayHello function with two arguments. By using a colon, you are sending "me" (people) and a string to Java code. You don't have a java method with two arguments. Just use period instead of colon in Lua to make it work.



0


source







All Articles