This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Fiurilli's profile picture

How to carry over variables to programs

Started by Fiurilli, 06 November 2013 - 02:46 PM
Fiurilli #1
Posted 06 November 2013 - 03:46 PM
I am reletivaly new to ComputerCraft and everything about programming and have been looking for how this works for about 2 hours now, but could find nothing.
Oke here is what I want to do.

I want to run a program in a program. So in the main program I clarify some variables and I want to use them in the other program, but I can't seem to make that work.
Here is my code for testing how to cary over variables:

y=1
os.run({},"test")

with test being
if y==1 then
print("yeah it works")
else
print("too bad :(/>")

So in the command os.run(…..) I must put stuff so it uses y, but how and what?

Thanks in advance!

p.s. does the variable also get carried over to the main program? Say I state y in mainprogram, stuff hapens to it in the subprogram and then stuff needs to happen to it in the main program. Is that possible or do I again have to do some fancy stuff I do not know about?
Lyqyd #2
Posted 06 November 2013 - 08:48 PM
Split into new topic.

If you're wanting them separated as actual programs, why not use arguments rather than polluting the global namespace? Or if you just want code callable from multiple programs, you should really create an API and load it up and use its functions instead.
Wobbo #3
Posted 07 November 2013 - 08:25 AM
You could do:

local y = 1
local env = {y = y}
os.run(env, 'test')
Now test should have y visible, but I haven't tested this. I believe that in this case, the variable does not get carried back over to the main program, because only the value is passed and not the variable itself. I am not sure though.

Also, like Lyqyd says, it is probably better to write an API instead of using this trick.
Edited on 07 November 2013 - 07:25 AM
Micheal Pearce #4
Posted 07 November 2013 - 06:44 PM
This should help


A = "Hi" -- a string
local B = "ASDAS" -- a local string
C = "There" -- a string
shell.run("test3")
The code above is the code the i run first. It has three strings two public one local inside of that code it runs 'test3'


print(A)
print(B)/>/>
print(C)
this is 'test3' all it does is print A,B, and C and if you ran this by itself you would get errors but because we ran 'test2' first it carries over the strings and their properties

This is after we ran test2

it prints string A and C but not B because it is local with test2 and not test3

Hope this helps
(in 'test3' it keeps add this '/>' after 'print( B)'. it shouldn't be there)
Edited on 07 November 2013 - 05:49 PM
ElvishJerricco #5
Posted 07 November 2013 - 07:12 PM
This should help

This is bad practice. Relying on globals that aren't standardized globals is a bad idea. Pass the variables as arguments in shell.run instead.
Kingdaro #6
Posted 07 November 2013 - 10:33 PM
Perhaps if you told us what you're actually trying to do, we could find a better solution to go about doing that?
spdkils #7
Posted 07 November 2013 - 11:20 PM
local tArgs = {"one","two","three","four"}
shell.run("otherprogram",unpack(tArgs))
Fiurilli #8
Posted 09 November 2013 - 03:21 PM
local tArgs = {"one","two","three","four"}
shell.run("otherprogram",unpack(tArgs))

Ah thanks a bunch! This worked perfectly!