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

How to call a program from a program

Started by FredMastro, 16 March 2014 - 03:09 PM
FredMastro #1
Posted 16 March 2014 - 04:09 PM
I've written some basic programs such as foward x, left x, turn around. How do I properly call these programs from within my main program? calling them directly I get an error. Do I need to have a main() function? Is there a startup function?
Lyqyd #2
Posted 16 March 2014 - 08:43 PM
Please post the code you've been using.
FredMastro #3
Posted 16 March 2014 - 09:40 PM
If this helps.. I have a "forward" program..


args = {...}
if args[1] == nil then
print("Moving foward")
turtle.forward()
else
print("Moving foward "..args[1])
for i=1,args[1] do
  turtle.forward()
end
end

I also have a back, down, up, dig down etc..
I want to call this indivuaully as needed but also use them in a main program which I tried.. this

Name Mine


digDownandmove 1
digfrontandmove 3
back
digup
digdownandmove
back

However I get an error: bios:339 [string "mine"]:1: '=' expected
CometWolf #4
Posted 16 March 2014 - 10:03 PM
A program you make won't act the same as something you input into the shell. To run a program from another, you use shell.run("programName",arguments)
apemanzilla #5
Posted 16 March 2014 - 10:07 PM
Try using this instead of the first line:

shell.run("digDownandmove",1)
And then use similar code for the other programs.

You may want to look into functions as well.
Lyqyd #6
Posted 16 March 2014 - 10:08 PM
You can also put the whole string in the first argument of shell.run, like so:


shell.run("digDownandmove 1")
FredMastro #7
Posted 16 March 2014 - 11:11 PM
Thank you both!