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

Running functions at the same time

Started by CatalinT, 14 May 2014 - 04:53 PM
CatalinT #1
Posted 14 May 2014 - 06:53 PM
How do I make the turtle run functions a and b run separately?
I want it to place the lamp and if I restart the turtle to make it mine the lamp even if it didn't place it in that session.

function a()
  while true do
	os.pullEvent("redstone")
	if rs.getInput("back") == true
	  then
		turtle.digUp()
		turtle.up()
		turtle.select(1)
		turtle.placeUp()
		turtle.down()
		turtle.select(2)
		turtle.placeUp()
end
end
end
function b()
	while true do
	os.pullEvent("redstone")
	if rs.getInput("front") == true
	  then
		turtle.select(2)
		turtle.digUp()
		turtle.up()
		turtle.select(1)
		turtle.digUp()
		turtle.down()
		turtle.select(2)
		turtle.placeUp()
		turtle.select(1)
end
end
end
while true do
a()
b()
end
Lyqyd #2
Posted 14 May 2014 - 07:56 PM
You can use parallel to make them run "at the same time", but that is a very bad idea with more than one turtle-movement function, as both functions could be in the process of performing the turtle actions (as they yield internally), this causing the instructions to interlace.
CometWolf #3
Posted 14 May 2014 - 08:39 PM
This thread appears to have a very misleading title… You could probably just check for the lamp prior to the while true do loop, using turtle.detect or turtle.compare.
Anavrins #4
Posted 14 May 2014 - 09:03 PM
You don't need two function in parallel, you only need one os.pullEvent
Here how it should look like.

while true do
  os.pullEvent("redstone")
  if rs.getInput("back") then
    turtle.digUp()
    turtle.up()
    turtle.select(1)
    turtle.placeUp()
    turtle.down()
    turtle.select(2)
    turtle.placeUp()
  elseif rs.getInput("front") then
    turtle.select(2)
    turtle.digUp()
    turtle.up()
    turtle.select(1)
    turtle.digUp()
    turtle.down()
    turtle.select(2)
    turtle.placeUp()
    turtle.select(1)
  end
end
Edited on 14 May 2014 - 07:05 PM
CatalinT #5
Posted 15 May 2014 - 09:04 AM
This thread appears to have a very misleading title… You could probably just check for the lamp prior to the while true do loop, using turtle.detect or turtle.compare.
The lamp is above ground, while the turtle is underground.If it checks for a block above it will detect the ground.

You don't need two function in parallel, you only need one os.pullEvent
Here how it should look like.

while true do
  os.pullEvent("redstone")
  if rs.getInput("back") then
	turtle.digUp()
	turtle.up()
	turtle.select(1)
	turtle.placeUp()
	turtle.down()
	turtle.select(2)
	turtle.placeUp()
  elseif rs.getInput("front") then
	turtle.select(2)
	turtle.digUp()
	turtle.up()
	turtle.select(1)
	turtle.digUp()
	turtle.down()
	turtle.select(2)
	turtle.placeUp()
	turtle.select(1)
  end
end
You sir, are a genius!Thanks!It works like I intended it to work.
Admin can lock this topic.
Edited on 15 May 2014 - 03:15 PM