2015-06-22 58 views
0

我已经从luaj网站做了一个复杂的例子.. LuaJ 我想在当前对象上运行一个函数目前正在使用。但是luaJ正在创造一个新的对象。LuaJ如何在调用对象上运行函数(LuaJ正在创建新对象如何避免它)

我怎样才能在当前对象上运行的功能,不做一个新的。

cosidering下面的代码...

import org.luaj.vm2.Globals; 
import org.luaj.vm2.LuaValue; 
import org.luaj.vm2.lib.TwoArgFunction; 
import org.luaj.vm2.lib.ZeroArgFunction; 
import org.luaj.vm2.lib.jse.JsePlatform; 

public class luaTest extends TwoArgFunction { 

    public luaTest() {} 
    changeInt mytest=new changeInt(); 

    public LuaValue call(LuaValue modname, LuaValue env) { 
     LuaValue library = tableOf(); 
     library.set("countup", new countup(mytest)); 
     env.set("luaTest", library); 
     return library; 
    } 

    void run() { 
     Globals globals = JsePlatform.standardGlobals(); 
     mytest.setA(10); // Setting it 10 before calling the script 
     LuaValue chunk = globals.loadfile("script.lua"); 

     chunk.call(); 

    } 
    class countup extends ZeroArgFunction { 
     changeInt mytest; 
     public countup(changeInt a) 
     { 
      mytest=a; 
     } 
     public LuaValue call() { 
      return LuaValue.valueOf(mytest.countup()); 
     } 
    } 

} 

的changeInt类是简单的一个变量...

public class changeInt { 

    int a = 1; 
    public int countup(){ 
     return a++; 
    } 
    public void setA(int x) 
    { 
     a=x; 
    } 

} 

luaScript很简单..

require 'luaTest' 

print('count',luaTest.countup()) 
print('count',luaTest.countup()) 
print('count',luaTest.countup()) 

有什么绕过它..

+0

是什么“这是目前正在使用的当前对象”呢? – immibis

+0

我称之为lua脚本的对象...(luaTest) 我在调用脚本之前设置了该对象的成员“a”的值。 我想要打印相同的变量.... – BravoAlphaRomeo

回答

相关问题