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

Passing Parameters Between Programs

Started by crys17p, 26 September 2013 - 02:41 PM
crys17p #1
Posted 26 September 2013 - 04:41 PM
Hy guys,


I just finished setting up a GPS program that uses Trilateration to get the gps potions and go to a specific location on the map. I use three global variables to set the desired destination for the turtle. Now i want to have a different program, say a "wood chopper" that uses the gps program to get around. So what i would like to know basically is how to pass parameters between programs in lua. I need to call the gps program and pass the three coordinate values to it. Also i may have to read values form the GPS program. Any help is appreciated and i thank you for the responses in advance.
Lyqyd #2
Posted 27 September 2013 - 12:13 AM
Moved to Ask a Pro.
LBPHacker #3
Posted 27 September 2013 - 12:30 AM
We (or I, at least) would really appreciate if you posted the full code of the GPS program, but whatever, I'll be guessing this time.

I know, it makes no sense to you, but insert this at the end of the GPS program:
return coordX, coordY, coordZ -- customize variables here
Since programs are ordinary functions, with that move you made a fancy function of that GPS program of yours which now returns coordinates. Now all you have to do is load the GPS program and run it:
-- this is the wood chopper

local getCoordinates = loadfile("gpsCoords") -- absolute filename of the GPS program

-- rest of the code
Now you can just call getCoordinates and it will return three values. (Actually it is the GPS program now, so if the GPS program returns three values, this function will do so as well.)
gknova61 #4
Posted 27 September 2013 - 03:04 AM
You could also incorporate arguments into the program you wana pass your parameters to like so:

local tArgs = {...}
if #tArgs < 3 then
   error("Needs moar parameters!")
end
local x,y,z = tArgs[1],tArgs[2],tArgs[3]
You'd put this at the top of your program to recieve the parameters.

For the program sending the parameters, you can do a shell.run passing the parameters like so:

shell.run("programname "..x.." "..y.." "..z)
Mitchfizz05 #5
Posted 27 September 2013 - 03:45 AM
If I am reading correctly, you want to get the current coords of the turtle by running the gps program and getting it's output?
If that is what you are trying to do, look into the GPS Api. The page has examples on howto retreive the current X, Y, Z position.

If you want to know howto run a program with arguments in lua, use…

shell.run("programname arg1 arg2 arg3")
crys17p #6
Posted 27 September 2013 - 08:41 AM
Thanks for all your responses. Sorry that i forgot to include the code i will put it in this reply. Actually the program i made is not a gps program , it uses the gps api to get the x y z coordinates from the four computer gps hosts i set up and its main purpose is to go to the specified location( trg_x , trg_y, trg_z). And what i want to do is to call this "goto_gps" program form inside the "wood chopper" program and pass parameters between them both ways ( something like 3 parameters form the wood chopper to the goto_gps for the cordinates and an acknowledge parameter form the goto_gps to the wood chopper to signal if the trutle has reached the destination.

This is my code for the goto_gps:





trg_x = 480
trg_y = 64
trg_z = -540

flag = 0
turtle.select(1)
turtle.refuel()


dir = 0



function direction()
count = 0

if turtle.detect() then
turtle.up()
count = count + 1
else
x_1,y_1,z_1 = gps.locate()
print( x_1..y_1..z_1)
turtle.forward()
x_2,y_2,z_2 = gps.locate()
print( x_2..y_2..z_2)

if x_1 > x_2 then
dir = -1
elseif x_1 < x_2 then
dir = 1
elseif z_1 > z_2 then
dir = 2
elseif z_1 < z_2 then
dir = -2
end
turtle.back()
for i=1,count do
turtle.down()

end
count = 0
print ("direction is"..dir)


end
end

function x_axis_plus()

if dir == 1 then
turtle.turnLeft()
turtle.turnLeft()
dir = -1
elseif dir == 2 then

turtle.turnLeft()
dir = -1

elseif dir == -2 then

turtle.turnRight()
dir = -1
end

if turtle.detect() then
turtle.up()
else
turtle.forward()
end


end


function x_axis_minus()

if dir == -1 then
turtle.turnRight()
turtle.turnRight()
dir = 1
elseif dir == 2 then

turtle.turnRight()
dir = 1

elseif dir == -2 then

turtle.turnLeft()
dir = 1
end
if turtle.detect() then
turtle.up()
else
turtle.forward()
end
end


function z_axis_plus()

if dir == 2 then
turtle.turnLeft()
turtle.turnLeft()
dir = -2
elseif dir == -1 then

turtle.turnLeft()
dir = -2

elseif dir == 1 then

turtle.turnRight()
dir = -2
end

if turtle.detect() then
turtle.up()
else
turtle.forward()
end
end


function z_axis_minus()

if dir == -2 then
turtle.turnRight()
turtle.turnRight()
dir = 2
elseif dir == -1 then

turtle.turnRight()
dir = 2

elseif dir == 1 then

turtle.turnLeft()
dir = 2
end
if turtle.detect() then
turtle.up()
else
turtle.forward()
end
end



function y_axis_plus()
turtle.down()

end


function y_axis_minus()
turtle.up()

end

while true do

x,y,z = gps.locate()
if flag == 0 then
direction()
flag = 1
end

if x > trg_x then
x_axis_plus()



elseif x < trg_x then
x_axis_minus()


elseif z < trg_z then
z_axis_plus()



elseif z > trg_z then
z_axis_minus()



elseif y > trg_y then
y_axis_plus()



elseif y < trg_y then
y_axis_minus()
end

end
LBPHacker #7
Posted 27 September 2013 - 02:41 PM
  • Use spoiler and code tags, please - I was scrolling for about ten seconds until I reached the posting section… ([spoiler][code]put everything in here[/code][/spoiler]
  • If I get this right, you want to pass arguments to the "gps program" of yours - well, it's now in a function, so you can just do getCoordinates(arg1, arg2, arg3)