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

[Question] Input Sleeping(Yielding)

Started by Grim Reaper, 16 May 2012 - 12:37 AM
Grim Reaper #1
Posted 16 May 2012 - 02:37 AM
I was curious as to if you could use read() or os.pullEvent() with an added argument to only accept input for a specified amount of time. An example as to where this might be useful would be:

If you had a menu with a moving background or image (like a character moving) and you wanted this character to change positions every half second but you still wanted to be accepting input at the same or roughly the same time. So the user could press the arrow keys to navigate the menu whilst the background is shifting through its phases.

The reason read() or os.pullEvent() won't work for this is because if the user is thinking about his/her decision on what option to pick, the background will lay motionless until a key is pressed to trigger the background change.

Any ideas?

Thanks in advance, PaymentOption.
Luanub #2
Posted 16 May 2012 - 02:52 AM
Use os.pullEvent() with a timer. So something like this.


os.startTimer(5)
local evt, p1, p2 == os.pullEvent()
if evt == "key" then
   do key stuff
elseif evt == "timer" and p1 == "timer" then
  do timer stuff
end
Grim Reaper #3
Posted 16 May 2012 - 03:49 AM
Thank you!
MysticT #4
Posted 16 May 2012 - 06:00 PM
Or use the parallel API:

local function drawBackground()
  while true do
    -- draw the background or whatever
    sleep(1) -- to let the other function work
  end
end

local function getInput()
  while true do
    local s = read()
    -- do something with the input
  end
end

parallel.waitForAny(getInput, drawBackGround)
I would work almost the same, choose what you think it's better for what you need.