string() function

Available since @irtio/room 0.1Hosted roomsirt dev@irtio/testingSelf-hosted room host

The string() function declares a string field of at most max characters, for entity() or call().

Syntax

string(max)

Parameters

max
The most characters, a whole number from 1 to 16,384, counted as value.length counts them, the way <input maxlength> does.

Return value

A field declaration, typed string.

Exceptions

RangeError
Thrown if max isn’t a whole number from 1 to 16,384.

Description

A longer value is refused like any value over its field’s limit: a write or create throws a RangeError, and a call arg fails the call’s check. The 16 KB of UTF-8 every string field has still applies. The bound is part of the schema hash.

Examples

Bounding a player’s chosen name

// names.ts
import { call, defineCalls, defineRoom, defineSchema, entity, string } from "@irtio/room";

const Tag = entity("tag", { player: "string", text: string(16) }, { key: "player" });
const schema = defineSchema({ entities: [Tag], calls: defineCalls({ rename: call({ text: string(16) }) }) });

export default defineRoom({
  schema,
  calls: {
    rename({ text }, { room, connection }) {
      const tag = room.entities.get(Tag, connection.player) ?? room.entities.create(Tag, { player: connection.player });
      tag.text = text;
    },
  },
});

See also

Runtime availability

Hosted roomsirt dev@irtio/testingSelf-hosted room host
string()0.10.10.10.1