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

ClearLine and Read at the same time

Started by Nolorwolf, 03 January 2013 - 08:16 AM
Nolorwolf #1
Posted 03 January 2013 - 09:16 AM
Hello, i am trying to write an SD program, but I have a problem with having an active countdown and also waiting for input in case I want to abort it.
This is simplified program without detonation signal, abort signal, else's and stuff like that, that's easy, all I attached was alarm to see if I managed to get the abort right.

It should look like this:
Counting:
60..1
(line awaiting for correct input)

Also, if you can add what to type for code looking like *'s it would help unless I find it before someone answers :D/> (optional)


term.clear()
term.setCursorPos(1,1)

print ("Self-destruction sequence initiated:")
sleep (2)
rs.setOutput("bottom", true) -- Howler alarm --
print ("Countdown started: 60 seconds untill")
print ("detonation.")
sleep (1.5)
x=59
term.clear()
term.setCursorPos(1,1)
print ("Counting:")
for i=1,59 do
term.setCursorPos(1,2)
term.clearLine()
print (x)
sleep (1)
i=i+1
x=x-1
end
------------
read(input)
if input=="abort" then
print ("Enter abort password:")
read(pass)
if pass=="bejesus" then
rs.setOutput("bottom", false)
term.clear()
term.setCursorPos(1,1)
print("Self-destruct sequence aborted")
end
end
------------

Thx!
remiX #2
Posted 03 January 2013 - 09:24 AM
You can make them into functions and then use
parallel.waitForAny(functionOne, functionTwo)
Or even using event's could work with char events and timer events but would be longer.

Edit: Try this out

term.clear()
term.setCursorPos(1,1)
function detonate()
    rs.setOutput("bottom", true) -- Howler alarm
    print ("Self-destruction sequence initiated")
    print ("Seconds until detonation:")
    for i = 60, 1, -1 do -- Step is now -1 so it decremts for a countdown.
        term.setCursorPos(1, 3)
        term.clearLine()
        write((#tostring(i) == 1 and ('0'..i) or i) .. " " .. (i == 1 and "second" or "seconds"))
        -- This write is quite advanced to put into a single one,
        -- what it is doing is that if the length of i is 1, it
        -- adds a leading '0' for like 05 seconds instead of just
        -- 5 seconds. The second part is that if i is 1
        -- it writes second and not seconds else it will write
        -- seconds for like 06 seconds left, and 01 second left.
        sleep(1)
        -- In for loops you do not have to increment / decrement the integer
    end
    term.clear()
    term.setCursorPos(1,1)
    print("BOOM!")
end
------------
function readInput()
    while true do
        term.setCursorPos(1, 5)
        print("Type 'abort' for a test")
        term.clearLine()
        input = read()
        if input == "abort" then
            term.setCursorPos(1, 5)
            term.clearLine()
            print ("Enter abort password:")
            term.clearLine()
            pass = read("*") -- ("*") will replace what you writing with * for security
            if pass == "bejesus" then -- Nice password :D/>
                rs.setOutput("bottom", false)
                term.clear()
                term.setCursorPos(1,1)
                print("Self-destruct sequence aborted")
                return -- So it will also stop the detonate function
            end
        end
    end
end
------------

parallel.waitForAny(detonate, readInput)
Nolorwolf #3
Posted 03 January 2013 - 10:00 AM
Wow, quick reply, I tried it out with first way (didn't work a lot with functions, failed) I will try second one as soon as I figure it out, I don't want to c/p it, it ruins the point :)/>
Anyway, if you could tell me what is wrong with this code (besides I just dumped things into functions), might be easier now to point a mistake, and that line seams very useful if I knew how to use it :D/>






term.clear()
term.setCursorPos(1,1)


function countdown()
  x=59
  print ("Counting:")
  for i=59,1,-1 do
    term.setCursorPos(1,2)
    term.clearLine()
    print (i)
    sleep (1)
  end
end

function aborting()
  term.setCursorPos(1,3)
  input = read()
  if input == "abort" then
    print ("Enter abort password:")
    pass = read()
    if pass=="bejesus" then
	  rs.setOutput("bottom", false)
	  term.clear()
	  term.setCursorPos(1,1)
	  print("Self-destruct sequence aborted")
    end
  end
end

print ("Self-destruction sequence initiated:")
sleep (2)
rs.setOutput("bottom", true)
print ("Countdown started: 60 seconds untill")
print ("detonation.")
sleep (1.5)
term.clear()
term.setCursorPos(1,1)
parallel.waitForAny(countdown(),aborting())

edit: What I meant, It looks to me like a same code, just yours being better, so what is that difference that makes your work? :)/>
edit2: Edited code a bit more.
remiX #4
Posted 03 January 2013 - 10:27 AM
the parallel.waitForAny(countdown(), aborting()) .. remove the parentheses as you can see in mine
Nolorwolf #5
Posted 03 January 2013 - 10:48 AM
Thank you, that worked! So () is for calling a function, when used as part of command you don't use it (or try both :D/>).
And thx again, that helped me a lot :)/>