Is there a way to catch the output in lua?

774 views Asked by At

I'm trying to catch the output from for example: print('Hello') and store it in a variable / table.

Please let me know if this is even possible. If not thanks for answering.

1

There are 1 answers

4
luther On BEST ANSWER

You can't intercept standard output directly, but you can change the global print function:

local outputs = {}
local function storeOutputs(...)
  table.insert(outputs, {...})
end

local oldPrint = print
function print(...)
  storeOutputs(...)
  oldPrint(...)
end

I'm not sure if there's a way to deal with io.write calls.