3

I'm completely new to Lua.

I have a very simple script: "var = 1"

I didn't find out how to get the result of this expression from my java app:

"var == 3 and 100 or -1"

I have started with this:

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

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

is returning "4" as expected.

but

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

is returning nil

SWAppDev
  • 248
  • 1
  • 3
  • 13

2 Answers2

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

D3_JMultiply
  • 1,022
  • 2
  • 12
  • 22
0

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

You're trying to read global variable under the name that is a result of evaluating expression "var_tex1 == 3 and 100 or -1". You can get either 100 or -1 in result. While you can create variables with such "names", as global environment is just another table, like all the rest, that's probably not what you really wanted to do.

Vlad
  • 5,450
  • 1
  • 12
  • 19
  • this is what i want: i need to get 100 or -1 in result. but apparently this this is not the good way of getting the expression result. do you know what i should do to get this result ? – SWAppDev May 29 '16 at 16:57
  • You want expression result, but instead you're reading some global variable that is not there. See LuaJ manuals, check LuaValue.valueof() method. – Vlad May 29 '16 at 17:13