Jump to content




Saving function output

api help lua

  • You cannot reply to this topic
5 replies to this topic

#1 CCGrimHaxor

  • Members
  • 140 posts

Posted 20 January 2015 - 04:34 PM

Hey guys I was wondering how I would execute a function and then save it's outputs.
Not the return but the actual prints and writes. The drawing and clearing of terminal is not needed.
Thanks

#2 InDieTasten

  • Members
  • 357 posts
  • LocationGermany

Posted 20 January 2015 - 04:53 PM

well, override print and write to save their passed arguments in a string you can later access, run the function which outputs to "capture", restore the functions and you are good to go. write for code examples

#3 civilwargeeky

  • Members
  • 393 posts

Posted 20 January 2015 - 04:53 PM

As far as I know, you can't just read what is on a specific part of the screen without re-writing the "write" or "print" function.

Getting what you tell print to output should be really easy though
local oldPrint = print
function print(...)
  local tab = {...}
  for i=1, #tab do
	tab[i] = tostring(tab[i])
  end
  toReturn = table.concat(tab)
  oldPrint(toReturn)
  return toReturn
end 
This would do what I *think* you want, but I can't exactly tell what you want to get.

Edited by civilwargeeky, 20 January 2015 - 04:54 PM.


#4 InDieTasten

  • Members
  • 357 posts
  • LocationGermany

Posted 20 January 2015 - 05:05 PM

View Postcivilwargeeky, on 20 January 2015 - 04:53 PM, said:

As far as I know, you can't just read what is on a specific part of the screen without re-writing the "write" or "print" function.

  oldPrint(toReturn)
  return toReturn

this would change the behaviour of the program, as print has it's own return values (number of linebreaks made)

I think he/she is looking for something like this:
local output = ""
--override
local oldPrint = print
print = function(...)
local args = {...}
for k,v in ipairs(args) do
  local e,m = pcall(tostring, v)
  if(not e) then
   error(m, 2)
  end
  output = output..m
end
output = output.."\n"
return oldPrint(...)
end
--call
yourFunc()
--restore
print = oldPrint
--analyze
-- here you can use output, which contains the
-- text printed by yourFunc, without cursor position changes though


#5 CCGrimHaxor

  • Members
  • 140 posts

Posted 20 January 2015 - 05:07 PM

View Postcivilwargeeky, on 20 January 2015 - 04:53 PM, said:

As far as I know, you can't just read what is on a specific part of the screen without re-writing the "write" or "print" function.

Getting what you tell print to output should be really easy though
local oldPrint = print
function print(...)
  local tab = {...}
  for i=1, #tab do
	tab[i] = tostring(tab[i])
  end
  toReturn = table.concat(tab)
  oldPrint(toReturn)
  return toReturn
end 
This would do what I *think* you want, but I can't exactly tell what you want to get.

Yep that's it thanks

View PostInDieTasten, on 20 January 2015 - 05:05 PM, said:

View Postcivilwargeeky, on 20 January 2015 - 04:53 PM, said:

As far as I know, you can't just read what is on a specific part of the screen without re-writing the "write" or "print" function.

  oldPrint(toReturn)
  return toReturn

this would change the behaviour of the program, as print has it's own return values (number of linebreaks made)

I think he/she is looking for something like this:
local output = ""
--override
local oldPrint = print
print = function(...)
local args = {...}
for k,v in ipairs(args) do
  local e,m = pcall(tostring, v)
  if(not e) then
   error(m, 2)
  end
  output = output..m
end
output = output.."\n"
return oldPrint(...)
end
--call
yourFunc()
--restore
print = oldPrint
--analyze
-- here you can use output, which contains the
-- text printed by yourFunc, without cursor position changes though

Nice thanks

#6 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 21 January 2015 - 01:31 AM

If you're on 1.6 or later, a "simple" way of doing it would be to draw everything you want into a window, then if you want to redraw it later, simply call that window's redraw function.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users