# ScriptBase

Kind: Class · Namespace: Sansar.Script · Assembly: Sansar.Script.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.Script">Namespace <a href="/scripting/api/sansar.script"><code>Sansar.Script</code></a></span>
<span>Inherits <a href="/scripting/api/sansar.script/reflective">Reflective</a></span>
</div>

<span id="T:Sansar.Script.ScriptBase"></span>

Base class for all scripts, has ScriptHandle accessors and coroutine utilities.

```csharp
public abstract class ScriptBase : Reflective
```

## Remarks

This abstract class manages shared information for all script types.

## Members at a glance

| Kind | Members |
|---|---|
| **Constructors** | [ScriptBase](#scriptbase) |
| **Properties** | [CurrentCoroutine](#currentcoroutine) · [Log](#log) · [MaxCoroutines](#maxcoroutines) · [Memory](#memory) · [PendingEventCount](#pendingeventcount) · [Script](#script) · [ScriptEventMessageIdCount](#scripteventmessageidcount) |
| **Methods** | [GetAllCoroutines](#getallcoroutines) · [GetCoroutineCount](#getcoroutinecount) · [Init](#init) · [Lock](#lock) · [PostScriptEvent](#postscriptevent) · [PostSimpleScriptEvent](#postsimplescriptevent) · [ReleaseLock](#releaselock) · [SetDefaultErrorMode](#setdefaulterrormode) · [SetRelaxedErrorMode](#setrelaxederrormode) · [SetStrictErrorMode](#setstricterrormode) · [StartCoroutine](#startcoroutine) · [SubscribeToScriptEvent](#subscribetoscriptevent) · [Terminate](#terminate) · [Wait](#wait) · [WaitFor](#waitfor) · [WaitForLock](#waitforlock) · [WaitForSignal](#waitforsignal) · [Yield](#yield) |

## Constructors

### ScriptBase

<div class="api-card">
<span id="M:Sansar.Script.ScriptBase.#ctor"></span>
<span id="C:Sansar.Script.ScriptBase()"></span>

```csharp signature
public ScriptBase()
```

Constructor

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

Initializes a new instance of the ScriptBase class.

</details>

</div>


## Properties

### CurrentCoroutine

<div class="api-card">
<span id="P:Sansar.Script.ScriptBase.CurrentCoroutine"></span>

```csharp signature
protected ICoroutine CurrentCoroutine { get; }
```

Gets the ICoroutine interface for the current coroutine.

**Value:** [ICoroutine](/scripting/api/sansar.script/icoroutine) — The ICoroutine interface to the currently running coroutine.

</div>

### Log

<div class="api-card">
<span id="P:Sansar.Script.ScriptBase.Log"></span>

```csharp signature
[Sansar.Script.NonReflective]
public Log Log { protected get; set; }
```

Gets the script console.

</div>

### MaxCoroutines

<div class="api-card">
<span id="P:Sansar.Script.ScriptBase.MaxCoroutines"></span>

```csharp signature
protected int MaxCoroutines { get; }
```

The maximum number of coroutines that a single script can run.

**Value:** [int](https://learn.microsoft.com/dotnet/api/system.int32) — The maximum number of coroutines that a single script can run.

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

If GetCoroutineCount() == MaxCoroutines, no more coroutines will be started.

</details>

</div>

### Memory

<div class="api-card">
<span id="P:Sansar.Script.ScriptBase.Memory"></span>

```csharp signature
[Sansar.Script.NonReflective]
public Memory Memory { protected get; set; }
```

Memory information for the pool this script is in.

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

Scripts are pooled by their owner and share memory. Use this object to get events on nearing memory limits as well as get information on memory activity and use.

</details>

</div>

### PendingEventCount

<div class="api-card">
<span id="P:Sansar.Script.ScriptBase.PendingEventCount"></span>

```csharp signature
protected int PendingEventCount { get; }
```

The number of events currently waiting to be processed.

**Value:** [int](https://learn.microsoft.com/dotnet/api/system.int32) — The number of events currently waiting to be processed.

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

If this number grows it indicates the script is not processing events as fast as it is receiving them which will bloat memory and is generally a sign of poor performance.

To remedy a situation of growing pending event count try reducing the number or rate of events the script is receiving or the amount of work done in each event.

</details>

</div>

### Script

<div class="api-card">
<span id="P:Sansar.Script.ScriptBase.Script"></span>

```csharp signature
[Sansar.Script.NonReflective]
public ScriptHandle Script { protected get; set; }
```

Script handle to this script.

**Value:** [ScriptHandle](/scripting/api/sansar.script/scripthandle) — An opaque type used by certain API methods.

</div>

### ScriptEventMessageIdCount

<div class="api-card">
<span id="P:Sansar.Script.ScriptBase.ScriptEventMessageIdCount"></span>
<div class="api-badges"><span class="api-badge api-deprecated">Deprecated</span></div>

```csharp signature
[System.Obsolete("No longer used as there is no longer a limited number of message ids. Obsoleted 2018-04.", true)]
protected static int ScriptEventMessageIdCount { get; }
```

Obsolete

**Value:** [int](https://learn.microsoft.com/dotnet/api/system.int32) — Always returns 0

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

No longer used as there is no longer a limited number of message ids. Obsoleted 2018-04.

</details>

</div>


## Methods

### GetAllCoroutines

<div class="api-card">
<span id="M:Sansar.Script.ScriptBase.GetAllCoroutines"></span>
<span id="M:Sansar.Script.ScriptBase.GetAllCoroutines()"></span>

```csharp signature
protected IReadOnlyList<ICoroutine> GetAllCoroutines()
```

A list of all coroutines for this script.

**Returns:** [IReadOnlyList&lt;ICoroutine&gt;](https://learn.microsoft.com/dotnet/api/system.collections.generic.ireadonlylist-1) — A list containing all running or waiting coroutines.

</div>

### GetCoroutineCount

<div class="api-card">
<span id="M:Sansar.Script.ScriptBase.GetCoroutineCount"></span>
<span id="M:Sansar.Script.ScriptBase.GetCoroutineCount()"></span>

```csharp signature
protected int GetCoroutineCount()
```

The total number of coroutines running on this script.

**Returns:** [int](https://learn.microsoft.com/dotnet/api/system.int32) — The total number of coroutines running on this script.

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

Includes currently active and waiting coroutines, but not killed coroutines. A new list is created every time it is called with the coroutines at the time of the call. It is possible for coroutines to finish while iterating on the list which will not remove them from the list. Any finished coroutines still in the list will have IsAlive == false.

</details>

</div>

### Init

<div class="api-card">
<span id="M:Sansar.Script.ScriptBase.Init"></span>
<span id="M:Sansar.Script.ScriptBase.Init()"></span>

```csharp signature
[Sansar.Script.NonReflective]
public abstract void Init()
```

Init() is called after all interfaces have been initialized.

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

Override Init() to set up script subscriptions and callbacks. Any members set before Init is called (such as in a script constructor) may be overwritten before Init is called.

</details>

</div>

### Lock

<div class="api-card">
<span id="M:Sansar.Script.ScriptBase.Lock(System.Object)"></span>

```csharp signature
protected IDisposable Lock(object Token)
```

Use to create a critical section with a using statement, ensuring that no other coroutines or scripts (via Reflective) may enter this code section while one coroutine or script is in it.

| Parameter | Type | Description |
|---|---|---|
| `Token` | [object](https://learn.microsoft.com/dotnet/api/system.object) | The object controlling the lock. Recommended to be a private object member. |

**Returns:** [IDisposable](https://learn.microsoft.com/dotnet/api/system.idisposable) — A ScopedLock that holds the lock and will release it in IDisposable.Dispose.

**Throws** [`ArgumentNullException`](https://learn.microsoft.com/dotnet/api/system.argumentnullexception): Thrown if Token is null.

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

Use with `using` to create a critical section. If a lock is already held on Token by a different coroutine then this coroutine will wait for the lock to be released before entering the critical section. Lock is conceptually similar to the `lock` keyword.

</details>

<details class="api-example" open><summary>Example</summary>

```csharp
private object BalanceLock = new Object();
int Balance = 100;
bool Spend(int value)
{
    using (Lock(BalanceLock))
    {
        if (Balance > value)
        {
            Balance -= value;
            return true;
        }
        return false;
    }
}
```

</details>

</div>

### PostScriptEvent

<div class="api-card" id="postscriptevent-string">
<span id="M:Sansar.Script.ScriptBase.PostScriptEvent(System.String)"></span>

```csharp signature
protected void PostScriptEvent(string message)
```

Post the event for all scripts.

| Parameter | Type | Description |
|---|---|---|
| `message` | [string](https://learn.microsoft.com/dotnet/api/system.string) | The message id used to direct the event. |

</div>

<div class="api-card" id="postscriptevent-string-reflective">
<span id="M:Sansar.Script.ScriptBase.PostScriptEvent(System.String,Sansar.Script.Reflective)"></span>

```csharp signature
protected void PostScriptEvent(string message, Reflective data)
```

Post the event for all scripts.

| Parameter | Type | Description |
|---|---|---|
| `message` | [string](https://learn.microsoft.com/dotnet/api/system.string) | The message id used to direct the event. |
| `data` | [Reflective](/scripting/api/sansar.script/reflective) | The object to be post. |

</div>

<div class="api-card" id="postscriptevent-scriptid-string-reflective">
<span id="M:Sansar.Script.ScriptBase.PostScriptEvent(Sansar.Script.ScriptId,System.String,Sansar.Script.Reflective)"></span>

```csharp signature
protected void PostScriptEvent(
    ScriptId targetScriptId,
    string message,
    Reflective data
)
```

Post the event for the target script.

| Parameter | Type | Description |
|---|---|---|
| `targetScriptId` | [ScriptId](/scripting/api/sansar.script/scriptid) | The id of the script to sent the event to. To broadcast the message to all subscripted scripts use [`ScriptBase.PostScriptEvent`](/scripting/api/sansar.script/scriptbase#postscriptevent-string-reflective). |
| `message` | [string](https://learn.microsoft.com/dotnet/api/system.string) | The message id used to direct the event. |
| `data` | [Reflective](/scripting/api/sansar.script/reflective) | The object to be post. |

</div>

### PostSimpleScriptEvent

<div class="api-card" id="postsimplescriptevent-string-object">
<span id="M:Sansar.Script.ScriptBase.PostSimpleScriptEvent(System.String,System.Object)"></span>
<div class="api-badges"><span class="api-badge api-deprecated">Deprecated</span></div>

```csharp signature
[System.Obsolete("Deprecated. Use PostScriptEvent directly instead.", true)]
protected void PostSimpleScriptEvent(string message, object data)
```

Deprecated. Use [`ScriptBase.PostScriptEvent`](/scripting/api/sansar.script/scriptbase#postscriptevent-scriptid-string-reflective)

| Parameter | Type | Description |
|---|---|---|
| `message` | [string](https://learn.microsoft.com/dotnet/api/system.string) | The message id used to direct the event. |
| `data` | [object](https://learn.microsoft.com/dotnet/api/system.object) | An object to send to scripts subscribed to message. |

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

Deprecated. Use [`ScriptBase.PostScriptEvent`](/scripting/api/sansar.script/scriptbase#postscriptevent-scriptid-string-reflective)

</details>

</div>

<div class="api-card" id="postsimplescriptevent-scriptid-string-object">
<span id="M:Sansar.Script.ScriptBase.PostSimpleScriptEvent(Sansar.Script.ScriptId,System.String,System.Object)"></span>
<div class="api-badges"><span class="api-badge api-deprecated">Deprecated</span></div>

```csharp signature
[System.Obsolete("Deprecated. Use PostScriptEvent directly instead.", true)]
protected void PostSimpleScriptEvent(
    ScriptId targetScriptId,
    string message,
    object data
)
```

Deprecated. Use [`ScriptBase.PostScriptEvent`](/scripting/api/sansar.script/scriptbase#postscriptevent-scriptid-string-reflective)

| Parameter | Type | Description |
|---|---|---|
| `targetScriptId` | [ScriptId](/scripting/api/sansar.script/scriptid) | The id of the script to sent the event to. To broadcast the message to all subscripted scripts use [`ScriptBase.PostSimpleScriptEvent`](/scripting/api/sansar.script/scriptbase#postsimplescriptevent-string-object). |
| `message` | [string](https://learn.microsoft.com/dotnet/api/system.string) | The message id used to direct the event. |
| `data` | [object](https://learn.microsoft.com/dotnet/api/system.object) | An object to send to script targetScriptId if subscribed to message. |

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

Deprecated. Use [`ScriptBase.PostScriptEvent`](/scripting/api/sansar.script/scriptbase#postscriptevent-scriptid-string-reflective)

</details>

</div>

### ReleaseLock

<div class="api-card">
<span id="M:Sansar.Script.ScriptBase.ReleaseLock(System.Object)"></span>

```csharp signature
protected void ReleaseLock(object Token)
```

Release a lock if held.

| Parameter | Type | Description |
|---|---|---|
| `Token` | [object](https://learn.microsoft.com/dotnet/api/system.object) | The object controlling the lock. Recommended to be a private object member. |

**Throws** [`ArgumentNullException`](https://learn.microsoft.com/dotnet/api/system.argumentnullexception): Thrown if Token is null.

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

It is highly recommended to use [`ScriptBase.Lock`](/scripting/api/sansar.script/scriptbase#lock) instead. This is for advanced use only and can easily create deadlocks that stop scripts from working. Can not be used to release a lock not currently held by this coroutine.

</details>

<details class="api-example" open><summary>Example</summary>

```csharp
private object BalanceLock = new Object();
int Balance = 100;
bool Spend(int value)
{
    WaitForLock(BalanceLock);
    bool success = false;
    if (Balance > value)
    {
        Balance -= value;
        success = true;
    }
    ReleaseLock(BalanceLock);
    // Note that any early exit between WaitForLock and ReleaseLock fail to release the lock, creating a deadlock the next time this method is called.
    // For this reason it is highly recommended to instead do: using (Lock(BalanceLock))
    return success;
}
```

</details>

</div>

### SetDefaultErrorMode

<div class="api-card">
<span id="M:Sansar.Script.ScriptBase.SetDefaultErrorMode"></span>
<span id="M:Sansar.Script.ScriptBase.SetDefaultErrorMode()"></span>
<div class="api-badges"><span class="api-badge api-since">Added in 37.0.0</span></div>

```csharp signature
protected void SetDefaultErrorMode()
```

Write errors to the Script Debug Console instead of throwing exceptions for some common API errors while still throwing exceptions for the more serious errors.

</div>

### SetRelaxedErrorMode

<div class="api-card">
<span id="M:Sansar.Script.ScriptBase.SetRelaxedErrorMode"></span>
<span id="M:Sansar.Script.ScriptBase.SetRelaxedErrorMode()"></span>
<div class="api-badges"><span class="api-badge api-since">Added in 37.0.0</span></div>

```csharp signature
protected void SetRelaxedErrorMode()
```

Write errors to the Script Debug Console instead of throw exceptions for almost all Sansar API errors.

</div>

### SetStrictErrorMode

<div class="api-card">
<span id="M:Sansar.Script.ScriptBase.SetStrictErrorMode"></span>
<span id="M:Sansar.Script.ScriptBase.SetStrictErrorMode()"></span>
<div class="api-badges"><span class="api-badge api-since">Added in 37.0.0</span></div>

```csharp signature
protected void SetStrictErrorMode()
```

Throw exceptions for almost all Sansar API errors.

</div>

### StartCoroutine

<div class="api-card" id="startcoroutine-action-action">
<span id="M:Sansar.Script.ScriptBase.StartCoroutine(System.Action,System.Action{Sansar.Script.OperationCompleteEvent})"></span>

```csharp signature
protected ICoroutine StartCoroutine(
    Action coroutine,
    Action<OperationCompleteEvent> handler
)
```

Starts a coroutine on the current script.

| Parameter | Type | Description |
|---|---|---|
| `coroutine` | [Action](https://learn.microsoft.com/dotnet/api/system.action) | The coroutine to run. |
| `handler` | [Action&lt;OperationCompleteEvent&gt;](https://learn.microsoft.com/dotnet/api/system.action-1) | A callback to know when the coroutine has finished. |

**Returns:** [ICoroutine](/scripting/api/sansar.script/icoroutine) — An ICoroutine interface to stop the coroutine or see if it has finished.

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

A coroutine can call `ScriptBase.WaitFor` to block while waiting for events to return.

The following three scripts acccomplish the same goal using event subscriptions and coroutines. Each script will listen for new agents to join the scene and keep track of the agents name and the time it joined. When the agent leaves, its name and the duration of its visit will be reported.



Event handlers are useful when there is a well contained bit of code which can get most or all of its state from the [`EventData`](/scripting/api/sansar.script/eventdata) it receives. In this example, a separate data structure is needed to keep track of the name and join time for each agent since there could be multiple agents in the scene.



```csharp title="CoroutineExample.cs"
/* This content is licensed under the terms of the Creative Commons Attribution 4.0 International License.
 * When using this content, you must:
 * •    Acknowledge that the content is from the Sansar Knowledge Base.
 * •    Include our copyright notice: "2022 Sansar, Inc."
 * •    Indicate that the content is licensed under the Creative Commons Attribution-Share Alike 4.0 International License.
 * •    Include the URL for, or link to, the license summary at https://creativecommons.org/licenses/by-sa/4.0/deed.hi (and, if possible, to the complete license terms at https://creativecommons.org/licenses/by-sa/4.0/legalcode.
 * For example:
 * "This work uses content from the Sansar Knowledge Base. © 2022 Sansar, Inc." Licensed under the Creative Commons Attribution 4.0 International License (license summary available at https://creativecommons.org/licenses/by/4.0/ and complete license terms available at https://creativecommons.org/licenses/by/4.0/legalcode)."
 */

/* Use a coroutine to wait for event callbacks to track visitors to a scene.
 * For every visitor start a new coroutine to track that visitor's time.
 */
using System;
using Sansar.Script;
using Sansar.Simulation;
using System.Diagnostics;


// This example shows how to use coroutines to track agents entering and leaving the scene
public class CoroutineExample : SceneObjectScript
{
    public override void Init()
    {
        StartCoroutine(TrackNewUsers);
    }

    // Wait for new users and start a new coroutine to time them.
    private void TrackNewUsers()
    {
        // This coroutine will run for the lifetime of the script.
        while (true)
        {
            // Block until an add user event happens
            UserData data = (UserData)WaitFor(ScenePrivate.User.Subscribe, User.AddUser, SessionId.Invalid);

            // Start a new coroutine to track the new user
            // Each coroutine will run cooperatively with the others, this script, and other scripts in the system
            StartCoroutine(TrackUser, data.User);
        }
    }

    // There will be one instance of this coroutine per active user in the scene
    private void TrackUser(SessionId userId)
    {
        // Track joined time
        long joined = Stopwatch.GetTimestamp();

        // Lookup the name of the agent. This is looked up now since the agent cannot be retrieved after they
        // leave the scene.
        string name = ScenePrivate.FindAgent(userId).AgentInfo.Name;

        // Block until the agent leaves the scene
        WaitFor(ScenePrivate.User.Subscribe, User.RemoveUser, userId);

        // Calculate elapsed time and report.
        TimeSpan elapsed = TimeSpan.FromTicks(Stopwatch.GetTimestamp()-joined);
        ScenePrivate.Chat.MessageAllUsers(string.Format("{0} was present for {0} seconds", name, elapsed.TotalSeconds));
    }
}
```

<p class="api-example-source">Source: <code>Examples/CoroutineExample.cs</code> in the Sansar 47.10.0 client ScriptApi snapshot.</p>



While the amount of code is approximately the same for each, the coroutine version does not require access to any member data, simplifying the tracking by moving all the required information to stack variables.



```csharp title="CoroutineEventExample.cs"
/* This content is licensed under the terms of the Creative Commons Attribution 4.0 International License.
 * When using this content, you must:
 * •    Acknowledge that the content is from the Sansar Knowledge Base.
 * •    Include our copyright notice: "2022 Sansar, Inc."
 * •    Indicate that the content is licensed under the Creative Commons Attribution-Share Alike 4.0 International License.
 * •    Include the URL for, or link to, the license summary at https://creativecommons.org/licenses/by-sa/4.0/deed.hi (and, if possible, to the complete license terms at https://creativecommons.org/licenses/by-sa/4.0/legalcode.
 * For example:
 * "This work uses content from the Sansar Knowledge Base. © 2022 Sansar, Inc." Licensed under the Creative Commons Attribution 4.0 International License (license summary available at https://creativecommons.org/licenses/by/4.0/ and complete license terms available at https://creativecommons.org/licenses/by/4.0/legalcode)."
 */

/* Use a coroutine as an event callback to easily track visitors to a scene.
 * Every visitor triggers an AddUser event which will start a coroutine for that visitor.
 */
using System;
using Sansar.Script;
using Sansar.Simulation;
using System.Diagnostics;


// This example shows how to use coroutines and events to track agents entering and leaving the scene
public class CoroutineEventExample : SceneObjectScript
{
    public override void Init()
    {
        // Subscribe to Add User events
        // Events can be handled by anonymous methods
        ScenePrivate.User.Subscribe(User.AddUser, (UserData data) => StartCoroutine(TrackUser, data.User), true);
    }


    // There will be one instance of this coroutine per active user in the scene
    private void TrackUser(SessionId userId)
    {
        // Track joined time
        long joined = Stopwatch.GetTimestamp();

        // Lookup the name of the agent. This is looked up now since the agent cannot be retrieved after they
        // leave the scene.
        string name = ScenePrivate.FindAgent(userId).AgentInfo.Name;

        // Block until the agent leaves the scene
        WaitFor(ScenePrivate.User.Subscribe, User.RemoveUser, userId);

        // Calculate elapsed time and report.
        TimeSpan elapsed = TimeSpan.FromTicks(Stopwatch.GetTimestamp()-joined);
        ScenePrivate.Chat.MessageAllUsers(string.Format("{0} was present for {0} seconds", name, elapsed.TotalSeconds));
    }
}
```

<p class="api-example-source">Source: <code>Examples/CoroutineEventExample.cs</code> in the Sansar 47.10.0 client ScriptApi snapshot.</p>



Coroutines can be mixed with event handlers. This example uses an event handler for the new user events to start a coroutine to watch for the remove user events.

</details>

<details class="api-example" open><summary>Example</summary>

```csharp title="EventExample.cs"
/* This content is licensed under the terms of the Creative Commons Attribution 4.0 International License.
 * When using this content, you must:
 * •    Acknowledge that the content is from the Sansar Knowledge Base.
 * •    Include our copyright notice: "2022 Sansar, Inc."
 * •    Indicate that the content is licensed under the Creative Commons Attribution-Share Alike 4.0 International License.
 * •    Include the URL for, or link to, the license summary at https://creativecommons.org/licenses/by-sa/4.0/deed.hi (and, if possible, to the complete license terms at https://creativecommons.org/licenses/by-sa/4.0/legalcode.
 * For example:
 * "This work uses content from the Sansar Knowledge Base. © 2022 Sansar, Inc." Licensed under the Creative Commons Attribution 4.0 International License (license summary available at https://creativecommons.org/licenses/by/4.0/ and complete license terms available at https://creativecommons.org/licenses/by/4.0/legalcode)."
 */

using System;
using Sansar.Script;
using Sansar.Simulation;
using System.Collections.Generic;

public class StarWarsVoiceover : SceneObjectScript
{
    // PUBLIC MEMBERS ------

    public SoundResource Sound;

    [Range(0.0, 60.0)]
    [DefaultValue(48.0)]
    public double Loudness_dB;


    // PRIVATE MEMBERS ------

    private float ZeroDBOffset = 48.0f;
    private RigidBodyComponent RigidBody;
    private float Relative_Loudness;
    static private Dictionary<SessionId, PlayHandle> playHandles = new Dictionary<SessionId, PlayHandle>();


    private void PlayOnAgent(AgentPrivate agent)
    {
        try
        {
            SessionId agentId = agent.AgentInfo.SessionId;
            Stop(agentId);

            PlaySettings playSettings = PlaySettings.PlayOnce;
            playSettings.Loudness = Relative_Loudness;
            if (agent.IsValid)
            {
                PlayHandle playHandle = agent.PlaySound(Sound, playSettings);
                if (playHandle != null)
                {
                    playHandles.Add(agentId, playHandle);
                    playHandle.OnFinished(() => Stop(agentId));
                }
            }
        }
        catch (Exception e)
        {
            Log.Write(LogLevel.Warning, "Voiceover", $"Exception {e.GetType().Name} in PlayOnAgent");
        }
    }

    private void Stop(SessionId id)
    {
        try
        {
            PlayHandle playing;
            if (playHandles.TryGetValue(id, out playing))
            {
                playing.Stop();
                playHandles.Remove(id);
            }
        }
        catch (Exception e)
        {
            Log.Write(LogLevel.Warning, "Voiceover", $"Exception {e.GetType().Name} while stopping a sound");
        }
    }

    private void OnCollide(CollisionData data)
    {
        if (data.Phase == Sansar.Simulation.CollisionEventPhase.TriggerEnter)
        {
            try
            {
                AgentPrivate agent = ScenePrivate.FindAgent(data.HitComponentId.ObjectId);
                if (agent != null && agent.IsValid) PlayOnAgent(agent);
            }
            catch (Exception e)
            {
                // User left with _really_ bad timing, don't worry about it.
                Log.Write(LogLevel.Warning, "Voiceover", $"Exception {e.GetType().Name} in OnCollide");
            }
        }
    }

    private void UserLeft(UserData data)
    {
        Stop(data.User);
    }

    public override void Init()
    {
        ScenePrivate.User.Subscribe(User.RemoveUser, UserLeft);
        // Collision events are related to the RigidBody component so we must find it.
        // See if this object has a RigidBodyComponent and grab the first one.
        if (ObjectPrivate.TryGetFirstComponent(out RigidBody))
        {
            Relative_Loudness = (float)Loudness_dB - ZeroDBOffset;

            if (RigidBody.IsTriggerVolume())
            {
                // Subscribe to TriggerVolume collisions on our rigid body: our callback (OnCollide) will get called
                // whenever a character or character vr hand collides with our trigger volume
                RigidBody.Subscribe(CollisionEventType.Trigger, OnCollide);
            }
        }
        else
        {
            // This will write to the region's log files. Only helpful to Linden developers.
            Log.Write("Couldn't find rigid body!");
        }
    }
}
```

<p class="api-example-source">Source: <code>Examples/EventExample.cs</code> in the Sansar 47.10.0 client ScriptApi snapshot.</p>

</details>

</div>

<div class="api-card" id="startcoroutine-action-t-action">
<span id="M:Sansar.Script.ScriptBase.StartCoroutine``1(System.Action{``0},``0,System.Action{Sansar.Script.OperationCompleteEvent})"></span>

```csharp signature
protected ICoroutine StartCoroutine<T>(
    Action<T> coroutine,
    T arg1,
    Action<OperationCompleteEvent> handler
)
```

Starts a coroutine on the current script.

| Parameter | Type | Description |
|---|---|---|
| `coroutine` | [Action&lt;T&gt;](https://learn.microsoft.com/dotnet/api/system.action-1) | The coroutine to run. |
| `arg1` | T | First parameter to pass to the coroutine when it is run. |
| `handler` | [Action&lt;OperationCompleteEvent&gt;](https://learn.microsoft.com/dotnet/api/system.action-1) | A callback to know when the coroutine has finished. Will report Success==false only if an exception was thrown by the coroutine. Message will be the name of the coroutine. |

**Type parameters:** `T`: Type of the first parameter to pass to the coroutine when it is run. This can usually be derived from the Action.

**Returns:** [ICoroutine](/scripting/api/sansar.script/icoroutine) — An ICoroutine interface to stop the coroutine or see if it has finished.

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

A coroutine can call `ScriptBase.WaitFor` to block while waiting for events to return. See [`ScriptBase.StartCoroutine`](/scripting/api/sansar.script/scriptbase#startcoroutine-action-action) for an extended example.

</details>

</div>

<div class="api-card" id="startcoroutine-action-t-t-action">
<span id="M:Sansar.Script.ScriptBase.StartCoroutine``2(System.Action{``0,``1},``0,``1,System.Action{Sansar.Script.OperationCompleteEvent})"></span>

```csharp signature
protected ICoroutine StartCoroutine<T, T1>(
    Action<T, T1> coroutine,
    T arg1,
    T1 arg2,
    Action<OperationCompleteEvent> handler
)
```

Starts a coroutine on the current script.

| Parameter | Type | Description |
|---|---|---|
| `coroutine` | [Action&lt;T, T1&gt;](https://learn.microsoft.com/dotnet/api/system.action-2) | The coroutine to run. |
| `arg1` | T | First parameter to pass to the coroutine when it is run. |
| `arg2` | T1 | Second parameter to pass to the coroutine when it is run. |
| `handler` | [Action&lt;OperationCompleteEvent&gt;](https://learn.microsoft.com/dotnet/api/system.action-1) | A callback to know when the coroutine has finished. Will report Success==false only if an exception was thrown by the coroutine. Message will be the name of the coroutine. |

**Type parameters:** `T`: Type of the first parameter to pass to the coroutine when it is run. This can usually be derived from the Action.; `T1`: Type of the second parameter to pass to the coroutine when it is run. This can usually be derived from the Action.

**Returns:** [ICoroutine](/scripting/api/sansar.script/icoroutine) — An ICoroutine interface to stop the coroutine or see if it has finished.

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

A coroutine can call `ScriptBase.WaitFor` to block while waiting for events to return. See [`ScriptBase.StartCoroutine`](/scripting/api/sansar.script/scriptbase#startcoroutine-action-action) for an extended example.

</details>

</div>

<div class="api-card" id="startcoroutine-action-t-t-t-action">
<span id="M:Sansar.Script.ScriptBase.StartCoroutine``3(System.Action{``0,``1,``2},``0,``1,``2,System.Action{Sansar.Script.OperationCompleteEvent})"></span>

```csharp signature
protected ICoroutine StartCoroutine<T, T1, T2>(
    Action<T, T1, T2> coroutine,
    T arg1,
    T1 arg2,
    T2 arg3,
    Action<OperationCompleteEvent> handler
)
```

Starts a coroutine on the current script.

| Parameter | Type | Description |
|---|---|---|
| `coroutine` | [Action&lt;T, T1, T2&gt;](https://learn.microsoft.com/dotnet/api/system.action-3) | The coroutine to run. |
| `arg1` | T | First parameter to pass to the coroutine when it is run. |
| `arg2` | T1 | Second parameter to pass to the coroutine when it is run. |
| `arg3` | T2 | Third parameter to pass to the coroutine when it is run. |
| `handler` | [Action&lt;OperationCompleteEvent&gt;](https://learn.microsoft.com/dotnet/api/system.action-1) | A callback to know when the coroutine has finished. Will report Success==false only if an exception was thrown by the coroutine. Message will be the name of the coroutine. |

**Type parameters:** `T`: Type of the first parameter to pass to the coroutine when it is run. This can usually be derived from the Action.; `T1`: Type of the second parameter to pass to the coroutine when it is run. This can usually be derived from the Action.; `T2`: Type of the third parameter to pass to the coroutine when it is run. This can usually be derived from the Action.

**Returns:** [ICoroutine](/scripting/api/sansar.script/icoroutine) — An ICoroutine interface to stop the coroutine or see if it has finished.

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

A coroutine can call `ScriptBase.WaitFor` to block while waiting for events to return. See [`ScriptBase.StartCoroutine`](/scripting/api/sansar.script/scriptbase#startcoroutine-action-action) for an extended example.

</details>

</div>

<div class="api-card" id="startcoroutine-action-t-t-t-t-action">
<span id="M:Sansar.Script.ScriptBase.StartCoroutine``4(System.Action{``0,``1,``2,``3},``0,``1,``2,``3,System.Action{Sansar.Script.OperationCompleteEvent})"></span>

```csharp signature
protected ICoroutine StartCoroutine<T, T1, T2, T3>(
    Action<T, T1, T2, T3> coroutine,
    T arg1,
    T1 arg2,
    T2 arg3,
    T3 arg4,
    Action<OperationCompleteEvent> handler
)
```

Starts a coroutine on the current script.

| Parameter | Type | Description |
|---|---|---|
| `coroutine` | [Action&lt;T, T1, T2, T3&gt;](https://learn.microsoft.com/dotnet/api/system.action-4) | The coroutine to run. |
| `arg1` | T | First parameter to pass to the coroutine when it is run. |
| `arg2` | T1 | Second parameter to pass to the coroutine when it is run. |
| `arg3` | T2 | Third parameter to pass to the coroutine when it is run. |
| `arg4` | T3 | Fourth parameter to pass to the coroutine when it is run. |
| `handler` | [Action&lt;OperationCompleteEvent&gt;](https://learn.microsoft.com/dotnet/api/system.action-1) | A callback to know when the coroutine has finished. Will report Success==false only if an exception was thrown by the coroutine. Message will be the name of the coroutine. |

**Type parameters:** `T`: Type of the first parameter to pass to the coroutine when it is run. This can usually be derived from the Action.; `T1`: Type of the second parameter to pass to the coroutine when it is run. This can usually be derived from the Action.; `T2`: Type of the third parameter to pass to the coroutine when it is run. This can usually be derived from the Action.; `T3`: Type of the fourth parameter to pass to the coroutine when it is run. This can usually be derived from the Action.

**Returns:** [ICoroutine](/scripting/api/sansar.script/icoroutine) — An ICoroutine interface to stop the coroutine or see if it has finished.

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

A coroutine can call `ScriptBase.WaitFor` to block while waiting for events to return. See [`ScriptBase.StartCoroutine`](/scripting/api/sansar.script/scriptbase#startcoroutine-action-action) for an extended example.

</details>

</div>

### SubscribeToScriptEvent

<div class="api-card" id="subscribetoscriptevent-string-action-boolean">
<span id="M:Sansar.Script.ScriptBase.SubscribeToScriptEvent(System.String,System.Action{Sansar.Script.ScriptEventData},System.Boolean)"></span>

```csharp signature
protected IEventSubscription SubscribeToScriptEvent(
    string message,
    Action<ScriptEventData> callback,
    bool persistent
)
```

Subscribes to events sent by other scripts

| Parameter | Type | Description |
|---|---|---|
| `message` | [string](https://learn.microsoft.com/dotnet/api/system.string) | The event message to listen for. |
| `callback` | [Action&lt;ScriptEventData&gt;](https://learn.microsoft.com/dotnet/api/system.action-1) | Handler to call when the event is generated |
| `persistent` | [bool](https://learn.microsoft.com/dotnet/api/system.boolean) | Set to false if a one time event is desired. |

**Returns:** [IEventSubscription](/scripting/api/sansar.script/ieventsubscription) — An IEventSubscription that can be used to Unsubscribe from these script events.

</div>

<div class="api-card" id="subscribetoscriptevent-scriptid-string-action-boolean">
<span id="M:Sansar.Script.ScriptBase.SubscribeToScriptEvent(Sansar.Script.ScriptId,System.String,System.Action{Sansar.Script.ScriptEventData},System.Boolean)"></span>

```csharp signature
protected IEventSubscription SubscribeToScriptEvent(
    ScriptId sourceScriptId,
    string message,
    Action<ScriptEventData> callback,
    bool persistent
)
```

Subscribes to events sent only by a specific script.

| Parameter | Type | Description |
|---|---|---|
| `sourceScriptId` | [ScriptId](/scripting/api/sansar.script/scriptid) | Ignore events posted by scripts not matching this id. |
| `message` | [string](https://learn.microsoft.com/dotnet/api/system.string) | The event message to listen for. |
| `callback` | [Action&lt;ScriptEventData&gt;](https://learn.microsoft.com/dotnet/api/system.action-1) | Handler to call when the event is generated. |
| `persistent` | [bool](https://learn.microsoft.com/dotnet/api/system.boolean) | Set to false if a one time event is desired. |

**Returns:** [IEventSubscription](/scripting/api/sansar.script/ieventsubscription) — An IEventSubscription that can be used to Unsubscribe from these script events.

</div>

### Terminate

<div class="api-card">
<span id="M:Sansar.Script.ScriptBase.Terminate(System.String)"></span>

```csharp signature
protected void Terminate(string message)
```

Terminates this script immediately.

| Parameter | Type | Description |
|---|---|---|
| `message` | [string](https://learn.microsoft.com/dotnet/api/system.string) | The message to write to the log. |

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

This method does not return.

</details>

</div>

### Wait

<div class="api-card" id="wait-double">
<span id="M:Sansar.Script.ScriptBase.Wait(System.Double)"></span>
<div class="api-badges"><span class="api-badge api-since">Added in 32.0.0</span></div>

```csharp signature
protected void Wait(double duration)
```

Delays execution of the current coroutine for the specified time.

| Parameter | Type | Description |
|---|---|---|
| `duration` | [double](https://learn.microsoft.com/dotnet/api/system.double) | The length of time to wait before continuing in seconds. |

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

Throws [`CoroutineException`](/scripting/api/sansar.script/coroutineexception) if called from a script&#39;s constructor.

</details>

</div>

<div class="api-card" id="wait-timespan">
<span id="M:Sansar.Script.ScriptBase.Wait(System.TimeSpan)"></span>

```csharp signature
protected void Wait(TimeSpan duration)
```

Delays execution of the current coroutine for the specified time.

| Parameter | Type | Description |
|---|---|---|
| `duration` | [TimeSpan](https://learn.microsoft.com/dotnet/api/system.timespan) | The length of time to wait before continuing. |

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

Throws [`CoroutineException`](/scripting/api/sansar.script/coroutineexception) if called from a script&#39;s constructor.

</details>

</div>

### WaitFor

<div class="api-card" id="waitfor-icoroutine">
<span id="M:Sansar.Script.ScriptBase.WaitFor(Sansar.Script.ICoroutine)"></span>

```csharp signature
protected void WaitFor(ICoroutine otherCoroutine)
```

Block the current coroutine until otherCoroutine finishes.

| Parameter | Type | Description |
|---|---|---|
| `otherCoroutine` | [ICoroutine](/scripting/api/sansar.script/icoroutine) | The coroutine to wait for. |

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

Waiting for the current coroutine, a null coroutine or a coroutine that is not alive will immediately return.

</details>

</div>

<div class="api-card" id="waitfor-action">
<span id="M:Sansar.Script.ScriptBase.WaitFor(System.Action{System.Action{Sansar.Script.OperationCompleteEvent}})"></span>

```csharp signature
protected OperationCompleteEvent WaitFor(
    Action<Action<OperationCompleteEvent>> func
)
```

Use to pause the script until an event happens.

| Parameter | Type | Description |
|---|---|---|
| `func` | [Action&lt;Action&lt;OperationCompleteEvent&gt;&gt;](https://learn.microsoft.com/dotnet/api/system.action-1) | The event-generating API to wait for. |

**Returns:** [OperationCompleteEvent](/scripting/api/sansar.script/operationcompleteevent) — An EventData that can be case to type corresponding to the event type of func.

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

Throws [`CoroutineException`](/scripting/api/sansar.script/coroutineexception) if called from a script&#39;s constructor.

</details>

</div>

<div class="api-card" id="waitfor-func">
<span id="M:Sansar.Script.ScriptBase.WaitFor(System.Func{System.Action{Sansar.Script.EventData},System.Action{Sansar.Script.CancelData},System.Boolean,Sansar.Script.IEventSubscription})"></span>

```csharp signature
protected EventData WaitFor(
    Func<Action<EventData>, Action<Sansar.Script.CancelData>,System.Boolean,Sansar.Script.IEventSubscription> func
)
```

Use to pause the script until an event happens.

| Parameter | Type | Description |
|---|---|---|
| `func` | [Func&lt;Action&lt;EventData&gt;, Action&lt;Sansar.Script.CancelData&gt;,System.Boolean,Sansar.Script.IEventSubscription&gt;](https://learn.microsoft.com/dotnet/api/system.func-4) | The event-generating API to wait for. |

**Returns:** [EventData](/scripting/api/sansar.script/eventdata) — An EventData that can be case to type corresponding to the event type of func.

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

Throws [`CoroutineException`](/scripting/api/sansar.script/coroutineexception) if called from a script&#39;s constructor.

</details>

</div>

<div class="api-card" id="waitfor-func-2">
<span id="M:Sansar.Script.ScriptBase.WaitFor(System.Func{System.Action{Sansar.Script.EventData},System.Boolean,Sansar.Script.IEventSubscription})"></span>

```csharp signature
protected EventData WaitFor(
    Func<Action<Sansar.Script.EventData>,System.Boolean,Sansar.Script.IEventSubscription> func
)
```

Use to pause the script until an event happens.

| Parameter | Type | Description |
|---|---|---|
| `func` | [Func&lt;Action&lt;Sansar.Script.EventData&gt;,System.Boolean,Sansar.Script.IEventSubscription&gt;](https://learn.microsoft.com/dotnet/api/system.func-3) | The event-generating API to wait for. |

**Returns:** [EventData](/scripting/api/sansar.script/eventdata) — An EventData that can be case to type corresponding to the event type of func.

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

Throws [`CoroutineException`](/scripting/api/sansar.script/coroutineexception) if called from a script&#39;s constructor.

</details>

</div>

<div class="api-card" id="waitfor-func-action">
<span id="M:Sansar.Script.ScriptBase.WaitFor(System.Func{System.Action{Sansar.Script.EventData},System.Boolean,Sansar.Script.IEventSubscription},System.Action)"></span>

```csharp signature
protected EventData WaitFor(
    Func<Action<Sansar.Script.EventData>,System.Boolean,Sansar.Script.IEventSubscription> func,
    Action run
)
```

Use to pause the script until an event happens.

| Parameter | Type | Description |
|---|---|---|
| `func` | [Func&lt;Action&lt;Sansar.Script.EventData&gt;,System.Boolean,Sansar.Script.IEventSubscription&gt;](https://learn.microsoft.com/dotnet/api/system.func-3) | The event-generating API to wait for. |
| `run` | [Action](https://learn.microsoft.com/dotnet/api/system.action) | An Action to run after subscribing the the event, but before waiting for the event to occurr. |

**Returns:** [EventData](/scripting/api/sansar.script/eventdata) — An EventData that can be case to type corresponding to the event type of func.

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

Throws [`CoroutineException`](/scripting/api/sansar.script/coroutineexception) if called from a script&#39;s constructor.

</details>

</div>

<div class="api-card" id="waitfor-func-t">
<span id="M:Sansar.Script.ScriptBase.WaitFor``1(System.Func{``0,System.Action{Sansar.Script.EventData},System.Action{Sansar.Script.CancelData},System.Boolean,Sansar.Script.IEventSubscription},``0)"></span>

```csharp signature
protected EventData WaitFor<ARG1>(
    Func<ARG1, Action<EventData>, Action<Sansar.Script.CancelData>,System.Boolean,Sansar.Script.IEventSubscription> func,
    ARG1 arg1
)
```

Use to pause the script until an event happens.

| Parameter | Type | Description |
|---|---|---|
| `func` | [Func&lt;ARG1, Action&lt;EventData&gt;, Action&lt;Sansar.Script.CancelData&gt;,System.Boolean,Sansar.Script.IEventSubscription&gt;](https://learn.microsoft.com/dotnet/api/system.func-5) | The event-generating API to wait for. |
| `arg1` | ARG1 | An argument to func. |

**Type parameters:** `ARG1`: The type of the argument to func.

**Returns:** [EventData](/scripting/api/sansar.script/eventdata) — An EventData that can be case to type corresponding to the event type of func.

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

Throws [`CoroutineException`](/scripting/api/sansar.script/coroutineexception) if called from a script&#39;s constructor.

</details>

</div>

<div class="api-card" id="waitfor-func-t-2">
<span id="M:Sansar.Script.ScriptBase.WaitFor``1(System.Func{``0,System.Action{Sansar.Script.EventData},System.Boolean,Sansar.Script.IEventSubscription},``0)"></span>

```csharp signature
protected EventData WaitFor<ARG1>(
    Func<ARG1, Action<Sansar.Script.EventData>,System.Boolean,Sansar.Script.IEventSubscription> func,
    ARG1 arg1
)
```

Use to pause the script until an event happens.

| Parameter | Type | Description |
|---|---|---|
| `func` | [Func&lt;ARG1, Action&lt;Sansar.Script.EventData&gt;,System.Boolean,Sansar.Script.IEventSubscription&gt;](https://learn.microsoft.com/dotnet/api/system.func-4) | The event-generating API to wait for. |
| `arg1` | ARG1 | An argument to func. |

**Type parameters:** `ARG1`: The type of the argument to func.

**Returns:** [EventData](/scripting/api/sansar.script/eventdata) — An EventData that can be case to type corresponding to the event type of func.

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

Throws [`CoroutineException`](/scripting/api/sansar.script/coroutineexception) if called from a script&#39;s constructor.

</details>

</div>

<div class="api-card" id="waitfor-func-t-action">
<span id="M:Sansar.Script.ScriptBase.WaitFor``1(System.Func{``0,System.Action{Sansar.Script.EventData},System.Boolean,Sansar.Script.IEventSubscription},``0,System.Action)"></span>

```csharp signature
protected EventData WaitFor<ARG1>(
    Func<ARG1, Action<Sansar.Script.EventData>,System.Boolean,Sansar.Script.IEventSubscription> func,
    ARG1 arg1,
    Action run
)
```

Use to pause the script until an event happens.

| Parameter | Type | Description |
|---|---|---|
| `func` | [Func&lt;ARG1, Action&lt;Sansar.Script.EventData&gt;,System.Boolean,Sansar.Script.IEventSubscription&gt;](https://learn.microsoft.com/dotnet/api/system.func-4) | The event-generating API to wait for. |
| `arg1` | ARG1 | An argument to func. |
| `run` | [Action](https://learn.microsoft.com/dotnet/api/system.action) | An Action to run after subscribing the the event, but before waiting for the event to occurr. |

**Type parameters:** `ARG1`: The type of the argument to func.

**Returns:** [EventData](/scripting/api/sansar.script/eventdata) — An EventData that can be case to type corresponding to the event type of func.

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

Throws [`CoroutineException`](/scripting/api/sansar.script/coroutineexception) if called from a script&#39;s constructor.

</details>

</div>

<div class="api-card" id="waitfor-action-t">
<span id="M:Sansar.Script.ScriptBase.WaitFor``1(System.Action{``0,System.Action{Sansar.Script.OperationCompleteEvent}},``0)"></span>

```csharp signature
protected OperationCompleteEvent WaitFor<T1>(
    Action<T1, Action<OperationCompleteEvent>> func,
    T1 t1
)
```

Use to pause the script until an event happens.

| Parameter | Type | Description |
|---|---|---|
| `func` | [Action&lt;T1, Action&lt;OperationCompleteEvent&gt;&gt;](https://learn.microsoft.com/dotnet/api/system.action-2) | The event-generating API to wait for. |
| `t1` | T1 | An argument to func. |

**Type parameters:** `T1`: The type of the argument to func.

**Returns:** [OperationCompleteEvent](/scripting/api/sansar.script/operationcompleteevent) — An EventData that can be case to type corresponding to the event type of func.

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

Throws [`CoroutineException`](/scripting/api/sansar.script/coroutineexception) if called from a script&#39;s constructor.

</details>

</div>

<div class="api-card" id="waitfor-func-t-t">
<span id="M:Sansar.Script.ScriptBase.WaitFor``2(System.Func{``0,``1,System.Action{Sansar.Script.EventData},System.Action{Sansar.Script.CancelData},System.Boolean,Sansar.Script.IEventSubscription},``0,``1)"></span>

```csharp signature
protected EventData WaitFor<ARG1, ARG2>(
    Func<ARG1, ARG2, Action<EventData>, Action<Sansar.Script.CancelData>,System.Boolean,Sansar.Script.IEventSubscription> func,
    ARG1 arg1,
    ARG2 arg2
)
```

Use to pause the script until an event happens.

| Parameter | Type | Description |
|---|---|---|
| `func` | [Func&lt;ARG1, ARG2, Action&lt;EventData&gt;, Action&lt;Sansar.Script.CancelData&gt;,System.Boolean,Sansar.Script.IEventSubscription&gt;](https://learn.microsoft.com/dotnet/api/system.func-6) | The event-generating API to wait for. |
| `arg1` | ARG1 | An argument to func. |
| `arg2` | ARG2 | An argument to func. |

**Type parameters:** `ARG1`: The type of the argument to func.; `ARG2`: The type of the argument to func.

**Returns:** [EventData](/scripting/api/sansar.script/eventdata) — An EventData that can be case to type corresponding to the event type of func.

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

Throws [`CoroutineException`](/scripting/api/sansar.script/coroutineexception) if called from a script&#39;s constructor.

</details>

</div>

<div class="api-card" id="waitfor-func-t-t-2">
<span id="M:Sansar.Script.ScriptBase.WaitFor``2(System.Func{``0,``1,System.Action{Sansar.Script.EventData},System.Boolean,Sansar.Script.IEventSubscription},``0,``1)"></span>

```csharp signature
protected EventData WaitFor<ARG1, ARG2>(
    Func<ARG1, ARG2, Action<Sansar.Script.EventData>,System.Boolean,Sansar.Script.IEventSubscription> func,
    ARG1 arg1,
    ARG2 arg2
)
```

Use to pause the script until an event happens.

| Parameter | Type | Description |
|---|---|---|
| `func` | [Func&lt;ARG1, ARG2, Action&lt;Sansar.Script.EventData&gt;,System.Boolean,Sansar.Script.IEventSubscription&gt;](https://learn.microsoft.com/dotnet/api/system.func-5) | The event-generating API to wait for. |
| `arg1` | ARG1 | An argument to func. |
| `arg2` | ARG2 | An argument to func. |

**Type parameters:** `ARG1`: The type of the argument to func.; `ARG2`: The type of the argument to func.

**Returns:** [EventData](/scripting/api/sansar.script/eventdata) — An EventData that can be case to type corresponding to the event type of func.

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

Throws [`CoroutineException`](/scripting/api/sansar.script/coroutineexception) if called from a script&#39;s constructor.

</details>

</div>

<div class="api-card" id="waitfor-func-t-t-action">
<span id="M:Sansar.Script.ScriptBase.WaitFor``2(System.Func{``0,``1,System.Action{Sansar.Script.EventData},System.Boolean,Sansar.Script.IEventSubscription},``0,``1,System.Action)"></span>

```csharp signature
protected EventData WaitFor<ARG1, ARG2>(
    Func<ARG1, ARG2, Action<Sansar.Script.EventData>,System.Boolean,Sansar.Script.IEventSubscription> func,
    ARG1 arg1,
    ARG2 arg2,
    Action run
)
```

Use to pause the script until an event happens.

| Parameter | Type | Description |
|---|---|---|
| `func` | [Func&lt;ARG1, ARG2, Action&lt;Sansar.Script.EventData&gt;,System.Boolean,Sansar.Script.IEventSubscription&gt;](https://learn.microsoft.com/dotnet/api/system.func-5) | The event-generating API to wait for. |
| `arg1` | ARG1 | An argument to func. |
| `arg2` | ARG2 | An argument to func. |
| `run` | [Action](https://learn.microsoft.com/dotnet/api/system.action) | An Action to run after subscribing the the event, but before waiting for the event to occurr. |

**Type parameters:** `ARG1`: The type of the argument to func.; `ARG2`: The type of the argument to func.

**Returns:** [EventData](/scripting/api/sansar.script/eventdata) — An EventData that can be case to type corresponding to the event type of func.

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

Throws [`CoroutineException`](/scripting/api/sansar.script/coroutineexception) if called from a script&#39;s constructor.

</details>

</div>

<div class="api-card" id="waitfor-action-t-t">
<span id="M:Sansar.Script.ScriptBase.WaitFor``2(System.Action{``0,``1,System.Action{Sansar.Script.OperationCompleteEvent}},``0,``1)"></span>

```csharp signature
protected OperationCompleteEvent WaitFor<T1, T2>(
    Action<T1, T2, Action<OperationCompleteEvent>> func,
    T1 t1,
    T2 t2
)
```

Use to pause the script until an event happens.

| Parameter | Type | Description |
|---|---|---|
| `func` | [Action&lt;T1, T2, Action&lt;OperationCompleteEvent&gt;&gt;](https://learn.microsoft.com/dotnet/api/system.action-3) | The event-generating API to wait for. |
| `t1` | T1 | An argument to func. |
| `t2` | T2 | An argument to func. |

**Type parameters:** `T1`: The type of the argument to func.; `T2`: The type of the argument to func.

**Returns:** [OperationCompleteEvent](/scripting/api/sansar.script/operationcompleteevent) — An EventData that can be case to type corresponding to the event type of func.

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

Throws [`CoroutineException`](/scripting/api/sansar.script/coroutineexception) if called from a script&#39;s constructor.

</details>

</div>

<div class="api-card" id="waitfor-func-t-t-t">
<span id="M:Sansar.Script.ScriptBase.WaitFor``3(System.Func{``0,``1,``2,System.Action{Sansar.Script.EventData},System.Action{Sansar.Script.CancelData},System.Boolean,Sansar.Script.IEventSubscription},``0,``1,``2)"></span>

```csharp signature
protected EventData WaitFor<ARG1, ARG2, ARG3>(
    Func<ARG1, ARG2, ARG3, Action<EventData>, Action<Sansar.Script.CancelData>,System.Boolean,Sansar.Script.IEventSubscription> func,
    ARG1 arg1,
    ARG2 arg2,
    ARG3 arg3
)
```

Use to pause the script until an event happens.

| Parameter | Type | Description |
|---|---|---|
| `func` | [Func&lt;ARG1, ARG2, ARG3, Action&lt;EventData&gt;, Action&lt;Sansar.Script.CancelData&gt;,System.Boolean,Sansar.Script.IEventSubscription&gt;](https://learn.microsoft.com/dotnet/api/system.func-7) | The event-generating API to wait for. |
| `arg1` | ARG1 | An argument to func. |
| `arg2` | ARG2 | An argument to func. |
| `arg3` | ARG3 | An argument to func. |

**Type parameters:** `ARG1`: The type of the argument to func.; `ARG2`: The type of the argument to func.; `ARG3`: The type of the argument to func.

**Returns:** [EventData](/scripting/api/sansar.script/eventdata) — An EventData that can be case to type corresponding to the event type of func.

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

Throws [`CoroutineException`](/scripting/api/sansar.script/coroutineexception) if called from a script&#39;s constructor.

</details>

</div>

<div class="api-card" id="waitfor-func-t-t-t-2">
<span id="M:Sansar.Script.ScriptBase.WaitFor``3(System.Func{``0,``1,``2,System.Action{Sansar.Script.EventData},System.Boolean,Sansar.Script.IEventSubscription},``0,``1,``2)"></span>

```csharp signature
protected EventData WaitFor<ARG1, ARG2, ARG3>(
    Func<ARG1, ARG2, ARG3, Action<Sansar.Script.EventData>,System.Boolean,Sansar.Script.IEventSubscription> func,
    ARG1 arg1,
    ARG2 arg2,
    ARG3 arg3
)
```

Use to pause the script until an event happens.

| Parameter | Type | Description |
|---|---|---|
| `func` | [Func&lt;ARG1, ARG2, ARG3, Action&lt;Sansar.Script.EventData&gt;,System.Boolean,Sansar.Script.IEventSubscription&gt;](https://learn.microsoft.com/dotnet/api/system.func-6) | The event-generating API to wait for. |
| `arg1` | ARG1 | An argument to func. |
| `arg2` | ARG2 | An argument to func. |
| `arg3` | ARG3 | An argument to func. |

**Type parameters:** `ARG1`: The type of the argument to func.; `ARG2`: The type of the argument to func.; `ARG3`: The type of the argument to func.

**Returns:** [EventData](/scripting/api/sansar.script/eventdata) — An EventData that can be case to type corresponding to the event type of func.

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

Throws [`CoroutineException`](/scripting/api/sansar.script/coroutineexception) if called from a script&#39;s constructor.

</details>

</div>

<div class="api-card" id="waitfor-func-t-t-t-action">
<span id="M:Sansar.Script.ScriptBase.WaitFor``3(System.Func{``0,``1,``2,System.Action{Sansar.Script.EventData},System.Boolean,Sansar.Script.IEventSubscription},``0,``1,``2,System.Action)"></span>

```csharp signature
protected EventData WaitFor<ARG1, ARG2, ARG3>(
    Func<ARG1, ARG2, ARG3, Action<Sansar.Script.EventData>,System.Boolean,Sansar.Script.IEventSubscription> func,
    ARG1 arg1,
    ARG2 arg2,
    ARG3 arg3,
    Action run
)
```

Use to pause the script until an event happens.

| Parameter | Type | Description |
|---|---|---|
| `func` | [Func&lt;ARG1, ARG2, ARG3, Action&lt;Sansar.Script.EventData&gt;,System.Boolean,Sansar.Script.IEventSubscription&gt;](https://learn.microsoft.com/dotnet/api/system.func-6) | The event-generating API to wait for. |
| `arg1` | ARG1 | An argument to func. |
| `arg2` | ARG2 | An argument to func. |
| `arg3` | ARG3 | An argument to func. |
| `run` | [Action](https://learn.microsoft.com/dotnet/api/system.action) | An Action to run after subscribing the the event, but before waiting for the event to occurr. |

**Type parameters:** `ARG1`: The type of the argument to func.; `ARG2`: The type of the argument to func.; `ARG3`: The type of the argument to func.

**Returns:** [EventData](/scripting/api/sansar.script/eventdata) — An EventData that can be case to type corresponding to the event type of func.

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

Throws [`CoroutineException`](/scripting/api/sansar.script/coroutineexception) if called from a script&#39;s constructor.

</details>

</div>

<div class="api-card" id="waitfor-action-t-t-t">
<span id="M:Sansar.Script.ScriptBase.WaitFor``3(System.Action{``0,``1,``2,System.Action{Sansar.Script.OperationCompleteEvent}},``0,``1,``2)"></span>

```csharp signature
protected OperationCompleteEvent WaitFor<T1, T2, T3>(
    Action<T1, T2, T3, Action<OperationCompleteEvent>> func,
    T1 t1,
    T2 t2,
    T3 t3
)
```

Use to pause the script until an event happens.

| Parameter | Type | Description |
|---|---|---|
| `func` | [Action&lt;T1, T2, T3, Action&lt;OperationCompleteEvent&gt;&gt;](https://learn.microsoft.com/dotnet/api/system.action-4) | The event-generating API to wait for. |
| `t1` | T1 | An argument to func. |
| `t2` | T2 | An argument to func. |
| `t3` | T3 | An argument to func. |

**Type parameters:** `T1`: The type of the argument to func.; `T2`: The type of the argument to func.; `T3`: The type of the argument to func.

**Returns:** [OperationCompleteEvent](/scripting/api/sansar.script/operationcompleteevent) — An EventData that can be case to type corresponding to the event type of func.

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

Throws [`CoroutineException`](/scripting/api/sansar.script/coroutineexception) if called from a script&#39;s constructor.

</details>

</div>

<div class="api-card" id="waitfor-func-t-t-t-t">
<span id="M:Sansar.Script.ScriptBase.WaitFor``4(System.Func{``0,``1,``2,``3,System.Action{Sansar.Script.EventData},System.Action{Sansar.Script.CancelData},System.Boolean,Sansar.Script.IEventSubscription},``0,``1,``2,``3)"></span>

```csharp signature
protected EventData WaitFor<ARG1, ARG2, ARG3, ARG4>(
    Func<ARG1, ARG2, ARG3, ARG4, Action<EventData>, Action<Sansar.Script.CancelData>,System.Boolean,Sansar.Script.IEventSubscription> func,
    ARG1 arg1,
    ARG2 arg2,
    ARG3 arg3,
    ARG4 arg4
)
```

Use to pause the script until an event happens.

| Parameter | Type | Description |
|---|---|---|
| `func` | [Func&lt;ARG1, ARG2, ARG3, ARG4, Action&lt;EventData&gt;, Action&lt;Sansar.Script.CancelData&gt;,System.Boolean,Sansar.Script.IEventSubscription&gt;](https://learn.microsoft.com/dotnet/api/system.func-8) | The event-generating API to wait for. |
| `arg1` | ARG1 | An argument to func. |
| `arg2` | ARG2 | An argument to func. |
| `arg3` | ARG3 | An argument to func. |
| `arg4` | ARG4 | An argument to func. |

**Type parameters:** `ARG1`: The type of the argument to func.; `ARG2`: The type of the argument to func.; `ARG3`: The type of the argument to func.; `ARG4`: The type of the argument to func.

**Returns:** [EventData](/scripting/api/sansar.script/eventdata) — An EventData that can be case to type corresponding to the event type of func.

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

Throws [`CoroutineException`](/scripting/api/sansar.script/coroutineexception) if called from a script&#39;s constructor.

</details>

</div>

<div class="api-card" id="waitfor-func-t-t-t-t-2">
<span id="M:Sansar.Script.ScriptBase.WaitFor``4(System.Func{``0,``1,``2,``3,System.Action{Sansar.Script.EventData},System.Boolean,Sansar.Script.IEventSubscription},``0,``1,``2,``3)"></span>

```csharp signature
protected EventData WaitFor<ARG1, ARG2, ARG3, ARG4>(
    Func<ARG1, ARG2, ARG3, ARG4, Action<Sansar.Script.EventData>,System.Boolean,Sansar.Script.IEventSubscription> func,
    ARG1 arg1,
    ARG2 arg2,
    ARG3 arg3,
    ARG4 arg4
)
```

Use to pause the script until an event happens.

| Parameter | Type | Description |
|---|---|---|
| `func` | [Func&lt;ARG1, ARG2, ARG3, ARG4, Action&lt;Sansar.Script.EventData&gt;,System.Boolean,Sansar.Script.IEventSubscription&gt;](https://learn.microsoft.com/dotnet/api/system.func-7) | The event-generating API to wait for. |
| `arg1` | ARG1 | An argument to func. |
| `arg2` | ARG2 | An argument to func. |
| `arg3` | ARG3 | An argument to func. |
| `arg4` | ARG4 | An argument to func. |

**Type parameters:** `ARG1`: The type of the argument to func.; `ARG2`: The type of the argument to func.; `ARG3`: The type of the argument to func.; `ARG4`: The type of the argument to func.

**Returns:** [EventData](/scripting/api/sansar.script/eventdata) — An EventData that can be case to type corresponding to the event type of func.

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

Throws [`CoroutineException`](/scripting/api/sansar.script/coroutineexception) if called from a script&#39;s constructor.

</details>

</div>

<div class="api-card" id="waitfor-func-t-t-t-t-action">
<span id="M:Sansar.Script.ScriptBase.WaitFor``4(System.Func{``0,``1,``2,``3,System.Action{Sansar.Script.EventData},System.Boolean,Sansar.Script.IEventSubscription},``0,``1,``2,``3,System.Action)"></span>

```csharp signature
protected EventData WaitFor<ARG1, ARG2, ARG3, ARG4>(
    Func<ARG1, ARG2, ARG3, ARG4, Action<Sansar.Script.EventData>,System.Boolean,Sansar.Script.IEventSubscription> func,
    ARG1 arg1,
    ARG2 arg2,
    ARG3 arg3,
    ARG4 arg4,
    Action run
)
```

Use to pause the script until an event happens.

| Parameter | Type | Description |
|---|---|---|
| `func` | [Func&lt;ARG1, ARG2, ARG3, ARG4, Action&lt;Sansar.Script.EventData&gt;,System.Boolean,Sansar.Script.IEventSubscription&gt;](https://learn.microsoft.com/dotnet/api/system.func-7) | The event-generating API to wait for. |
| `arg1` | ARG1 | An argument to func. |
| `arg2` | ARG2 | An argument to func. |
| `arg3` | ARG3 | An argument to func. |
| `arg4` | ARG4 | An argument to func. |
| `run` | [Action](https://learn.microsoft.com/dotnet/api/system.action) | An Action to run after subscribing the the event, but before waiting for the event to occurr. |

**Type parameters:** `ARG1`: The type of the argument to func.; `ARG2`: The type of the argument to func.; `ARG3`: The type of the argument to func.; `ARG4`: The type of the argument to func.

**Returns:** [EventData](/scripting/api/sansar.script/eventdata) — An EventData that can be case to type corresponding to the event type of func.

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

Throws [`CoroutineException`](/scripting/api/sansar.script/coroutineexception) if called from a script&#39;s constructor.

</details>

</div>

<div class="api-card" id="waitfor-action-t-t-t-t">
<span id="M:Sansar.Script.ScriptBase.WaitFor``4(System.Action{``0,``1,``2,``3,System.Action{Sansar.Script.OperationCompleteEvent}},``0,``1,``2,``3)"></span>

```csharp signature
protected OperationCompleteEvent WaitFor<T1, T2, T3, T4>(
    Action<T1, T2, T3, T4, Action<OperationCompleteEvent>> func,
    T1 t1,
    T2 t2,
    T3 t3,
    T4 t4
)
```

Use to pause the script until an event happens.

| Parameter | Type | Description |
|---|---|---|
| `func` | [Action&lt;T1, T2, T3, T4, Action&lt;OperationCompleteEvent&gt;&gt;](https://learn.microsoft.com/dotnet/api/system.action-5) | The event-generating API to wait for. |
| `t1` | T1 | An argument to func. |
| `t2` | T2 | An argument to func. |
| `t3` | T3 | An argument to func. |
| `t4` | T4 | An argument to func. |

**Type parameters:** `T1`: The type of the argument to func.; `T2`: The type of the argument to func.; `T3`: The type of the argument to func.; `T4`: The type of the argument to func.

**Returns:** [OperationCompleteEvent](/scripting/api/sansar.script/operationcompleteevent) — An EventData that can be case to type corresponding to the event type of func.

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

Throws [`CoroutineException`](/scripting/api/sansar.script/coroutineexception) if called from a script&#39;s constructor.

</details>

</div>

<div class="api-card" id="waitfor-func-t-t-t-t-t">
<span id="M:Sansar.Script.ScriptBase.WaitFor``5(System.Func{``0,``1,``2,``3,``4,System.Action{Sansar.Script.EventData},System.Action{Sansar.Script.CancelData},System.Boolean,Sansar.Script.IEventSubscription},``0,``1,``2,``3,``4)"></span>

```csharp signature
protected EventData WaitFor<ARG1, ARG2, ARG3, ARG4, ARG5>(
    Func<ARG1, ARG2, ARG3, ARG4, ARG5, Action<EventData>, Action<Sansar.Script.CancelData>,System.Boolean,Sansar.Script.IEventSubscription> func,
    ARG1 arg1,
    ARG2 arg2,
    ARG3 arg3,
    ARG4 arg4,
    ARG5 arg5
)
```

Use to pause the script until an event happens.

| Parameter | Type | Description |
|---|---|---|
| `func` | [Func&lt;ARG1, ARG2, ARG3, ARG4, ARG5, Action&lt;EventData&gt;, Action&lt;Sansar.Script.CancelData&gt;,System.Boolean,Sansar.Script.IEventSubscription&gt;](https://learn.microsoft.com/dotnet/api/system.func-9) | The event-generating API to wait for. |
| `arg1` | ARG1 | An argument to func. |
| `arg2` | ARG2 | An argument to func. |
| `arg3` | ARG3 | An argument to func. |
| `arg4` | ARG4 | An argument to func. |
| `arg5` | ARG5 | An argument to func. |

**Type parameters:** `ARG1`: The type of the argument to func.; `ARG2`: The type of the argument to func.; `ARG3`: The type of the argument to func.; `ARG4`: The type of the argument to func.; `ARG5`: The type of the argument to func.

**Returns:** [EventData](/scripting/api/sansar.script/eventdata) — An EventData that can be case to type corresponding to the event type of func.

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

Throws [`CoroutineException`](/scripting/api/sansar.script/coroutineexception) if called from a script&#39;s constructor.

</details>

</div>

<div class="api-card" id="waitfor-func-t-t-t-t-t-2">
<span id="M:Sansar.Script.ScriptBase.WaitFor``5(System.Func{``0,``1,``2,``3,``4,System.Action{Sansar.Script.EventData},System.Boolean,Sansar.Script.IEventSubscription},``0,``1,``2,``3,``4)"></span>

```csharp signature
protected EventData WaitFor<ARG1, ARG2, ARG3, ARG4, ARG5>(
    Func<ARG1, ARG2, ARG3, ARG4, ARG5, Action<Sansar.Script.EventData>,System.Boolean,Sansar.Script.IEventSubscription> func,
    ARG1 arg1,
    ARG2 arg2,
    ARG3 arg3,
    ARG4 arg4,
    ARG5 arg5
)
```

Use to pause the script until an event happens.

| Parameter | Type | Description |
|---|---|---|
| `func` | [Func&lt;ARG1, ARG2, ARG3, ARG4, ARG5, Action&lt;Sansar.Script.EventData&gt;,System.Boolean,Sansar.Script.IEventSubscription&gt;](https://learn.microsoft.com/dotnet/api/system.func-8) | The event-generating API to wait for. |
| `arg1` | ARG1 | An argument to func. |
| `arg2` | ARG2 | An argument to func. |
| `arg3` | ARG3 | An argument to func. |
| `arg4` | ARG4 | An argument to func. |
| `arg5` | ARG5 | An argument to func. |

**Type parameters:** `ARG1`: The type of the argument to func.; `ARG2`: The type of the argument to func.; `ARG3`: The type of the argument to func.; `ARG4`: The type of the argument to func.; `ARG5`: The type of the argument to func.

**Returns:** [EventData](/scripting/api/sansar.script/eventdata) — An EventData that can be case to type corresponding to the event type of func.

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

Throws [`CoroutineException`](/scripting/api/sansar.script/coroutineexception) if called from a script&#39;s constructor.

</details>

</div>

<div class="api-card" id="waitfor-func-t-t-t-t-t-action">
<span id="M:Sansar.Script.ScriptBase.WaitFor``5(System.Func{``0,``1,``2,``3,``4,System.Action{Sansar.Script.EventData},System.Boolean,Sansar.Script.IEventSubscription},``0,``1,``2,``3,``4,System.Action)"></span>

```csharp signature
protected EventData WaitFor<ARG1, ARG2, ARG3, ARG4, ARG5>(
    Func<ARG1, ARG2, ARG3, ARG4, ARG5, Action<Sansar.Script.EventData>,System.Boolean,Sansar.Script.IEventSubscription> func,
    ARG1 arg1,
    ARG2 arg2,
    ARG3 arg3,
    ARG4 arg4,
    ARG5 arg5,
    Action run
)
```

Use to pause the script until an event happens.

| Parameter | Type | Description |
|---|---|---|
| `func` | [Func&lt;ARG1, ARG2, ARG3, ARG4, ARG5, Action&lt;Sansar.Script.EventData&gt;,System.Boolean,Sansar.Script.IEventSubscription&gt;](https://learn.microsoft.com/dotnet/api/system.func-8) | The event-generating API to wait for. |
| `arg1` | ARG1 | An argument to func. |
| `arg2` | ARG2 | An argument to func. |
| `arg3` | ARG3 | An argument to func. |
| `arg4` | ARG4 | An argument to func. |
| `arg5` | ARG5 | An argument to func. |
| `run` | [Action](https://learn.microsoft.com/dotnet/api/system.action) | An Action to run after subscribing the the event, but before waiting for the event to occurr. |

**Type parameters:** `ARG1`: The type of the argument to func.; `ARG2`: The type of the argument to func.; `ARG3`: The type of the argument to func.; `ARG4`: The type of the argument to func.; `ARG5`: The type of the argument to func.

**Returns:** [EventData](/scripting/api/sansar.script/eventdata) — An EventData that can be case to type corresponding to the event type of func.

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

Throws [`CoroutineException`](/scripting/api/sansar.script/coroutineexception) if called from a script&#39;s constructor.

</details>

</div>

### WaitForLock

<div class="api-card">
<span id="M:Sansar.Script.ScriptBase.WaitForLock(System.Object)"></span>

```csharp signature
protected void WaitForLock(object Token)
```

WaitFor and take a lock on Token

| Parameter | Type | Description |
|---|---|---|
| `Token` | [object](https://learn.microsoft.com/dotnet/api/system.object) | The object controlling the lock. Recommended to be a private object member. |

**Throws** [`ArgumentNullException`](https://learn.microsoft.com/dotnet/api/system.argumentnullexception): Thrown if Token is null.

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

It is highly recommended to use [`ScriptBase.Lock`](/scripting/api/sansar.script/scriptbase#lock) instead. This is for advanced use only and can easily create deadlocks that stop scripts from working. If not paired with [`ScriptBase.ReleaseLock`](/scripting/api/sansar.script/scriptbase#releaselock) will create a deadlock preventing this code from being entered.

</details>

<details class="api-example" open><summary>Example</summary>

```csharp
private object BalanceLock = new Object();
int Balance = 100;
bool Spend(int value)
{
    WaitForLock(BalanceLock);
    bool success = false;
    if (Balance > value)
    {
        Balance -= value;
        success = true;
    }
    ReleaseLock(BalanceLock);
    // Note that any early exit between WaitForLock and ReleaseLock fail to release the lock, creating a deadlock the next time this method is called.
    // For this reason it is highly recommended to instead do: using (Lock(BalanceLock))
    return success;
}
```

</details>

</div>

### WaitForSignal

<div class="api-card">
<span id="M:Sansar.Script.ScriptBase.WaitForSignal"></span>
<span id="M:Sansar.Script.ScriptBase.WaitForSignal()"></span>

```csharp signature
protected int WaitForSignal()
```

Wait the current coroutine until at least 1 signal is sent to it through the ICoroutine interface.

**Returns:** [int](https://learn.microsoft.com/dotnet/api/system.int32) — The number of signals received while waiting.

</div>

### Yield

<div class="api-card">
<span id="M:Sansar.Script.ScriptBase.Yield"></span>
<span id="M:Sansar.Script.ScriptBase.Yield()"></span>

```csharp signature
[Sansar.Script.NonReflective]
public void Yield()
```

Yield to let other coroutines or events run.

</div>


## Used in examples

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

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