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

Random number generator and having the computer wait

Started by tommynov1000, 12 April 2014 - 05:18 PM
tommynov1000 #1
Posted 12 April 2014 - 07:18 PM
So, I know how to make a random number generator, but I also have no idea how to get the computer to do two things at once. Specifically, I have a monitor hooked up to a computer with the abilty to have touch screen. Basically, what I want to do is generate a random number every t seconds while also waiting for the user to press the big red stop button on the monitor. Once the button is pressed, it'll stop generating numbers and do something else. I was thinking that sleep(t) would work, but I believe that it just makes the computer do absolutely nothing for a while.
Bomb Bloke #2
Posted 12 April 2014 - 11:39 PM
os.startTimer() and os.pullEvent() are what you're after here.

local myTimer = os.startTimer(t)
local myEvent

while true do
	myEvent = {os.pullEvent()}
	
	if myEvent[1] == "timer" and myEvent[2] == myTimer then
		print(math.random(10))
		myTimer = os.startTimer(t)
		
	elseif myEvent[1] == "monitor_touch" and myEvent[3] >= buttonLeftSide and myEvent[4] >= buttonTop and etc then
		break
	end
end
Edited on 12 April 2014 - 09:40 PM