How do I script a wait that the player can skip by pressing a key?

From OHRRPGCE-Wiki
Jump to navigation Jump to search

Sometimes you may want a script to wait for a certain amount of time, maybe for a dramatic pause, or to allow a musical flourish to finish, but you also want to allow the impatient player to skip that pause by pressing any use key or cancel key (including on a joystick). Here is an example script that implements an interruptible wait.

If you want to skip on any key, modify the script to use instead

    if (key is pressed (any key)) then (exit returning (true))

If you want to skip a whole series of actions, such as a cutscene, an extended article is at Scripts:Skippable cutscene

script, skippable wait, ticks, begin
  # Returns true if the wait was cancelled by a keypress.
  variable (i)
  for (i, 1, ticks) do (
    wait (1)
    if (key is pressed (use key) || key is pressed (cancel key)) then (exit returning (true))
  )
  exit returning (false)
end

And below is a wrapper script that allows you to specify the delay in seconds rather than in ticks.

script, skippable wait seconds, seconds, begin
  # Returns true if the wait was cancelled by a keypress.
  exit returning (key wait (1000 * seconds / read general (198)))  # Read the value of "milliseconds per frame"
end