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

Parallel API

Started by Zenon, 21 July 2015 - 12:00 AM
Zenon #1
Posted 21 July 2015 - 02:00 AM
Hi all! I was just reading up on parallel api, and was making a little test code . . .

This will wait for input, though it wont respond to getting it:
Spoiler

function r()
ev, param1 = os.pullEvent()
if param1 == keys.r then
  print("R")
end
end
function e()
ev, param1 = os.pullEvent()
if param1 == keys.e then
  print("R")
end
end
parallel.waitForAll(e(), r())

This wont draw errors, but wont wait for input:
Spoiler

function r()
ev, param1 = os.pullEvent()
if param1 == keys.r then
  print("R")
end
end
function e()
ev, param1 = os.pullEvent()
if param1 == keys.e then
  print("R")
end
end
parallel.waitForAll(e, r)

Any ideas? Thanks in advance!
H4X0RZ #2
Posted 21 July 2015 - 02:27 AM
You need to place content of the functions into an infinite loop. Also, you should not add parentheses. If you really have to call the function with arguments, you have to use an anonymous function.

 
local f1 = function() 
  while true do
    --do the event stuff here 
  end
end

local f2 = function(...) 
  --the same as above
end

parallel.waitForAll(f1, function() 
    f2("Random","arguments","foo","bar")
  end) 
Edited on 21 July 2015 - 01:33 AM
HPWebcamAble #3
Posted 21 July 2015 - 02:40 AM
In most situations, you don't need to use the parallel API, though it is good to know how to use it and how it works.



parallel.waitForAll(f1, function()
	f2("Random","arguments","foo","bar")
  end

You missed a closing parenthese at the end
H4X0RZ #4
Posted 21 July 2015 - 03:34 AM
In most situations, you don't need to use the parallel API, though it is good to know how to use it and how it works.



parallel.waitForAll(f1, function()
	f2("Random","arguments","foo","bar")
  end

You missed a closing parenthese at the end

Oh, thanks for telling me! I'm on my phone right now, pretty hard to write code on it xD
Zenon #5
Posted 21 July 2015 - 03:47 AM
You need to place content of the functions into an infinite loop. Also, you should not add parentheses. If you really have to call the function with arguments, you have to use an anonymous function.


local f1 = function()
  while true do
	--do the event stuff here
  end
end

local f2 = function(...)
  --the same as above
end

parallel.waitForAll(f1, function()
	f2("Random","arguments","foo","bar")
  end)

Thanks a bunch!
flaghacker #6
Posted 21 July 2015 - 07:15 AM

parallel.waitForAll(e(), r())
Remove the parentheses after 'e' and 'r'. Here you're calling them, but you want to pass them to the function instead. Fixed code:

parallel.waitForAll(e, r)
Zenon #7
Posted 21 July 2015 - 08:21 PM

parallel.waitForAll(e(), r())
Remove the parentheses after 'e' and 'r'. Here you're calling them, but you want to pass them to the function instead. Fixed code:

parallel.waitForAll(e, r)
Yeah, I saw that while looking around . . . thats why I made the second code