0

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.

TritriGuy123
  • 31
  • 1
  • 6
  • I can't tell what you're trying to do. You seem to be asking for a way to intercept output, but [`load`](https://www.lua.org/manual/5.3/manual.html#pdf-load) is unrelated to output. In your example, `print` returns nothing, but `load` requires a string or function argument. – luther Mar 01 '20 at 18:05
  • 1
    I'm trying to catch the output from for example print('Hello') and store it in a variable/table, sorry for bad explanation. – TritriGuy123 Mar 01 '20 at 18:08

1 Answers1

0

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.

luther
  • 5,195
  • 1
  • 14
  • 24