3

我对Lua完全陌生。

我有一个非常简单的脚本:“var = 1”

我没有找到如何从我的 java 应用程序中获取此表达式的结果:

“var == 3 和 100 或 -1”

我从这个开始:

Globals globals = JsePlatform.standardGlobals();
LuaValue chunk = globals.load("var = 4");
chunk.call();

LuaValue luaGetLine = globals.get("var");

按预期返回“4”。

LuaValue luaGetLine = globals.get("var_tex1 == 3 and 100 or -1");

返回零

4

2 回答 2

1

globals.get("key") returns the LuaValue of an object in the globals table, it is not used to do expressions. The code you're giving as an example is trying to find a variable named "var_tex1 == 3 and 100 or -1", which is returning null because no such variable exists.

If you need 100 or -1 you should try to calculate it in Java:

int result = globals.get("var_tex1").checkint() == 3 ? 100 : -1;

If you need the result to be a LuaValue you can do something more like the following:

public static final LuaValue Lua_100 = LuaInteger.valueOf(100);
public static final LuaValue Lua_n1 = LuaInteger.valueOf(-1);

public LuaValue check() {
    return globals.get("var_tex1").checkint() == 3 ? Lua_100 : Lua_n1;
}

The other option is to do exactly as you did for setting the value:

public LuaValue eval(String s)
{
    LuaValue chunk = globals.load("__temp_result__=" + s);
    chunk.call();

    LuaValue result = globals.get("_temp_result_");
    globals.set("__temp_result__", LuaValue.NIL);

    return result;
}

Then call:

LuaValue result = eval("var_tex1 == 3 and 100 or -1");

If you want an integer instead of LuaValue just call checkint() on the LuaValue

于 2016-08-24T02:50:11.513 回答
0

LuaValue luaGetLine = globals.get("var_tex1 == 3 and 100 or -1");

您正在尝试读取名称下的全局变量,该名称是评估表达式“var_tex1 == 3 and 100 or -1”的结果。您可以得到 100 或 -1 结果。虽然您可以使用这样的“名称”创建变量,但由于全局环境只是另一个表,就像其他所有表一样,这可能不是您真正想要做的。

于 2016-05-29T14:40:18.920 回答