> For the complete documentation index, see [llms.txt](https://stoneforgeteam.gitbook.io/stoneforgeteam-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://stoneforgeteam.gitbook.io/stoneforgeteam-docs/core-concepts/events.md).

# Game events

StoneForge tells a mod what happens in the game: a unit dies, a door opens, a save is written, the player picks something up. Each event is registered from the class it's about, as `Area.OnSomething(context, handler)`:

```csharp
Units.OnDied(context, (unit, killer) =>
{
    if (Units.IsPlayer(killer))
        context.Log($"You killed {ActionsLog.NameOf(unit)}");
});
Doors.OnChanged(context, (door, open) => context.Log(open ? "A door opens" : "A door closes"));
Rooms.OnEntered(context, room => context.Log($"Entered {Rooms.CurrentName}"));
```

Register them in `Load`. A mod's handlers go with it when it's switched off, and an exception in one is that mod's, as with any other handler: it's reported, and a mod that keeps failing is paused. You don't need `[assembly: HookScript]` for any of these; StoneForge makes the scripts they need hookable itself.

For anything not listed here, hook the game's own scripts and events: see [Hooks](/stoneforgeteam-docs/gml-and-c-code/hooks.md).

## Every event

| Event                                                                                                                                    | Runs                                                                                                          |
| ---------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------- |
| **World**                                                                                                                                |                                                                                                               |
| [`Rooms.OnEntered(room)`](/stoneforgeteam-docs/world/rooms-and-saves.md#entering-and-leaving-rooms)                                      | Once the game has gone into a room of the game being played, its instances set up. Each dungeon floor counts. |
| [`Rooms.OnLeaving(room)`](/stoneforgeteam-docs/world/rooms-and-saves.md#entering-and-leaving-rooms)                                      | As the game leaves one, its instances still there.                                                            |
| [`Turns.OnTurn()`](/stoneforgeteam-docs/world/units.md#turns)                                                                            | After each world turn has passed.                                                                             |
| [`Locations.OnSaved(preset)`](/stoneforgeteam-docs/world/locations-and-items.md#locations)                                               | The game has saved the place the player is leaving.                                                           |
| [`Doors.OnChanged(door, open)`](/stoneforgeteam-docs/world/units.md#doors)                                                               | A door starts opening or closing, whoever does it.                                                            |
| [`MapMarkers.OnPlaced(marker)`, `OnRemoved(marker)`](/stoneforgeteam-docs/world/time-and-map.md#map-markers)                             | The player places a marker on the world map or takes one off.                                                 |
| **Saves**                                                                                                                                |                                                                                                               |
| [`SaveData.OnLoaded(save)`](/stoneforgeteam-docs/world/rooms-and-saves.md#when-saves-are-read-and-written)                               | A save has been read, before the game sets itself up from it.                                                 |
| [`SaveData.OnSaving(save)`](/stoneforgeteam-docs/world/rooms-and-saves.md#when-saves-are-read-and-written)                               | A save is about to be written: the save data can still be changed.                                            |
| **Units and combat**                                                                                                                     |                                                                                                               |
| [`Units.OnSpawned(unit)`](/stoneforgeteam-docs/world/units.md#when-units-come-and-go)                                                    | A unit comes into play: summoned, spawned, made by a mod.                                                     |
| [`Units.OnDied(unit, killer)`](/stoneforgeteam-docs/world/units.md#when-units-come-and-go)                                               | A unit dies, before it's destroyed.                                                                           |
| [`Combat.OnAttack(attack)`, `OnHit(attack)`](/stoneforgeteam-docs/content/combat.md#every-attack)                                        | Any attack is resolved; any attack strikes.                                                                   |
| [`Skills.OnUsed(cast)`](/stoneforgeteam-docs/content/skills.md#any-skill-used)                                                           | Anyone uses a skill.                                                                                          |
| **The player**                                                                                                                           |                                                                                                               |
| [`Player.OnDying()`](/stoneforgeteam-docs/world/player.md#death-and-levelling-up)                                                        | The player is about to die. Return true to stop it.                                                           |
| [`Player.OnLevelUp(level)`](/stoneforgeteam-docs/world/player.md#death-and-levelling-up)                                                 | The player levels up.                                                                                         |
| [`Quests.OnStarted`, `OnProgress`, `OnCompleted`, `OnFailed`](/stoneforgeteam-docs/world/player.md#quests)                               | A quest starts, moves on a step, is done or fails.                                                            |
| **Items**                                                                                                                                |                                                                                                               |
| [`Inventory.OnAdded(item)`, `OnRemoved(item)`, `OnEquipped(item, on)`](/stoneforgeteam-docs/world/inventory-and-containers.md#inventory) | The player gains or loses an item, puts one on or takes one off.                                              |
| [`Containers.OnOpened`, `OnClosed`, `OnItemAdded`, `OnItemRemoved`](/stoneforgeteam-docs/world/inventory-and-containers.md#containers)   | A container opens or closes; an item goes into an open one or comes out.                                      |
| [`GroundItems.OnAdded(item)`, `OnRemoved(item)`](/stoneforgeteam-docs/world/locations-and-items.md#ground-items)                         | An item comes onto the ground or leaves it.                                                                   |
| **UI**                                                                                                                                   |                                                                                                               |
| [`ContextMenus.OnOpen(menu)`](/stoneforgeteam-docs/ui/context-menus.md)                                                                  | A right-click menu opens.                                                                                     |

## What counts

Events report what happens **in play**. The units, items and doors a place already has as it loads, or a room is built with, don't count as spawned or added. Where an event says "on the frame after", the thing is set up by then (its data filled in); one that's gone again by then isn't reported.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://stoneforgeteam.gitbook.io/stoneforgeteam-docs/core-concepts/events.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
