Key (thing)

From MZXWiki
(Redirected from Held key)
Jump to navigation Jump to search
Key # 39 Id39.png
Robotic name Key
Default color none
Default param 0
ID char 12 (#39)
Attributes Pushable: any
Effect when
touched
If held keys aren't full: give held key, become Space, move Player;
Otherwise: block Player

Key (ID 39, Key) is a built-in item.

When touched by the Player, the Player is given a held key, the message You grab the key. displays, SFX 8 (Key) plays, the Key is replaced with a Space, and the Player moves on top of the new Space. If the Player already has 16 held keys, the message You can't carry any more keys! displays, SFX 9 (Full Keys) plays, and the Key acts as a wall that blocks the Player from moving.

The color of the held key given by a Key is derived from the foreground color of the Key, i.e. if the Key has color ce2, the Player will receive a held key of color 2 when the Key is collected.

Held keys

The Player can have, at most, 16 held keys of any color. Keys currently held by the Player can be viewed using the game menu (Enter) in most games.

When the Player touches a locked Lock, Door, or Gate, held keys are used to unlock and open them. The Robotic commands GIVEKEY, GIVEKEY ELSE, TAKEKEY, and TAKEKEY ELSE can be used to interact with held keys. The Robotic command OPEN may consume held keys when used to open locked Doors/Gates.

Simulating ZZT keys with Robotic

At most one Key of each color can be held at a time in ZZT. This can be simulated in MegaZeux by using Robots instead of built-in Keys and the following program:

end
: "touch"
takekey "this_color" else "give"
givekey "this_color"
sfx 9
end
: "give"
givekey "this_color"
sfx 8
die as an item

(It is not possible to make these ZZT "keys" pushable without breaking how the Player interacts with them.)

Counting held keys with Robotic

Counting the Player's current held keys can be useful for implementing a custom HUD. The current held keys can be counted with this Robotic fragment:

set "commands" to "max&commands&,1000"
set "local" to 0
: "count"
set "num_keys_&local&" to 0
loop start
takekey "local" else "all_keys"
inc "num_keys_&local&" by 1
loop for 15
: "all_keys"
if "num_keys_&local&" = 0 then "no_giveback"
loop start
givekey "local"
loop "('num_keys_&local&'-1)"
: "no_giveback"
inc "local" to 1
if "local" < 16 then "count"

Note that the original order of the held keys can not be determined by Robotic. This code will sort the held keys as viewed by the game menu, as it removes every held key and then return the observed number of each type of held key.

Version differences

In MegaZeux 1.x, Keys display the message You grab the [color] key. when collected, where [color] corresponds to the foreground color of the collected Key:

Color Name
0 black
1 dark blue
2 dark green
3 dark cyan
4 dark red
5 dark purple
6 brown
7 light grey
8 dark grey
9 blue
10 green
11 cyan
12 red
13 purple
14 yellow
15 white

This also applies to Lock—which displays the message You unlock the [color] lock.—but not to Door or Gate.

ZZT connection

In ZZT, the equivalent element to Key is zzt:Key. In ZZT, the Player can only hold 7 keys (not counting buggy black keys), and can hold at most only one key of each color.