2

To clarify, I am using ComputerCraft (emulator: http://gravlann.github.io/, language: Lua)

I know that to wait for a keypress

os.pullEvent("key")

and to wait 5 seconds I need to use this

sleep(5)

but I want to wait for a keypress and disable the event waiter after 5 seconds.

Piper McCorkle
  • 1,044
  • 13
  • 27

1 Answers1

2

I'm not familiar with ComputerCraft API, but I guess, You could use parallel API for this. Basically, it allows executing two or more functions in parallel.

To be specific - parallel.waitForAny. Which returns after any of function finished, so, only the one being executed. In contrary, parallel.waitForAll waits for all functions being executed.

I'd use something like this:

local action_done = 0

local function wait_for_keypress()
    local event, key_code = os.pullEvent("key")

    --do something according to separate key codes? :}
end

local function wait_some_time()
    sleep(5)
end

action_done = parallel.waitForAny(wait_for_keypress, wait_some_time)
--action done now contains the number of function which was finished first.

EDIT: if using only ComputerCraft API, You should change to this (using timer event):

local function wait_some_time()
    os.startTimer(5)
end
Kamiccolo
  • 7,758
  • 3
  • 34
  • 47