Why does my on-keypress script freeze/produce errors when I hold down a key?
From OHRRPGCE-Wiki
If you use on-keypress scripts which contain wait commands (or commands with an implicit wait - check the Plotscripting Dictionary) your scripts might be in danger of piling up, producing strange behaviour, often freezing, and possibly script buffer overflows.
[edit] When on-keypress scripts are called repeatedly
The onkeypress script is run every tick that any key is held down and the "topmost" currently running script is NOT the onkeypress script (unless you set the "Permit double triggering of scripts" bitset, which forgoes this safety check) and the topmost script is not using plot:wait for key.
script, my onkeypress script, begin handle movement end script, handle movement, begin if (key is pressed (key: up)) then ( #... wait (2) ) end
The wait is inside handle movement, so to the engine it appears the on-keypress script is not the currently running script, and it calls it again next tick if any buttons as pressed. As you hold down the up key, it'll get called over and over.
[edit] Fixing the Problem
Use a global variable (or a tag) to indicate that the on-keypress script is already running. A local variable will NOT work.
global variable (10, keyhandler running)
script, my onkeypress script, begin
if (keyhandler running == false) then (
keyhandler running := true
# call other scripts, wait commands, whatever
# ...
keyhandler running := false
)
end
Now the script will immediately exit if it is called while already running.
