Search code examples
javalualuaj

Getting print calls in LuaJ


I'm writing a Java program which uses Lua scripts to determine what to output to certain areas of the program. Currently, my code looks as such:

Globals globals = JsePlatform.standardGlobals();
LuaValue chunk = globals.loadfile(dir.getAbsolutePath() + "/" + name);
chunk.call();
String output = chunk.tojstring();

The problem is that calling tojstring() appears to return return values from the Lua script. This is fine, but I need to get print calls, as that's what will be displayed on the screen. As of now, the print calls get sent directly to the Console (printed to console), and I cannot figure out a way to retrieve these print calls.

I've tried digging through the documentation but have had little success. Will change from LuaJ if needed.


Solution

  • I actually was able to solve the problem by changing the STDOUT variable in the globals object to a temporary file, and then reading the data from the temporary file.

    Probably not the best solution, but works perfectly fine.