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

How to do two things at a time in a program

Started by Alerith, 07 April 2015 - 07:35 AM
Alerith #1
Posted 07 April 2015 - 09:35 AM
I don't know if the title is understandable, but for example, I have this countdown and in a separate line, let's say "term.setCursorPos(1,3)" I want to write something, anything, but with the countdown running all the time.

Thank you and apologize my english!
Bomb Bloke #2
Posted 07 April 2015 - 11:01 AM
The question isn't so much what you want to write as it is when do you want to do it.

Odds are the examples here will show you what you need. If they don't, please explain what your end goals are.
hbomb79 #3
Posted 07 April 2015 - 11:18 AM
I don't know if the title is understandable, but for example, I have this countdown and in a separate line, let's say "term.setCursorPos(1,3)" I want to write something, anything, but with the countdown running all the time.

Thank you and apologize my english!

Im going to assume you want to function running at the same time as each other. If so then parallel events is possibly what you want, or maybe coroutines, but heres an example for parallel events:


function countdown()
for i = 1, 10, 1 do
  --# do stuff
  sleep(0)
end
end
function main()
--# Doing stuff in here
end
parallel.waitForAll(countdown, main)
--# This parallel will wait for both function to finish before continuing with the rest of the program


parallel.waitForAny(countdown, main)
--# whereas this parallel will wait for any of the functions to finish, and then continue, so if you have a while loop in main, it will be STOPPED when the countdown function is done.


--# You can have more than two functions running at a time:
parallel.waitForAny(func1, func2, func3, func4, etc...)

With this code you can have your coundown running in the background, and have main with the rest of the code you want to run whilst the countdown continues. Let me know if this is what you're after.
Edited on 07 April 2015 - 09:18 AM
Alerith #4
Posted 08 April 2015 - 07:45 AM
Hi, thank you very much for your responses. Although I still have a problem. This is my code:


local num = "4 8 15 16 23 42"

local function input()
term.setCursorPos(1,3)
x = io.read()
print("numbers: "..x)
end

local function countdown()
term.setCursorPos(1,1)
m = 108
s = 59
for i=1, 6480 do --6480 sec = 108 min
term.clearLine()
term.setCursorPos(1,1)
write(m..":")
write(s)
sleep(1)
if s == 0 then
  m = m-1
  s = 60
end
s = s-1
end
end
parallel.waitForAny(input, countdown)

If you haven't watched Lost series there is a countdown of 108 minutes and before that ends, is needed to enter the code "4 8 15 16 23 42". If not, well, there are consequencies.
So, the countdown works perfect but the other thing does not. I need to enter the numbers on pos (1,3), but tue cursor is always on (1,1).
Bomb Bloke #5
Posted 08 April 2015 - 08:39 AM
Use term.getCursorPos(). Have your countdown() function record where the cursor is before it moves it, then put it back there before it goes to sleep.
Alerith #6
Posted 09 April 2015 - 12:25 AM
Use term.getCursorPos(). Have your countdown() function record where the cursor is before it moves it, then put it back there before it goes to sleep.

Thank you! That helped and solved the problem :D/>