> 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/api-lifetime-rules.md).

# API lifetime rules

## Context content APIs

Prefer `context.Items.Add(item)`, `context.Buffs.Add(buff)` and `context.Skills.Add(skill)` in `Load(ModContext context)`. The owning context is supplied automatically. Use `context.Items.Give(item)` and `context.Buffs.Apply(buff, target, turns)` for operations. Inside item, consumable, buff and skill subclasses, use the inherited `Context` property, for example `Context.Buffs.Apply(buff, target, 3)`.

This allows your mod to use namespaces such as `MyMod.Items`, `MyMod.Buffs` and `MyMod.Skills` without qualifying the StoneForge static classes. The old static APIs continue working. Registration, duplicate checks and unload cleanup share their existing implementations; content keys remain game-wide and must still be unique. Item queries and buff operations are not restricted to content owned by the context.

## Instances, handles and assets

* Store room instances as `Instance` or call `Persist()` for an explicitly ID-only reference. Check `Exists` before use; IDs are identities in the running game, not save-file identities.
* An `Instance` handed to a callback is good for that callback; keep one longer with `Persist()`. A struct read through `Instance` (not as a `GmStruct`) is the same. Arrays and structs held as `GmArray` / `GmStruct` are kept alive for the game's garbage collector until they're disposed or C# stops referring to them (see [Values](/stoneforgeteam-docs/gml-and-c-code/values.md)).
* Native function failures raise `GameCallException`. Missing variable reads still return `Undefined`. Native engine crashes or GML failures that bypass the bridge's status return are not converted into recoverable managed exceptions.
* Load sprites and sounds through `ModContext` for ownership tracking. Do not manually delete an owned sprite/stream: the loader needs its ID for retirement and cleanup.
* A paused mod keeps imported sprites alive until unload. Its registered callbacks stop, and its windows close on the next frame. Resetting failure state/reloading is explicit.


---

# 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/api-lifetime-rules.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.
