# Scriptable systems (singletons)

You can define a custom scriptable system:

```swift
module MyMod

public class MySystem extends ScriptableSystem {
  private func OnAttach() -> Void {
    LogChannel(n"DEBUG", "MySystem::OnAttach");
  }

  private func OnDetach() -> Void {
    LogChannel(n"DEBUG", "MySystem::OnDetach");
  }

  public func GetData() -> Float {
    return GetPlayer(this.GetGameInstance()).GetGunshotRange();
  }
}
```

This scriptable system can then be accessed using the scriptable system container:

```swift
let container = GameInstance.GetScriptableSystemsContainer(gameInstance);
// don't forget the namespace if you're using modules
let system = container.Get(n"MyMod.MySystem") as MySystem;

LogChannel(n"DEBUG", ToString(system.GetData()));
```


---

# Agent Instructions: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://wiki.redmodding.org/redscript/references-and-examples/common-patterns/scriptable-systems-singletons.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
