> 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/persistence.md).

# Persistence

The fields of classes that extends [`ScriptableSystem`](/scripting-cyberpunk/redscript/language-reference/scriptables/scriptablesystem.md), [`ScriptableService`](/scripting-cyberpunk/redscript/language-reference/scriptables/scriptableservice.md) (only with [Codeware](/scripting-cyberpunk/introduction/tools-and-frameworks/codeware.md) installed), and [`PersistentState`](https://nativedb.red4ext.com/c/8840884683507736) (e.g. [`ScriptedPuppetPS`](https://nativedb.red4ext.com/c/5380750630101078) for the player and NPCs) can be declared with the `persistent` modifier to be persisted in their respective contexts.

* `ScriptableSystem`: global storage scoped to save file,
* `PersistentState`: entity scoped storage,
* `ScriptableService`: global storage shared across all saves.

{% hint style="info" %}
For persistent fields of `ScriptableSystem` and `PersistentState`, data will be removed the next time the save file is written without the mod (this also means if the mod is temporarily disabled/removed), the data will be lost and cannot be recovered other than by loading an older save.
{% endhint %}

You can persist all types of data except for `String`, `Variant` and `ResRef` (and arrays of these types). Instances of classes can also be persisted as well, but note that their fields must also be marked with `persistent` as well or they won't be persisted and instead will be initialized with defaults.

{% hint style="info" %}
If you want to save a `String`, you can instead save the data as `CName` and [convert](/scripting-cyberpunk/redscript/language-reference/utility-functions/string-functions.md#string-conversions) it to a `String`/`CName` when you want to use/store the field.
{% endhint %}

## Scriptables

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

```swift
public class MySystem extends ScriptableSystem {
  public persistent let m_amount: Int32;

  public persistent let m_id: TweakDBID;

  // ...
```

{% hint style="info" %}
Persistent fields of `ScriptableSystem` are saved directly into a save file, meaning that values can differ from different save files.
{% endhint %}
{% endtab %}

{% tab title="ScriptableService" %}

```swift
public class MyService extends ScriptableService {
  public persistent let m_amount: Int32;

  public persistent let m_id: TweakDBID;
  
  // ...
```

{% hint style="info" %}
Persistent fields of `ScriptableService` are saved globally, the values do not differ with save files.
{% endhint %}
{% endtab %}
{% endtabs %}

## `PersistentState`

For any class that inherits [`PersistentState`](https://nativedb.red4ext.com/c/8840884683507736), you can [add a field](/scripting-cyberpunk/redscript/language-reference/hook-annotations.md#addfield-class) with a `persistent` modifier to persist data in that class.

```swift
@addField(PlayerPuppetPS)
public persistent let m_number: Int32;

@addMethod(PlayerPuppet)
protected func DoSomething() -> Void {
  let ps: ref<PlayerPuppetPS> = this.GetPS();

  // do something with ps.m_number
}
```

## Persisting Classes

```swift
module MyMod

public class Entry {
    // both of these will be persisted
    public persistent let related: TweakDBID;
    public persistent let lasting: Int32;

    // this won't be persisted, will be initialized with the 
    // default value (0)
    public let temporary: Int32;
}

public class MySystem extends ScriptableSystem {
    private persistent let m_entries: array<ref<Entry>>;
    
    // ...
}
```
