Get "output" command Lua

1

I want to get the output of a lua command and store it in a string. EX: print (1) output- > 1 getOutput ("print (1)") I would like to store the returned value in a string, in this case, 1.

Something like a debugger, it would execute the command and store the result in a string.

I did it!

a= "print('stackoverflow')"
b = loadstring(a)
print(b())

But I wanted you to show it if you had an error too.

    
asked by anonymous 21.11.2014 / 00:07

1 answer

0

The loadstring function has a return like this:

If success:

  • Return # 1: Function
  • Return # 2: true

If error:

  • Return # 1: false
  • Return # 2: string containing error message

In addition, there is the pcall function that calls another and returns if there was success.

It works the same way, the difference from the second return onwards, it returns what you returned.

Basically:

local ret,err = loastring(str)
if not ret then
   print(err)
else
   ret,err = pcall(ret,...)
   if not ret then
      print(err)
   end
end
    
08.11.2017 / 05:05