Get "output" from Lua command

Asked

Viewed 94 times

1

I want to take Output from a moon 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 got!

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

But I wanted you to show if I had a mistake too.

  • 1

    I don’t understand what you want. Try to assemble a code that shows something. Explain in detail. Tell me where you’re going, there might be another way to do the same.

  • I edited the question.

  • Yes, but I wanted you to indicate the error, sorry I wasn’t clear. EX: Attempt to call a nil value.

1 answer

0

The loadstring function has a return like this:

Succeed:

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

If mistake:

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

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

It works the same way, the difference that from the second return on, 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.