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

[Lua] executing two commands at a time?

Started by Unfortold, 15 March 2012 - 12:26 AM
Unfortold #1
Posted 15 March 2012 - 01:26 AM
Hello,
maybe this is a really stupid question but I have been using Lua for only two days now. what I want to do is run both:

id, message = Rednet.receive()
local name = io.read()

together at the same time. can anyone help me with this? this is my full code:


rednet.open("right")
output = "back"
redstone.setOutput(output, true)
while true do
id, message = rednet.receive(3)
local name = io.read()
if name == "unlock" then
redstone.setOutput(output, false)
end
if name == "lock" then
redstone.setOutput(output, true)
end
if message == "unlock" then
redstone.setOutput(output, false)
end
if message == "lock" then
redstone.setOutput(output, true)
end
end
Espen #2
Posted 15 March 2012 - 01:42 AM
You might want to look into the parallel API:
help parallel

Basically you can run multiple functions at the "same" time.
That means you can listen for rednet events and you can input commands at the same time, without the one having to wait for the other.

Also search in the forum for parallel.waitForAny or parallel.waitForAll
That should point you in the right direction.
Unfortold #3
Posted 15 March 2012 - 01:51 AM
You might want to look into the parallel API:
help parallel

Basically you can run multiple functions at the "same" time.
That means you can listen for rednet events and you can input commands at the same time, without the one having to wait for the other.

Also search in the forum for parallel.waitForAny or parallel.waitForAll
That should point you in the right direction.

OH! thank you so much, I think that fixed it.
Unfortold #4
Posted 15 March 2012 - 02:16 AM
okay I tried it with this:

rednet.open("right")
output = "back"
redstone.setOutput(output, true)
while true do
parallel.waitForAny(id, message = rednet.receive(), local name = io.read())
if name == "unlock" then
redstone.setOutput(output, false)
end
if name == "lock" then
redstone.setOutput(output, true)
end
if message == "unlock" then
redstone.setOutput(output, false)
end
if message == "lock" then
redstone.setOutput(output, true)
end
end

but I get a bios error?
hellp O.o
Espen #5
Posted 15 March 2012 - 12:01 PM
You're not using parallel the right way. You don't call it multiple times, you call it once and provide it with the functions that should run in separate threads until they finish. If you search the forum for the words how I told you before, then you will get some examples for how to use it!

For example look at the last code in this post: http://www.computerc...ndpost__p__2241
But please use the search function next time! That's how I found the above post again. :D/>/>