Getting return values from another program

Started by Kaikaku, Apr 10, 2021, 04:43 PM

Previous topic - Next topic

Kaikaku

I learned from this post (old forum), how to get the return value from another program:
  • If the other program "programname" doesn't take arguments you can use:  returnvalue = assert(loadfile("programname"))()
  • If the other program "programname" takes one argument you can use:  returnvalue = assert(loadfile("programname"))("argument")

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).
Hi there!
I make YouTube videos about my turtle programs (incl. pastebin). Visit me if you like :)

Lupus590

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.

Kaikaku

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.
Hi there!
I make YouTube videos about my turtle programs (incl. pastebin). Visit me if you like :)