So, I have a sickness. I tried, but simply could not leave this alone. What I am about to share is both WAAY overkill, and deeply neurotic. Here’s the thing. I have a strong preference for the zippy gameplay ofgetKey where single character choices are requested, over getLine which requires A WHOLE EXTRA CR. UNACCEPTABLE!!!
I also really, really wanted a countdown indication.
To enable this requires a new, specialized capability based on adv3 inputManager getEventOrKey(). The new method basically combines a narrowed version with a real-time fuse to detect either its firing, or an approved keystroke.
Look on my works ye mighty and despair:
#include <adv3.h>
#include <en_us.h>
versionInfo: GameID;
gameMain : GameMainDef
initialPlayerChar = me
// countdown mgt attributes
count = 5 // would need reset if reused
tiktikboom() { count--; }
showIntro() {
local choice = nil;
"Choose one, quickly:\n\n
\t1. One\n
\t2. Two\n
\t3. Three";
do {
choice =
inputManager.getKeyTimeout(1000,
{: "\n<<charX(count, '.')>> >"}, ['1','2','3'],
self, &tiktikboom);
} while ((count > 1) && (choice == nil));
if (choice == nil)
"\nTime\'s up!<p>";
else
"You chose <<choice>>.<p>";
}
;
startRoom: Room 'Featureless Room'
"This is a featureless room. "
;
+me: Person ;
/* ====================================================================
*
* getKeyTimeout
*
* single key input handler, with timeout, based on input.t getEventOrKey()
* stripped down, special purpose event handler, starts realtime fuse
* and returns entered key or nil if times out
* Parameters:
* timeout - time value in ms
* promptFunc - function that displays prompt for input
* validKeys - optional list of keys to accept. if not provided (nil)
* accept any key
* fuseObj - optional object that implements method executed on fuse
* expiry. if none provided, default to do-nothing
* fuseProp - if fuseObj specified, pointer to method executed on
* expiry. if none provided, default to local
* placeholder do-nothing method
*
* Returns:
* character typed, or nil if none (timed out)
*
* ====================================================================
*/
modify inputManager
gkTimeoutMethod() { } // default do-nothing timeout trigger
getKeyTimeout(timeout, promptFunc, validKeys?,
fuseObj=self, fuseProp=&gkTimeoutMethod) {
// make sure the command transcript is flushed
if (gTranscript != nil)
gTranscript.flushForInput();
local toFuse = new RealTimeFuse(fuseObj, fuseProp, timeout);
// keep going until we get a keystroke or timeout
for (;;) {
local result;
local timedOut;
// process real-time events, if possible
timedOut = processRealTimeEvents(true);
// show the prompt and any pre-input codes
inputEventBegin(promptFunc);
getInput:
// Read the input. (Note that if our timedOut is nil, this
// will simply act like the ordinary untimed getKey.)
result = aioInputEvent(timedOut);
// check the event code from the result list
switch(result[1]) {
case InEvtNoTimeout:
// the platform doesn't support timeouts - note it for
// future reference so that we don't ask for input with
// timeout again, then go back to try the input again
// without a timeout
//
noInputTimeout = true;
timedOut = nil;
goto getInput;
case InEvtTimeout:
// We got a timeout without finishing the input line.
//
inputEventEnd();
return nil;
case InEvtKey:
// keystroke - if valid finish the input and return the value
//
if ((validKeys == nil) || validKeys.indexOf(result[2])) {
toFuse.removeEvent();
inputEventEnd();
"<<result[2]>>\n";
return result[2];
} else
break;
default:
// ignore other events
break;
}
}
}
;
/* ====================================================================
*
* charX utility function
*
* Produces a string of requested length of repeated character
* used to align proportional font output
* Parameters:
* num - repeat count
* char - character to repeat. If not given, default to unicode blank
*
* Returns:
* repeated string
*
* ====================================================================
*/
charX(num, char = '\u2007') {
local str = '';
if (num == 0) return '';
for(local i = 1; i<= num; i++) str += char;
return str;
}
This provides output like so
Choose one, quickly:
1. One
2. Two
3. Three
..... >
.... >
... >3
You chose 3.
Featureless Room
This is a featureless room.
>restart
Do you really want to start over? (Y is affirmative) > y
Choose one, quickly:
1. One
2. Two
3. Three
..... >
.... >
... >
.. >
. >
Time's up!
Featureless Room
This is a featureless room.
>
That’s it! No one may ever use it, but now I AM FREE!!!