52 posts
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!
1583 posts
Location
Germany
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
957 posts
Location
Web Development
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
1583 posts
Location
Germany
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
52 posts
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!
656 posts
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)
52 posts
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