entity() function
Available since
@irtio/room 0.1Hosted roomsirt dev@irtio/testingSelf-hosted room hostThe entity() function declares an entity kind: its name, its fields and their types, and who may write and see them. The room and its clients import the same declaration.
Syntax
entity(name, fields)
entity(name, fields, options)
Parameters
name- The kind’s name: a letter or
_, then up to 63 letters, digits or_, not starting withirt_. fields- An object of up to 255 fields, each named as
nameis, whose values are a field type such as"u8","f32"or"string", or a declaration fromjson()orstring().id,kindandpatchare reserved names. See the field types table. optionsOptional-
An object with any of these properties:
writable- Fields an entity’s owner may write from the client.
ownerOnly- Fields sent only to the connections of the entity’s owner; every other connection holds their defaults.
hidden- If
true, the kind’s entities are room state that’s never sent to any connection. key- A
stringfield that names each entity, set when it’s created and never changed, so the room and its clients can find it withentities.get(kind, key). onOwnerLeave- What happens to an entity when its owner’s presence ends:
"room", the default, gives it back to the room, and"delete"deletes it.
Return value
The kind, to pass to defineSchema() and to room.entities.
Exceptions
Error- Thrown for an invalid kind or field name, a reserved field name, more than 255 fields, an unknown field type, a
writableorownerOnlyname with no such field, a hidden kind withwritableorownerOnlyfields oronOwnerLeave, akeythat isn’t astringfield or is writable or owner-only, or anonOwnerLeavevalue it doesn’t know. RangeError- Thrown for a kind larger than 61,376 bytes with every field at its initial value.
Description
Neither key nor onOwnerLeave changes the schema hash; the fields, their types and writable, ownerOnly and hidden do, so clients built with another declaration are refused with close code 4004.
Examples
A player kind with owner writes
// ship.ts
import { defineRoom, defineSchema, entity } from "@irtio/room";
const Ship = entity(
"ship",
{ pilot: "string", x: "f32", y: "f32", heading: "angle", cargo: "u16" },
{ writable: ["x", "y", "heading"], ownerOnly: ["cargo"], key: "pilot", onOwnerLeave: "delete" },
);
export default defineRoom({
schema: defineSchema({ entities: [Ship] }),
onJoin(room, connection) {
if (!room.entities.get(Ship, connection.player)) room.entities.create(Ship, { pilot: connection.player }, { owner: connection.player });
},
});
See also
Runtime availability
| Hosted rooms | irt dev | @irtio/testing | Self-hosted room host | |
|---|---|---|---|---|
entity() | 0.1 | 0.1 | 0.1 | 0.1 |