3

I'm developing a game by luajava.When I call a java function in a lua coroutine,I get an error "Invalid method call. 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 is 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 print the "people.sayHello" twice,Unexpectedly,I get different results.

How can I make it work? Thank you advance!

LU RD
  • 34,438
  • 5
  • 88
  • 296
wp_g4
  • 31
  • 3

1 Answers1

0

The colon is messing you up. There is no sayHello function with two arguments. By using a colon, you are sending 'self' (people) AND the string to the java code. You have no java method taking two arguments. Just use a dot instead of a colon in the Lua to make it work.

TheNickmaster21
  • 275
  • 3
  • 13