ComputerCraft Forums

ComputerCraft => Ask a Pro => Topic started by: Kaikaku on Apr 10, 2021, 04:43 PM

Title: Getting return values from another program
Post by: Kaikaku on Apr 10, 2021, 04:43 PM
I learned from this post (http://www.computercraft.info/forums2/index.php?/topic/2153-luaquestion-how-can-i-get-the-return-statements-value-from-another-program/) (old forum), how to get the return value from another program:

However, I haven't managed to use this approach if programname needs not only one, but an undefined number of arguments.

The old post also mentions a solution using a global variable (here return_Value), but it doesn't work for me. return_Value is always "" :(
programname.lua:
tArgs={...}
-- does stuff'n things
return_Value = 4
program which should get the return value:
shell.run("programname.lua arg1 arg2 arg3 arg4 arg5 arg6")

if return_Value == nil then
  print("This test has failed.")
else
  print("Success!")
  print(return_Value)
  if return_Value=="" then print("return_Value=''") end
end

I assume I could rewrite programname so I could use loadfile to access a function in programname, but I'd prefer an easy way just like for one argument.
Or maybe I'm just making some noob errors (first time trying out global variables).
Title: Getting return values from another program
Post by: Lupus590 on Apr 10, 2021, 05:50 PM
returnvalue = assert(loadfile("programname"))("argument1", "argument2", "argument3", ...)
At about the same time as adding require, the craftos shell gives every program its own environment and thus breaks this old behaviour that you are using in the other example. You could use os.run instead but then you have to provide the environment and stuff for the client program.
Title: Getting return values from another program
Post by: Kaikaku on Apr 10, 2021, 06:01 PM
Quote from: Lupus590 on Apr 10, 2021, 05:50 PMreturnvalue = assert(loadfile("programname"))("argument1", "argument2", "argument3", ...)
At about the same time as adding require, the craftos shell gives every program its own environment and thus breaks this old behaviour that you are using in the other example. You could use os.run instead but then you have to provide the environment and stuff for the client program.

Thank you Lupus :)
In my (obviously not systematic) trial and error, I did try with and without "" and with and without , but missed the combination.