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

Parallel not working..

Started by stabby, 15 December 2013 - 02:43 PM
stabby #1
Posted 15 December 2013 - 03:43 PM
So, i made this simple little program to manage my tree farm. I've tried VERY many ways to do this but parallel (api) always messes it up..


Basically it only does the function that is first so if i write

parallel.waitForAll(timer(), click())
It would only run the timer function and if i do the other way around

  parallel.waitForAll(click(),timer())
it only runs the click function.

Here's the code for the whole program


mouseHeight = 0
mouseWidth = 0
onOff = true
function printMonitor(state, number, max)
  monitor.setCursorPos(1,1)
  monitor.setTextScale(1)
  monitor.write(state.. " ".. number)
  monitor.setCursorPos(1,2)
  monitor.write("/".. max)
end

function timer()
  local maxOn = 60
  local maxOff = 270
  while true do
  
	  for i = 1, maxOn do
		if onOff == true then
		  rs.setOutput("right", true)
		  sleep(0.5)
		  printMonitor("On",i,maxOn)
		else
		  repeat
		  monitor.setCursorPos(1,1)
		  monitor.setTextScale(2)
		  monitor.write("OFF")
		  until onOff == true
		end
			  
	  end
	  for i = 1, maxOff do
		if onOff == true then
		rs.setOutput("right", false)
		sleep(0.5)
		printMonitor("Off", i, maxOff)
		else
		  repeat
		  monitor.setCursorPos(1,1)
		  monitor.setTextScale(2)
		  monitor.write("OFF")
		  until onOff == true
		end
	  end
  
	monitor.setCursorPos(1,1)
	monitor.write("OFF")
  
  end
end


function click()
while true do
  event, p1, p2, p3 = os.pullEvent()
  if event == "monitor_touch" then
	mouseWidth = p2
	mouseHeight = p3  
	if mouseWidth >15 and mouseWidth < 18 and mouseHeight == 2 then
	  onOff = false
	  print("OOPS")
	end
	if mouseWidth >12 and mouseWidth < 18 and mouseHeight == 4 then
	  onOff = true
	  print("off")
	end
  end
  end
end


monitor = peripheral.wrap("top")
monitor.setBackgroundColour(colours.lime)
monitor.setCursorPos(12,2)
monitor.write("  ON  ")
monitor.setCursorPos(12,4)
monitor.write("  OFF ")
monitor.setBackgroundColour(colours.black)
parallel.waitForAll(timer(),click())

I hope someone can find a fix for this
Edited by
Lyqyd #2
Posted 15 December 2013 - 03:45 PM
You need to pass the functions instead of calling them:


parallel.waitForAll(timer,click)
stabby #3
Posted 15 December 2013 - 04:00 PM
Fixed it, Thanks!
Zudo #4
Posted 16 December 2013 - 12:46 PM
When you set a variable to something like this:

myvar = myfunction()
it will use the return value of that function, rather than that function itself.