# SimpleScript

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>Inherits <a href="/scripting/api/sansar.script/scriptbase">Sansar.Script.ScriptBase</a></span>
</div>

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

Extend SimpleScript to create a script that can be used on content that is natively in the scene. This is the primary script type in Sansar.

```csharp
[System.Obsolete("SimpleScript is deprecated and will not receive new features. Please use SceneObjectScript or ObjectScript.", true)]
public abstract class SimpleScript : Sansar.Script.ScriptBase
```

## Remarks

Use [`SimpleScript.ObjectPrivate`](/scripting/api/sansar.simulation/simplescript#objectprivate) to access the Object the script is on.



Use [`SimpleScript.ScenePrivate`](/scripting/api/sansar.simulation/simplescript#sceneprivate) to access the Scene the object is in.



Override [`SimpleScript.OnAddUser`](/scripting/api/sansar.simulation/simplescript#onadduser) to handle AddUser events.



Override [`SimpleScript.OnRemoveUser`](/scripting/api/sansar.simulation/simplescript#onremoveuser) to handle AddUser events.

```csharp
protected override OnRemoveUser(AgentInfo agent)
{
    Log.Write($"{agent.Name} has left the region.");
}
```



Override [`SimpleScript.OnTimer`](/scripting/api/sansar.simulation/simplescript#ontimer) to do something at regular intervals.

```csharp
protected override OnTimer()
{
    RigidBodyComponent?.SetPosition(ObjectPrivate?.InitialPosition);
}
```



Override [`SimpleScript.OnChat`](/scripting/api/sansar.simulation/simplescript#onchat) to handle Chat events when users or scripts chat.

```csharp
protected override OnChat(ChatData data)
{
    AgentPrivate agent = ScenePrivate.FindAgent(data.SourceId);
    if (agent != null && agent.AgentInfo.AvatarUuid == ScenePrivate.SceneInfo.AvatarUuid))
    {
       Log.Write($"Scene owner {agent.AgentInfo.Name} said {data.Message}");
    }
}
```



Override [`SimpleScript.OnCollision`](/scripting/api/sansar.simulation/simplescript#oncollision) to handle Collision events with the object this script is on.

```csharp
protected override OnCollision(CollisionData data)
{
    Log.Write($"Bumped {data.HitObject.ObjectId}");
}
```



Override [`SimpleScript.OnScriptEvent`](/scripting/api/sansar.simulation/simplescript#onscriptevent) to handle ScriptEvent events from other scripts.

```csharp
protected override OnScriptEvent(AgentPrivate agent)
{
    ISimpleScriptEvent simple = data.Data.As<ISimpleScriptEvent>();
    Log.Write($"Received message {data.Message} event with data {simple.Value}");
}
```



Override `SimpleScript.SimpleInit` for more advanced script initialization.



```csharp title="SimpleScriptExample.cs"
/* (C) Linden Research, Inc. All Rights Reserved. */

using Sansar.Script;
using Sansar.Simulation;

// Updated from deprecated SimpleScript to SceneObjectScript
public class SimpleScriptExample : SceneObjectScript
{
    public override void Init()
    {
        // SceneObjectScript uses standard event subscription instead of RegisterSimpleScriptEvent
        Log.Write(LogLevel.Info, "SimpleScriptExample initialized");
    }
}
```

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

## Example

```csharp
protected override OnAddUser(AgentPrivate agent)
{
    agent.SendChat($"Welcome to the {ScenePrivate.SceneInfo.ExperienceName} scene!");
}
```

## Members at a glance

| Kind | Members |
|---|---|
| **Constructors** | [SimpleScript](#simplescript) |
| **Properties** | [ObjectPrivate](#objectprivate) · [RigidBodyComponent](#rigidbodycomponent) · [ScenePrivate](#sceneprivate) |
| **Methods** | [GetSubscription](#getsubscription) · [Init](#init) · [OnAddUser](#onadduser) · [OnChat](#onchat) · [OnCollision](#oncollision) · [OnRemoveUser](#onremoveuser) · [OnScriptEvent](#onscriptevent) · [OnTimer](#ontimer) · [SimpleInit](#simpleinit) |

## Constructors

### SimpleScript

<div class="api-card">
<span id="M:Sansar.Simulation.SimpleScript.#ctor"></span>
<span id="C:Sansar.Simulation.SimpleScript()"></span>
<div class="api-badges"><span class="api-badge api-deprecated">Deprecated</span></div>

```csharp signature
[System.Obsolete("SimpleScript is deprecated and will not receive new features. Please use SceneObjectScript or ObjectScript.", false)]
protected SimpleScript()
```

This constructor is called before any properties have been set. Override `SimpleScript.SimpleInit` to initialize the script after properties have been set and events setup.

</div>


## Properties

### ObjectPrivate

<div class="api-card">
<span id="P:Sansar.Simulation.SimpleScript.ObjectPrivate"></span>

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

The ObjectPrivate this script is attached to if it is attached to an object.

**Value:** [ObjectPrivate](/scripting/api/sansar.simulation/objectprivate) — The scene object this script is attached to if it is attached to an object, null otherwise.

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

For AgentScripts this is the Agent&#39;s ObjectPrivate of their avatar.

</details>

</div>

### RigidBodyComponent

<div class="api-card">
<span id="P:Sansar.Simulation.SimpleScript.RigidBodyComponent"></span>

```csharp signature
protected RigidBodyComponent RigidBodyComponent { get; }
```

The first RigidBodyComponent on ObjectPrivate

**Value:** [RigidBodyComponent](/scripting/api/sansar.simulation/rigidbodycomponent) — The first [`SimpleScript.RigidBodyComponent`](/scripting/api/sansar.simulation/simplescript#rigidbodycomponent) on the object this script is attached to.

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

This is a convenience for:

```csharp
RigidBodyComponent rigidbody;
 if (ObjectPrivate != null && ObjectPrivate.TryGetFirstComponent(out rigidbody))
{
    // Do something with rigidbody
}
```

</details>

</div>

### ScenePrivate

<div class="api-card">
<span id="P:Sansar.Simulation.SimpleScript.ScenePrivate"></span>

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

The Scene API for the Scene this script is a part of if the script is attached to scene content.

**Value:** [ScenePrivate](/scripting/api/sansar.simulation/sceneprivate) — The Scene API for this scene if this script was attached to scene content, null otherwise.

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

This value will be set before Init is called.

</details>

</div>


## Methods

### GetSubscription

<div class="api-card">
<span id="M:Sansar.Simulation.SimpleScript.GetSubscription(System.String)"></span>

```csharp signature
protected Sansar.Script.IEventSubscription GetSubscription(
    string methodName
)
```

Get the IEventSubscription for any of the registered event subscription methods

| Parameter | Type | Description |
|---|---|---|
| `methodName` | [string](https://learn.microsoft.com/dotnet/api/system.string) | The n |

**Returns:** [Sansar.Script.IEventSubscription](/scripting/api/sansar.script/ieventsubscription) — An IEventSubscription interface that can be used to unsubscribe from a registered event.

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

```csharp
GetSubscription("OnChat").Unsubscribe();
```

</details>

</div>

### Init

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

```csharp signature
[Sansar.Script.Api]
public override sealed void Init()
```

Init() initializes all event subscriptions for the overridable methods in SimpleScript

</div>

### OnAddUser

<div class="api-card">
<span id="M:Sansar.Simulation.SimpleScript.OnAddUser(Sansar.Simulation.AgentPrivate)"></span>
<div class="api-badges"><span class="api-badge api-deprecated">Deprecated</span></div>

```csharp signature
[System.Obsolete("SimpleScript is deprecated and will not receive new features. Please use SceneObjectScript or ObjectScript.", false)]
protected virtual void OnAddUser(AgentPrivate agent)
```

Code in OnAddUser will run whenever a user enters the scene.

| Parameter | Type | Description |
|---|---|---|
| `agent` | [AgentPrivate](/scripting/api/sansar.simulation/agentprivate) | The AgentPrivate for the user that has joined the scene. |

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

Override OnAddUser to handle events when a user enters the scene.

</details>

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

```csharp
protected override OnAddUser(AgentPrivate agent)
{
   agent.SendChat($"Welcome to the {ScenePrivate.SceneInfo.ExperienceName} scene!");
}
```

</details>

</div>

### OnChat

<div class="api-card">
<span id="M:Sansar.Simulation.SimpleScript.OnChat(Sansar.Simulation.ChatData)"></span>
<div class="api-badges"><span class="api-badge api-deprecated">Deprecated</span></div>

```csharp signature
[System.Obsolete("SimpleScript is deprecated and will not receive new features. Please use SceneObjectScript or ObjectScript.", false)]
protected virtual void OnChat(ChatData data)
```

Code in OnChat will run whenever chat is heard.

| Parameter | Type | Description |
|---|---|---|
| `data` | [ChatData](/scripting/api/sansar.simulation/chatdata) | ChatData for the message. |

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

Defaults to all chat on the default channel. Use \[OnChatOptions\] to change the channel or limit the source of the chat.

</details>

</div>

### OnCollision

<div class="api-card">
<span id="M:Sansar.Simulation.SimpleScript.OnCollision(Sansar.Simulation.CollisionData)"></span>
<div class="api-badges"><span class="api-badge api-deprecated">Deprecated</span></div>

```csharp signature
[System.Obsolete("SimpleScript is deprecated and will not receive new features. Please use SceneObjectScript or ObjectScript.", false)]
protected virtual void OnCollision(CollisionData data)
```

Receive events whenever the object this script is on collides with something or someone.

| Parameter | Type | Description |
|---|---|---|
| `data` | [CollisionData](/scripting/api/sansar.simulation/collisiondata) | The CollisionData about the collision. |

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

By default receives events for every collision. Use [`SimpleScript.OnCollisionOptionsAttribute`](/scripting/api/sansar.simulation/simplescript.oncollisionoptionsattribute) to change which types of collision events will trigger OnCollision.

</details>

</div>

### OnRemoveUser

<div class="api-card">
<span id="M:Sansar.Simulation.SimpleScript.OnRemoveUser(Sansar.Simulation.AgentInfo)"></span>
<div class="api-badges"><span class="api-badge api-deprecated">Deprecated</span></div>

```csharp signature
[System.Obsolete("SimpleScript is deprecated and will not receive new features. Please use SceneObjectScript or ObjectScript.", false)]
protected virtual void OnRemoveUser(AgentInfo data)
```

Code in OnRemoveUser will run whenever a user leaves the scene.

| Parameter | Type | Description |
|---|---|---|
| `data` | [AgentInfo](/scripting/api/sansar.simulation/agentinfo) | UserData for the user that has left the scene. |

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

Override OnAddUser to handle events when a user enters the scene.

</details>

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

```csharp
// This event occurs when a user leaves the scene
            protected override void OnRemoveUser(AgentInfo info)
            {
                Log.Write($"{info.Name} has left the region.");
            }
```

</details>

</div>

### OnScriptEvent

<div class="api-card">
<span id="M:Sansar.Simulation.SimpleScript.OnScriptEvent(Sansar.Script.ScriptId,System.Object)"></span>
<div class="api-badges"><span class="api-badge api-deprecated">Deprecated</span></div>

```csharp signature
[System.Obsolete("SimpleScript is deprecated and will not receive new features. Please use SceneObjectScript or ObjectScript.", false)]
protected virtual void OnScriptEvent(
    Sansar.Script.ScriptId sourceScriptId,
    object data
)
```

Receive events from other scripts.

| Parameter | Type | Description |
|---|---|---|
| `sourceScriptId` | [Sansar.Script.ScriptId](/scripting/api/sansar.script/scriptid) | The [`ScriptId`](/scripting/api/sansar.script/scriptid) of the script that sent the message |
| `data` | [object](https://learn.microsoft.com/dotnet/api/system.object) | The data sent by another script, as an object. |

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

If EventName is not set then OnScriptEvent will receive events with the name of the script class. In the following example it will receive events named &quot;MySimpleScript&quot;

```csharp
public class SimpleExample : SimpleScript
{
    // Send events to this handler from other simple scripts with: PostScriptEvent("SimpleExample", myVector);
    protected override void OnScriptEvent(object eventData)
    {
        Vector newPos = eventData as Vector;
        if (newPos != null) AllPositions.Add(newPos);
    }
}    
```



To send events to a SimpleScript from a non-simple script, the non-simple script must build a Reflective class that contains a single public object field named Data like this:

```csharp
class SimpleData : Reflective
{
    public object Data;
}
```

 Then send the data like this:

```csharp
PostScriptEvent("simpleeventname",new SimpleData {Data = position});
```

</details>

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

```csharp
// Send events to this handler from other simple scripts with: PostScriptEvent("AddPosition", myVector);
[OnScriptEventOptions(EventName="AddPosition")]
protected override void OnScriptEvent(object eventData)
{
    Vector newPos = eventData as Vector;
    if (newPos != null) AllPositions.Add(newPos);
}
```

</details>

</div>

### OnTimer

<div class="api-card">
<span id="M:Sansar.Simulation.SimpleScript.OnTimer"></span>
<span id="M:Sansar.Simulation.SimpleScript.OnTimer()"></span>
<div class="api-badges"><span class="api-badge api-deprecated">Deprecated</span></div>

```csharp signature
[System.Obsolete("SimpleScript is deprecated and will not receive new features. Please use SceneObjectScript or ObjectScript.", false)]
protected virtual void OnTimer()
```

Code in OnTimer will run at regular intervals.

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

Defaults to one call per second. Change the rate with [`SimpleScript.OnTimerOptionsAttribute`](/scripting/api/sansar.simulation/simplescript.ontimeroptionsattribute)

</details>

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

```csharp
// Set OnTimer to happen once every minute.
[OnTimerOptions(Rate=60)]
protected override void OnTimer()
```

</details>

</div>

### SimpleInit

<div class="api-card">
<span id="M:Sansar.Simulation.SimpleScript.SimpleInit"></span>
<span id="M:Sansar.Simulation.SimpleScript.SimpleInit()"></span>
<div class="api-badges"><span class="api-badge api-deprecated">Deprecated</span></div>

```csharp signature
[System.Obsolete("SimpleScript is deprecated and will not receive new features. Please use SceneObjectScript or ObjectScript.", false)]
protected virtual void SimpleInit()
```

Override SimpleInit for script setup, such as subscribing to other events or

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

SimpleInit is run after events have been set up for overridden methods. Use it to set up more complex subscriptions or otherwise initialize the script.

</details>

</div>


## Used in examples

- [SimpleScriptExample.cs](/scripting/examples/simple-script)

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