# ClientEntity

> The ClientEntity interface of the Client API is one entity as a client sees it, with a read-only property for each field of its kind.

The **`ClientEntity`** interface of the [Client API](/docs/client) is one entity as a client sees it: the shared state [`room.state`](/docs/client/room-client/state), a presence entry or one of the room's entities in the client's view. It has a read-only property for each field of its kind, and `id`, `kind` and `toJSON()`.

Game code never constructs a `ClientEntity`. The client makes one when an entity enters its view, and [`EntityStore`](/docs/client/entity-store) methods such as [`get()`](/docs/client/entity-store/get) return them. An entity the local player owns is an [`OwnedEntity`](/docs/client/owned-entity), whose writable fields can be set.

## Instance properties

`ClientEntity.id`
: The entity's ID, a number: 0 for the shared state, 1 for the client's private state and from 2 for the room's entities. It's the same ID the room's [`RoomEntity`](/docs/room/room-entity) has. Read only.

`ClientEntity.kind`
: The entity's kind, the object [`entity()`](/docs/room/entity) returned in the client's schema. Compare it with your kinds to tell what an entity from `get(id)` is; `kind.name` is the kind's name. Read only.

## Instance methods

[`ClientEntity.toJSON()`](#tojson)
: Returns a plain copy of the entity: its ID, its kind's name and its fields' current values.

## Description

`ClientEntity<K>` takes the entity's kind, `typeof` what `entity()` returned, and types each field property with its field's type.

### Field values

Each field of the kind is a read-only property of the entity. Reading it gives the value of the last state update the client applied. The object stays the same while the entity is in view, and its values change with each state update, so a reference to it always reads the current values.

- Setting a field throws a `TypeError`, unless it's a writable field of an entity the local player owns. A client changes the room's entities by sending the room a message or a call; see [`RoomClient.send()`](/docs/client/room-client/send).
- The fields are accessors on a prototype shared by the kind's entities, so `{ ...entity }` copies only `id` and `kind`. Use [`toJSON()`](#tojson) to copy the fields.
- After the entity leaves the client's view, reading it still gives the values it last had.

### `toJSON()`

The **`toJSON()`** method returns a plain copy of the entity.

```ts
toJSON()
```

It takes no parameters and returns an object with the entity's `id`, its `kind` as the kind's name, a string, and a property for each field with its current value. The copy doesn't change with later state updates. `JSON.stringify()` calls it, so stringifying an entity gives its fields. It throws nothing.

## Examples

### Logging a copy of the shared state

A game logs the shared state as it was when the round ended, in a copy that later updates don't change.

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

const Round = entity("round", { number: "u8", winner: "string" });
const schema = defineSchema({ state: Round });

const room = await connect("quiz-night", { schema });
const history: ReturnType<typeof room.state.toJSON>[] = [];

room.entities.onChange(schema.state, (round, changed) => {
  if (!changed.includes("winner") || round.winner === "") return;
  history.push(round.toJSON());
  console.log(JSON.stringify(round)); // {"id":0,"kind":"round","number":1,"winner":"ada"}
});
```

## See also

- [`EntityStore`](/docs/client/entity-store)
- [`OwnedEntity`](/docs/client/owned-entity)
- [`RoomEntity`](/docs/room/room-entity), the room's side
- [State sync](/docs/room/guides/state-sync#on-the-client)
