ReferenceVerse

Map Keys Unique

map keys unique

Updated
Watch the Knot — map-keys-unique in ~90 seconds.

A pirate tosses a second glowing blue crystal into the grotto pool, and the scoreboard instantly overwrites their first find with the new, higher bounty.

In Verse, a map stores key-value pairs, but every key must be totally unique. When you assign a new value to an existing key like our Grotto zone, it doesn't add a second entry—it completely replaces the old one.

We declare a map to track crystal bounties, then set the Grotto key to ten. When we set that exact same Grotto key to fifty, the ten is erased. We read the single surviving value and push it to our billboard device.

Reach for maps when you need to track one definitive state per player or zone, knowing a duplicate key will always cleanly overwrite the past.

MyText<localizes>(S:string):message = "{S}"
grotto_map := class(creative_device):
    @editable
    Board : billboard_device = billboard_device{}
    OnBegin<override>()<suspends>:void =
        var Crystals : [string]int = map{}
        set Crystals["Grotto"] = 10
        set Crystals["Grotto"] = 50
        if (Count := Crystals["Grotto"]?):
            Board.SetText(MyText("Grotto: {Count}"))

Build your own lesson with map-keys-unique

Generate a personalized, compile-checked lesson grounded in this exact reference.

Build a lesson →