# OwnedEntity

> The OwnedEntity interface of the Client API is an entity the local player owns, whose writable fields the client can set.

The **`OwnedEntity`** interface of the [Client API](/docs/client) is an entity the local player owns: a [`ClientEntity`](/docs/client/client-entity) whose writable fields the client can set. The client's private state, [`room.private`](/docs/client/room-client/private), is one, and so is each entity the room gives the player with [`Entities.setOwner()`](/docs/room/entities/setowner).

[`EntityStore.mine()`](/docs/client/entity-store/mine) lists the entities of a kind the player owns, and [`onCorrect()`](/docs/client/entity-store/oncorrect) passes one to its callback.

## Instance properties

`OwnedEntity` has the properties of [`ClientEntity`](/docs/client/client-entity): `id`, `kind` and a property for each field. The fields the kind lists in `writable` can be set; the rest are read only.

## Instance methods

`OwnedEntity` has the method of [`ClientEntity`](/docs/client/client-entity), [`toJSON()`](/docs/client/client-entity#tojson).

## Description

`OwnedEntity<K>` takes the entity's kind and types its writable fields as writable, so setting any other field is a type error. A kind lists its writable fields in `writable`, as [Declaring writable fields](/docs/room/guides/ownership#declaring-writable-fields) describes.

### Writing fields

Setting a writable field checks the value, changes the client's copy at once and queues the write for the room. Other clients see it once the room has applied it.

- A value of the wrong type throws a `TypeError`, and a value out of its field's range or over its size limit throws a `RangeError`. Both name the field.
- Setting a field that isn't writable, or a field of an entity the player no longer owns, throws a `TypeError`.
- Setting a field of an entity that has left the client's view throws an `Error`.

### Sending writes

In a room with ticks, the client sends writes at most once per room tick, since the room applies them at the tick; until then the last write to a field wins. In a room without ticks, it sends them at the end of the task that made them, and during a burst, such as a drag that writes on every pointer move, at most once per `WRITE_GAP` (16 ms).

The client also sends them before each [`send()`](/docs/client/room-client/send) and call and before [`close()`](/docs/client/room-client/close), so the room applies them before it handles the message, the call or the close. [`flush()`](/docs/client/room-client/flush) sends them at once. Writes travel reliably, so over a real connection an unreliable message can arrive ahead of the writes made before it.

### Corrections

The room has the last word. Its [`writes`](/docs/room/room-definition/writes) handlers can clamp or reject a write, and room code can write the field itself. When the room's value differs from the owner's, the owner gets a correction: the room's value overwrites the client's copy, the client drops its unsent writes to the field, and [`onChange()`](/docs/client/entity-store/onchange) then `onCorrect()` run. Until then, the client keeps its own values for the writable fields it owns and ignores the room's ordinary changes to them.

## Examples

### Saving a player's settings in private state

The private state holds the player's volume, which they set, and their coins, which only the room changes.

```ts
// settings.ts
import { connect, defineSchema, entity } from "@irtio/client";

const Settings = entity("settings", { volume: "u8", coins: "u32" }, { writable: ["volume"] });
const schema = defineSchema({ private: Settings });

const room = await connect("lobby", { schema });

export function setVolume(volume: number): void {
  try {
    room.private.volume = volume;
  } catch (e) {
    // A RangeError for a volume outside 0 to 255
    console.warn("volume not set", e);
  }
}

console.log(`${room.private.coins} coins`);
```

`room.private.coins = 10` is a type error, and throws a `TypeError` if the types are bypassed: coins are the room's to give.

## See also

- [`ClientEntity`](/docs/client/client-entity)
- [`EntityStore.mine()`](/docs/client/entity-store/mine)
- [`EntityStore.onCorrect()`](/docs/client/entity-store/oncorrect)
- [Ownership](/docs/room/guides/ownership#on-the-client)
