I want to make a program that repeats a function until a person presses a key or mabye even types something. I am making a program that is compatible with a wheat harvester.
What I am looking for is that when I run the program the program will not stop until I press any key.
Repeat until user input
Started by SNWLeader, Nov 27 2012 05:38 PM
7 replies to this topic
#1
Posted 27 November 2012 - 05:38 PM
#2
Posted 27 November 2012 - 05:52 PM
My quick and dirty technique is the following:
local function yourFunction()
-- loop here
end
parallel.waitForAny(yourFunction, function() os.pullEvent('key') end)
This will run yourFunction until it returns or a key is pressed.
#3
Posted 28 November 2012 - 03:18 AM
while os.pullEvent()~='key' do --your code os.startTimer(loop delay) end
#5
Posted 29 November 2012 - 03:05 PM
I figured one out.
repeat ----code wanting to repeat until os.pullEvent() = 'key'
#6
Posted 30 November 2012 - 09:54 AM
Yes, but yours will stall unless the code you repeat generates events (which apparently it does). Kaos' "os.startTimer(loop delay)" prevents that, but you need to replace "loop delay" with a numeric value indicating the maximum time you'll pause before starting the next iteration of the loop. I agree with your use of repeat...until rather than while ~= do...end, it is structurally more appropriate to the particular task at hand. If the code you've inserted is such that it generates/receives sufficient events to keep the loop running, then that is all well and good.
I fear that you're pulling and discarding at least some events that you actually want to use, though.
I fear that you're pulling and discarding at least some events that you actually want to use, though.
#7
Posted 30 November 2012 - 10:01 AM
you are most correct on the use of the repeat until loop, I derped out there
you shouldn't lose any timers, perhaps rednet messages if you get them at the wrong time but if you want to use os.pullEvent() in your code let us know and we will give you an adaptation
#8
Posted 30 November 2012 - 06:00 PM
Ya I had to add the os.startTimer(loopdelay)
the program would run the desired code once then stop working the os.startTimer(loopdelay) fixes that.
the program would run the desired code once then stop working the os.startTimer(loopdelay) fixes that.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users












