> For the complete documentation index, see [llms.txt](https://wiki.redmodding.org/scripting-cyberpunk/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://wiki.redmodding.org/scripting-cyberpunk/redscript/language-reference/scriptables/scriptablesystem.md).

# ScriptableSystem

[`ScriptableSystem`](https://nativedb.red4ext.com/c/5802799602997948) is a built-in class, they're singletons that are bounded to game sessions as they are created and disposed with game sessions.

## Example

```swift
module MyMod

public class MySystem extends ScriptableSystem {
  public func OnAttach() -> Void {
    FTLog(s"\(this.GetClassName())#OnAttach");
  }

  public func OnDetach() -> Void {
    FTLog(s"\(this.GetClassName())#OnDetach");
  }

  public func OnRestored(saveVersion: Int32, gameVersion: Int32) -> Void {
    FTLog(s"\(this.GetClassName())#OnRestored | \(saveVersion) | \(gameVersion)");
  }

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

  public static final func GetInstance(gameInstance: GameInstance) -> ref<MySystem> {
    return GameInstance.GetScriptableSystemsContainer(gameInstance).Get(n"MyMod.MySystem") as MySystem; // don't forget the namespace if you're using modules
  }
}
```

To get a reference to `MySystem`, all you need to do is to give a `GameInstance` reference:

{% tabs %}
{% tab title="Redscript" %}

```swift
let mySystem: ref<MySystem> = MySystem.GetInstance(GetGameInstance()); // or an existing game instance if you have one
FTLog(s"Data: \(ToString(mySystem.GetData()))");
```

{% hint style="info" %}
If you have defined your system in a module, make sure the module has been imported or define the snippet in the same module.
{% endhint %}
{% endtab %}

{% tab title="Lua" %}

```lua
print("Data: " .. tostring(MyMod_MySystem.GetInstance():GetData()))
```

{% hint style="info" %}
For CET, you don't specify the parameter for `GameInstance` as it's given automagically.
{% endhint %}
{% endtab %}
{% endtabs %}

## Persistence

ScriptableSystems can persist data of all types except for `String`, `Variant`, and `ResRef` (and arrays of these types) between game sessions. If a property is marked as `persistent` it will be stored within a save file and will persist the next time that save file is loaded.&#x20;

For more documentation about persistence, look here: [Persistence](/scripting-cyberpunk/redscript/language-reference/persistence.md)
