# EntityStore: get() method

> The get() method of the EntityStore interface returns the entity with an ID, or the entity of a kind with a key, while it's in the client's view.

The **`get()`** method of the [`EntityStore`](/docs/client/entity-store) interface returns the entity with an ID, or the entity of a kind whose key field holds a key, while it's in the client's view.

## Syntax

```ts
get(id)
get(kind, key)
```

### Parameters

`id`
: An entity ID. 0 is the shared state and 1 the client's private state.

`kind`
: A kind declared with a `key`.

`key`
: A string: the value of the kind's key field to look for.

### Return value

The [`ClientEntity`](/docs/client/client-entity), or `undefined` when none in the client's view has that ID or key.

- `get(id)` returns an entity typed with no particular kind. Compare its `kind` with your kind before reading its fields.
- `get(kind, key)` returns the entity typed as its kind.

### Exceptions

`TypeError`
: Thrown by `get(kind, key)` if the kind has no key.

## Description

Both forms are map lookups, whatever the number of entities, unlike [`all()`](/docs/client/entity-store/all), which visits every entity of a kind.

An entity outside the client's view isn't held, so `get()` returns `undefined` for it even though the room has it. See the [Entity IDs and lookups](/docs/room/guides/state-sync#entity-ids-and-lookups) section of State sync.

## Examples

### Finding a player's hero by player ID

The hero kind is keyed by player ID, so the client finds its own hero, and anyone else's in view, without a loop.

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

const Hero = entity("hero", { player: "string", hp: "u16", gold: "u32" }, { key: "player" });
const schema = defineSchema({ entities: [Hero] });

const room = await connect("dungeon-3", { schema });

room.addEventListener("update", () => {
  const hero = room.entities.get(Hero, room.player);
  if (hero) console.log(`${hero.hp} HP, ${hero.gold} gold`);
});
```

### Following an entity the room names in a message

The room sends the ID of the ball in a message. The client looks it up and checks its kind before reading its fields.

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

const Ball = entity("ball", { x: "f32", y: "f32" });
const schema = defineSchema({ entities: [Ball] });

const room = await connect("pitch-9", { schema });

room.addEventListener("message", ({ data }) => {
  const id = (data as { ball?: unknown } | null)?.ball;
  const found = typeof id === "number" ? room.entities.get(id) : undefined;
  if (found?.kind !== Ball) return;
  const ball = found as ClientEntity<typeof Ball>;
  console.log("ball at", ball.x, ball.y);
});
```

## See also

- [`EntityStore.all()`](/docs/client/entity-store/all)
- [`Entities.get()`](/docs/room/entities/get), the room's side
- [Entity IDs and lookups](/docs/room/guides/state-sync#entity-ids-and-lookups)
