# Useful Functions

Section: Usage

KEYPRESS :-

key_pressed
key_pressed_with_question "..."

This optionally displays a message and waits for a key press, returning the numerical key code of the key pressed.

For example,

if the key_pressed is 65 or the key_pressed is 127 begin;
" 'A' pressed.";
end if;

while the key_pressed_with_message "Press SPACE to continue." <> 32 begin;
end while.

YESNO :-

This optionally displays a message and waits for the player to press 'Y' or 'N'. The result of the answer is either "YES" or "NO".

PAUSE_CLEAR :-

pause_clear.
pause_clear " ....".

say "[pause_clear]";
say "[pause clear some_text]";

This optionally displays a message and waits for the player to press a key. It then clears the screen and redraws the status line.

PERCENTAGES :-

say "x percentage of y";

This calculates the percentage value of a number divided by another number. The default precision is 2 decimal points and the default suffix is "%". These can be changed by the following statements:-

The percentage_precision is x.
The percentage_suffix is " ... ".

e.g,
say "[8 percentage of 12]";

RANKINGS:-

Table of Strength
 rank "puny" "weak" "average" "strong" "muscle-bound"

Table of Dexterity
 maxpercent rank 10 "completely uncoordinated" 25 "uncoordinated" 75 "coordinated" 100 "marksman"

let ranktext be EqualRankOf 5 out_of 12 from_table Table of Strength;
say "[EqualRankOf 3 out_of 10 from_table Table of Strength]";
say "[RangedlRankOf 3 out_of 10 from_table Table of Dexterity]";

These functions use the values given to look up a piece of text indicating the ranking of the initial value from the max value. EqualRank applies equal weightings to each "level" within the table. PercentageRank uses a percentage lookup. The first column is the MAXIMUM percentage that the value needs to be to reach the rank.

SQUARE ROOT:-

let num be the square_root of 64;

This is a moderately inefficient function for calculating the square root of a value. Due to the fact that Inform7 has a very low integer precision, this function only returns the integer approximation (rounded down) of the square root, and stops working properly for numbers above +32767.