# RoomDefinition: onTick() hook

> The onTick() hook of a room definition runs each tick, after the stepper, in a room with a tickRate.

The **`onTick()`** hook of a [room definition](/docs/room/room-definition) runs each tick, after the stepper. Only a room with a [`tickRate`](/docs/room/room-definition/tickrate) has ticks.

## Syntax

```ts
defineRoom({
  tickRate: 20,
  onTick(room, dt) {},
});
```

### Parameters

`room`
: The [`Room`](/docs/room/room).

`dt`
: The fixed timestep in seconds, `1 / tickRate`.

### Return value

None, or a `Promise`, which the room doesn't wait for.

### Exceptions

If `onTick` throws, or its promise rejects, the room stops and its connections close with close code 4007.

`defineRoom()` throws an `Error` for a definition with `onTick` and no `tickRate`, and the types reject it too.

## Description

Each tick applies owner writes through [`writes`](/docs/room/room-definition/writes), runs the [stepper](/docs/room/room-definition/stepper) with this tick's inputs, then runs `onTick`, then sends the state update to every connection. What `onTick` changes goes out in that tick's update.

Inside `onTick`:

- `room.tick` is the number of the tick running, 1 for a new room's first.
- Each connection's `input` is the input applied this tick, so `onTick` can act on a shot with the `viewTick` it was aimed at.

Ticks run with a fixed timestep. After a stall a room runs at most 4 ticks to catch up and drops the rest. Ticks stop while a room is hibernated, and none run on wake to catch up; time spent hibernated is in [`onWake()`](/docs/room/room-definition/onwake)'s `elapsed`. Use `room.now()` or an alarm, not a count of ticks, for anything timed in real time across hibernation.

## Examples

### Rising lava

```ts
// lava.ts
import { defineRoom, defineSchema, entity } from "@irtio/room";

const Level = entity("level", { lava: "f32" });

export default defineRoom({
  schema: defineSchema({ state: Level }),
  tickRate: 20,
  onTick(room, dt) {
    room.state.lava = Math.min(room.state.lava + 0.5 * dt, 40);
  },
});
```

## See also

- [`RoomDefinition.tickRate`](/docs/room/room-definition/tickrate)
- [`Room.tick`](/docs/room/room/tick)
- [Ticks](/docs/room/sdk#ticks)
