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

parallel problems

Started by nahkranoth, 08 November 2012 - 08:50 AM
nahkranoth #1
Posted 08 November 2012 - 09:50 AM
I'm fooling around with the parallel api, and it sounds great, but I can't seem to get it to work.
For example i want two animations to run at once (see code below)
It only plays the first function i put into the parallel.waitForAll


local frame1 = 1
local frame2 = 1
local animT = {"/","-","\\","|",}
function play()
while true do
  term.setCursorPos(10,4)
  currAnim = animT[frame1]
  term.write(currAnim)
  if frame1 == #animT then frame1 = 0 end
  frame1 = frame1 + 1
  sleep(.5)
end
end
function play2()
while true do
  term.setCursorPos(20,2)
  currAnim = animT[frame2]
  term.write(currAnim)
  if frame2 == #animT then frame2 = 0 end
  frame2 = frame2 + 1
  sleep(.1)
end
end
parallel.waitForAll(play(), play2())
Luanub #2
Posted 08 November 2012 - 09:53 AM
Remove the ()'s from the waitForAll()


parallel.waitForAll(play, play2)
Kingdaro #3
Posted 08 November 2012 - 09:53 AM
Remove the parentheses on your last line.

parallel.waitForAll(play, play2)

EDIT: F*cking ninjas.
nahkranoth #4
Posted 08 November 2012 - 09:55 AM
Yes!!, thank you it works like a charm
nahkranoth #5
Posted 08 November 2012 - 10:36 AM
Another question. Is it possible to add and remove functions from executing in the parallel.waitForAll on the fly?
So for instance on my example: is it possible to stop one animation and remove it from the list to be executed (it stops playing) and later restart him?

I hope i was clear, else i can add an example
Kingdaro #6
Posted 08 November 2012 - 10:43 AM
It's not really possible to explicitly do what you said, but you could just edit your functions so that they stop looping when a certain condition is met.


local running = true

function foo()
	while running do -- keep doing this loop while running is not false or nil
		print 'foo'
		sleep(0.01)
	end
end

function bar()
	-- set running to false after one second
	-- subsequently, the function foo() should stop printing "foo"
	sleep(1)
	running = false
end

parallel.waitForAll(foo, bar)
nahkranoth #7
Posted 08 November 2012 - 10:53 AM
Yes i did something similar, but then when i want to start it again by making running true again. It doesn't restart the animation.
My guess is that the parallel.waitForAll stopped checking for the function after i stopped it.

The example below shows what i do: one function is checking for button presses, the other is playing a wait animation.
I want one button to start/restart the wait animation and one to pause it.
In the wait api i did what you decribed above and the button api is just executing the function when you click it (hence the name ;p )

example:

os.loadAPI("button")
os.loadAPI("debug")
os.loadAPI("wait")
button.buildButton("Start", 12, 12, function() wait.start() end)
button.buildButton("Stop", 12, 14, function() wait.stop() end)

function startParallel()
parallel.waitForAll(wait.play, button.runBtnCheck)
end

startParallel()

Kingdaro #8
Posted 08 November 2012 - 11:01 AM
With the example I've given, setting running to false exits the loop completely. Instead, just have the loop run continuously, while checking if running is true inside the loop.


function foo()
	while true do
		if running then
			print 'foo'
		end
		sleep(0.01)
	end
end
nahkranoth #9
Posted 08 November 2012 - 11:35 AM
Ahh, yes offcourse, thank you Kingdaro! You helped me allot, now i have a solid concept of how to structure multithread functions.