# DataStore

Kind: Class · Namespace: Sansar.Simulation · Assembly: Sansar.Simulation.dll · Script API 47.10.0

<div class="api-meta">
<span class="api-kind api-kind-class" data-pagefind-filter="kind:Class">Class</span>
<span data-pagefind-filter="namespace:Sansar.Simulation">Namespace <a href="/scripting/api/sansar.simulation"><code>Sansar.Simulation</code></a></span>
<span class="api-badge api-since">Added in 37.1.0</span>
</div>

<span id="T:Sansar.Simulation.DataStore"></span>

The [`DataStore`](/scripting/api/sansar.simulation/datastore) class provides access to a persistent data store that can be accessed by scripts from any experience.

```csharp
public class DataStore
```

## Remarks

All [`DataStore`](/scripting/api/sansar.simulation/datastore) objects with a given [`DataStore.Id`](/scripting/api/sansar.simulation/datastore#id) share the same database.

## Members at a glance

| Kind | Members |
|---|---|
| **Properties** | [Id](#id) |
| **Methods** | [Delete](#delete) · [Restore](#restore) · [Store](#store) |

## Properties

### Id

<div class="api-card">
<span id="P:Sansar.Simulation.DataStore.Id"></span>
<div class="api-badges"><span class="api-badge api-since">Added in 37.1.0</span></div>

```csharp signature
public Guid Id { get; }
```

The Id of this [`DataStore`](/scripting/api/sansar.simulation/datastore).

<details class="api-remarks"><summary>Remarks</summary>

All [`DataStore`](/scripting/api/sansar.simulation/datastore) objects with a given [`DataStore.Id`](/scripting/api/sansar.simulation/datastore#id) share the same database.

</details>

</div>


## Methods

### Delete

<div class="api-card" id="delete-string-action">
<span id="M:Sansar.Simulation.DataStore.Delete(System.String,System.Action{Sansar.Script.OperationCompleteEvent})"></span>
<div class="api-badges"><span class="api-badge api-since">Added in 37.1.0</span></div>

```csharp signature
public void Delete(
    string key,
    Action<Sansar.Script.OperationCompleteEvent> done
)
```

Deletes the value associated with the given key

| Parameter | Type | Description |
|---|---|---|
| `key` | [string](https://learn.microsoft.com/dotnet/api/system.string) | They key the data is stored under. |
| `done` | [Action&lt;Sansar.Script.OperationCompleteEvent&gt;](https://learn.microsoft.com/dotnet/api/system.action-1) | The action to call when the delete is complete. |

<details class="api-remarks"><summary>Remarks</summary>

Unconditionally deletes the value associated with the given key. For a conditional delete, use [`DataStore.Delete`](/scripting/api/sansar.simulation/datastore#delete-string-options-action).

</details>

</div>

<div class="api-card" id="delete-string-options-action">
<span id="M:Sansar.Simulation.DataStore.Delete``1(System.String,Sansar.Simulation.DataStore.Options,System.Action{Sansar.Simulation.DataStore.Result{``0}})"></span>
<div class="api-badges"><span class="api-badge api-since">Added in 37.1.0</span></div>

```csharp signature
public void Delete<T>(
    string key,
    DataStore.Options options,
    Action<DataStore.Result<T>> done
)
```

Deletes the value associated with the given key

| Parameter | Type | Description |
|---|---|---|
| `key` | [string](https://learn.microsoft.com/dotnet/api/system.string) | They key the data is stored under. |
| `options` | [DataStore.Options](/scripting/api/sansar.simulation/datastore.options) | Controls how the operation proceeds. |
| `done` | [Action&lt;DataStore.Result&lt;T&gt;&gt;](https://learn.microsoft.com/dotnet/api/system.action-1) | The action to call when the delete is complete. |

**Type parameters:** `T`: The type of the data stored.

<details class="api-remarks"><summary>Remarks</summary>

[`Options.Version`](/scripting/api/sansar.simulation/datastore.options#version) can be specified to conditionally delete only if the version matches. If the version number does not match, [`DataStore.Result<T>`](/scripting/api/sansar.simulation/datastore.result-1) will have Success == false and the current value in the Version and Object fields.

</details>

</div>

### Restore

<div class="api-card">
<span id="M:Sansar.Simulation.DataStore.Restore``1(System.String,System.Action{Sansar.Simulation.DataStore.Result{``0}})"></span>
<div class="api-badges"><span class="api-badge api-since">Added in 37.1.0</span></div>

```csharp signature
public void Restore<T>(string key, Action<DataStore.Result<T>> done)
```

Retrieves the value associated with the given key.

| Parameter | Type | Description |
|---|---|---|
| `key` | [string](https://learn.microsoft.com/dotnet/api/system.string) | They key the data is stored under. |
| `done` | [Action&lt;DataStore.Result&lt;T&gt;&gt;](https://learn.microsoft.com/dotnet/api/system.action-1) | The action to call when the restore is complete. |

**Type parameters:** `T`: The type of the data stored.

<details class="api-remarks"><summary>Remarks</summary>

If the done action is null, this method will fail without making a call to the data store.

</details>

</div>

### Store

<div class="api-card" id="store-string-t-action">
<span id="M:Sansar.Simulation.DataStore.Store``1(System.String,``0,System.Action{Sansar.Simulation.DataStore.Result{``0}})"></span>
<div class="api-badges"><span class="api-badge api-since">Added in 37.1.0</span></div>

```csharp signature
public void Store<T>(
    string key,
    T value,
    Action<DataStore.Result<T>> done
)
```

Serializes the given object to the data store

| Parameter | Type | Description |
|---|---|---|
| `key` | [string](https://learn.microsoft.com/dotnet/api/system.string) | The key to store the data under. |
| `value` | T | The object to be converted |
| `done` | [Action&lt;DataStore.Result&lt;T&gt;&gt;](https://learn.microsoft.com/dotnet/api/system.action-1) | The action to call when the store is complete. |

<details class="api-remarks"><summary>Remarks</summary>

The specified key and value will always overwrite any existing data in the store. For a conditional store, use [`DataStore.Store`](/scripting/api/sansar.simulation/datastore#store-string-t-options-action) with [`Options.Version`](/scripting/api/sansar.simulation/datastore.options#version) set to a value from a previous operation.

</details>

</div>

<div class="api-card" id="store-string-t-options-action">
<span id="M:Sansar.Simulation.DataStore.Store``1(System.String,``0,Sansar.Simulation.DataStore.Options,System.Action{Sansar.Simulation.DataStore.Result{``0}})"></span>
<div class="api-badges"><span class="api-badge api-since">Added in 37.1.0</span></div>

```csharp signature
public void Store<T>(
    string key,
    T value,
    DataStore.Options options,
    Action<DataStore.Result<T>> done
)
```

Serializes the given object to the data store

| Parameter | Type | Description |
|---|---|---|
| `key` | [string](https://learn.microsoft.com/dotnet/api/system.string) | The key to save the data under. |
| `value` | T | The object to be converted. |
| `options` | [DataStore.Options](/scripting/api/sansar.simulation/datastore.options) | Controls how the operation proceeds. |
| `done` | [Action&lt;DataStore.Result&lt;T&gt;&gt;](https://learn.microsoft.com/dotnet/api/system.action-1) | The action to call when the conversion is complete. |

<details class="api-remarks"><summary>Remarks</summary>

[`Options.Version`](/scripting/api/sansar.simulation/datastore.options#version) can be specified to conditionally store only if the version matches. If the version number does not match, [`DataStore.Result<T>`](/scripting/api/sansar.simulation/datastore.result-1) will have Success == false and the current value in the Version and Object fields. If [`Options.RetrieveData`](/scripting/api/sansar.simulation/datastore.options#retrievedata) false and the write is successful, the response will not include the value written to the store and will simply pass back the value parameter.

</details>

</div>


## Used in examples

- [DataStoreExample.cs](https://github.com/Wookey-Technologies/sansar-script-starter/blob/66af8b516b701088d043e4615acfcf5779356604/examples/official/DataStoreExample.cs)

<p class="api-generated" data-pagefind-ignore>Generated from the Sansar 47.10.0 client ScriptApi snapshot.</p>