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

Turtle - Problem with infinite loops

Started by Wiesl, 27 September 2013 - 12:29 PM
Wiesl #1
Posted 27 September 2013 - 02:29 PM
Hi there,
i have a short question about turtles and especially interrupting loops:

i sent from a computer 2 messages:


(button pressed)
modem.transmit (1, 1, "turtleon")


(button pressed again)
modem.transmit (1, 1, "turtleoff")

in my turtle i want to recieve this message and start or end a infinite turtle.attack() loop. And here i have the probelm.

To make my turtle attack all the time, i need this,or?


while true do
turtle.attack()
end

to activate it i use a os.pullEvent


function msg()
event, par1, par2, par3, message, os.pullEvent(modem_message)
if message = "turtleon"
then
turtle.attack() -- how to make this infinite??
if message = "turtleoff"
then
print("OFF")
end
end

while true do
msg()
end

but how to make the turtle.attack now infinite till i send "turtleoff", i tried following:


function msg()
event, par1, par2, par3, message, os.pullEvent(modem_message)
if message = "turtleon"
then
while true do
turtle.attack() -- how to make this infinite??
end
if message = "turtleoff"
then
print("OFF")
end
end

while true do
msg()
end

–> Error

Can anyone help me here?


thx in advance
#
Wiesl
Lyqyd #2
Posted 27 September 2013 - 09:04 PM
Split into new topic.
apemanzilla #3
Posted 27 September 2013 - 11:25 PM
Although I don't have much experience with this myself, you may be able to use the coroutines API or possibly parallels.
Wiesl #4
Posted 28 September 2013 - 04:04 AM
i tried it with parallels, but then i have a problem to toggle the stat of the turtle.

As i mentioned i donnot want to stop the infinite turtle.attack() loop simply, but to toggle the the infinite loop with a rednet message.

i tried this now for hours and i cannot come to a solution.

i know i am not very experienced with programming, but there should be a solution i think ;-)

Wiesl
theoriginalbit #5
Posted 28 September 2013 - 04:19 AM
As I stated on IRC, use an event loop.

The native turtle api does not hold your program while the turtle is performing the task, instead it gives you an ID and fires a "turtle_response" event when it completes with the ID

If I understand what you want correctly the code would look something like this


--# the native table does not hold your program while the turtle is performing the task, instead it gives you an ID and fires a "turtle_response" event when it completes with the ID
local attackID = turtle.native.attack()

--# should the turtle attack?
local active = true

--# infinitely loop
while true do
  --# wait here until something happens
  local event = { os.pullEventRaw() }

  --# if what just happenned was a message from a computer
  if event[1] == "modem_message" then
	--# was the message turtleon?
	if event[5] == "turtleon" then
	  --# if the turtle was inactive, restart it
	  if not active then
		attackID = turtle.native.attack()
	  end
	  --# allow it to continue
	  active = true

	--# was the message turtleoff?
	elseif event[5] == "turtleoff" then
	  --# don't allow the turtle to continue
	  active = false

	--# message wasn't for us
	else
	  print("Invalid message")
	end

  --# if what just happenned was the turtle finished what we told it to
  elseif event[1] == "turtle_response" then
	--# if it can continue
	if active then
	  --# attack again
	  attackID = turtle.native.attack()
	end

  --# if what just happenned was the user just pressed CTRL+T
  elseif event[1] == "terminate" then
	--# you can do something here, including sending a message to the controlling computer
	return
  end
end