45 posts
Location
IA, USA
Posted 18 February 2015 - 12:10 AM
I have a question If i have in one program on my computer a global called x that is equal to the int 5 can other programs access that var and change it so that next time I load the program it will initialize as whatever it was changed to?
3057 posts
Location
United States of America
Posted 18 February 2015 - 03:18 AM
Err… no. Try this:
Script
x = 5
print( x )
shell.run( "other" )
print( x )
other:
x = 10
Output: 5, 10
Now, if you set x to 5, then run another program that sets x to 6, then run the program you originally used
again, it will simply set x to 5 again. Does this answer your question?
Edit: This was split from a tutorial on local variables.
Edited on 18 February 2015 - 02:02 PM
1426 posts
Location
Does anyone put something serious here?
Posted 18 February 2015 - 09:09 AM
I presume you mean between programs rather than computers. You can always do:
x = x or 5
What this will do is check if x is set as a global. If it is it will use the value of x, otherwise it will use 5. It is a useful way of handling default arguments in functions. It is worth noting though that if x is false or nil, then it will be set to 5 anyway.
Edited on 18 February 2015 - 08:09 AM
7083 posts
Location
Tasmania (AU)
Posted 18 February 2015 - 09:59 AM
Assuming you
are talking about sharing data with entirely different computers, then global variables alone won't cut it. You'd want to either use network transmissions (eg
rednet) to send the data to other systems, or write the data to a floppy disk drive that all computers involved are wired up to and can therefore read it back from.