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

[Lua] [Question] possible to stop read()?

Started by Illmatar, 26 July 2012 - 03:00 AM
Illmatar #1
Posted 26 July 2012 - 05:00 AM
I am pretty new to programming in general have been working with computer craft for a bit and am writing a program that takes input only during a period of time but if they don't enter anything the read() that is called prevents the program from acting correctly so I am wondering if it is possible to put a timer in the read() call.

So is it possible to stop a read() call or maybe stop a function that has the read() in it?

If you need more info just let me know what you need.

I have not found anything on this so I am guessing it is not possible or just not easily done so any help on this is greatly appreciated.
KingMachine #2
Posted 26 July 2012 - 05:49 AM
parallel.waitForAny(getInput, startTimer)
Wait for "any" because when either one finishes it will cancel the whole parallel function and whichever one finished will have done it's thing and the other one I BELIEVE ends prematurely.

Your "getInput" function will either have to return your value or set a global variable. I'm honestly not sure which.
If you're returning then I assume it would look like this…

function getInput()
return read()
end --end function

function startTimer()
--paraphrasing currentTime + timerLengthTime = goalTime
○while bTimerGoing do
○time = currentTime
○if time >= goalTime then
○○bTimerGoing = false
○end--end if
end--end while
return nil
end--end function

input = parallel.waitForAny(getInput, startTimer)

if input == nil then
○fail = true
○elseif input ~=nil then
○win = true
end


I don't know the exact api for time, so I was paraphrasing as stated. But you get the idea. This is the simplest idea I could come up with in 5 minutes, there are probably more precise ways of doing this.

EDIT: Also, I'm not sure if you can use parallel as a return like that. I should get that computercraft emulator and find out…

EDIT: Cleaned up the code formatting, I'm used to coding in-game :)/>/>
ALSO: ○ = tab
Illmatar #3
Posted 27 July 2012 - 01:40 AM
That resolved the read() issue but ran into another but that is all good just had to change how I did the timer.

Thanks for the help!