# entity() function

> The entity() function declares an entity kind, its name and fields, for a room's schema.

The **`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

```ts
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 with `irt_`.

`fields`
: An object of up to 255 fields, each named as `name` is, whose values are a field type such as `"u8"`, `"f32"` or `"string"`, or a declaration from [`json()`](/docs/room/json) or [`string()`](/docs/room/string). `id`, `kind` and `patch` are reserved names. See the [field types table](/docs/room/guides/state-sync#declaring-kinds).

`options` Optional
: 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 `string` field that names each entity, set when it's created and never changed, so the room and its clients can find it with `entities.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()`](/docs/room/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 `writable` or `ownerOnly` name with no such field, a hidden kind with `writable` or `ownerOnly` fields or `onOwnerLeave`, a `key` that isn't a `string` field or is writable or owner-only, or an `onOwnerLeave` value 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

```ts
// 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

- [Declaring kinds](/docs/room/guides/state-sync#declaring-kinds)
- [Ownership](/docs/room/guides/ownership)
- [Hidden entities](/docs/room/guides/state-sync#hidden-entities)
