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

[Solved][Lua][Error]Parallel.waitForEver():

Started by Andybish, 29 August 2012 - 10:30 PM
Andybish #1
Posted 30 August 2012 - 12:30 AM
The program, shown below, never stops, despite i reaching 10.
Note: the receiving turtle is inactive, so no message is received by the code, and x is not pressed.
term.clear()
term.setCursorPos(1,1)
exit = false
exitrpt = false
i = 0
turtlecontrol = {
  send = function()
	repeat
	  i = i + 1
	  rednet.broadcast("intro")
	  print(i)
	until exitrpt or i == 10
  end;
  receive = function()
	eventtype,p1,p2,p3 = os.pullEvent()
	exitrpt = true
	if eventtype == "rednet_message" then
	  print(p2)
	else
	  if eventtype == "char" and p1 == "x" then
		exit = true
	  end
	end
  end;
}
print("Sending...")
parallel.waitForAny(turtlecontrol.send(),turtlecontrol.receive())
if exit == false then
  if exitrpt == false then
	print("Failed")
  end
end
The full output is:
Sending…
1
2
3
4
5
6
7
8
9
10
And the program doesn't end.
MysticT #2
Posted 30 August 2012 - 12:36 AM
You don't have to call the functions, just pass them as parameters:

parallel.waitForAny(turtlecontrol.send, turtlecontrol.receive) -- removed the brackets

Also, the send function never yields, so it won't run at the same time as the receive, so it's pretty pointless to use parallel.
Andybish #3
Posted 30 August 2012 - 06:36 PM
The parallel is necessary to make the program work, as I want the program to continuously send while waiting for an event. With the 2 running in the same code section, the event pull will stop the repeat, and for the 2 to run simultaneously in different code sections, only a parallel can be used. So firstly, what do you mean by yield? and secondly, how could I recode the send function to yield, making the parallel possible?
Removing the brackets seems to have resolved the problem, thanks.