Skip to content

Get Sansar

Script API

SimpleScript

Where: Client
More page actions

For the complete documentation index, see llms.txt. This page is also available as Markdown.

Class Namespace Sansar.Simulation Inherits Sansar.Script.ScriptBase

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.

[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 to access the Object the script is on.

Use SimpleScript.ScenePrivate to access the Scene the object is in.

Override SimpleScript.OnAddUser to handle AddUser events.

Override SimpleScript.OnRemoveUser to handle AddUser events.

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

Override SimpleScript.OnTimer to do something at regular intervals.

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

Override SimpleScript.OnChat to handle Chat events when users or scripts chat.

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 to handle Collision events with the object this script is on.

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

Override SimpleScript.OnScriptEvent to handle ScriptEvent events from other scripts.

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.

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");
}
}

Source: Examples/SimpleScriptExample.cs in the Sansar 47.10.0 client ScriptApi snapshot.

Example

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

Members at a glance

KindMembers
ConstructorsSimpleScript
PropertiesObjectPrivate · RigidBodyComponent · ScenePrivate
MethodsGetSubscription · Init · OnAddUser · OnChat · OnCollision · OnRemoveUser · OnScriptEvent · OnTimer · SimpleInit

Constructors

SimpleScript

Deprecated
[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.

Properties

ObjectPrivate

[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 — The scene object this script is attached to if it is attached to an object, null otherwise.

Remarks

For AgentScripts this is the Agent’s ObjectPrivate of their avatar.

RigidBodyComponent

protected RigidBodyComponent RigidBodyComponent { get; }

The first RigidBodyComponent on ObjectPrivate

Value: RigidBodyComponent — The first SimpleScript.RigidBodyComponent on the object this script is attached to.

Remarks

This is a convenience for:

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

ScenePrivate

[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 — The Scene API for this scene if this script was attached to scene content, null otherwise.

Remarks

This value will be set before Init is called.

Methods

GetSubscription

protected Sansar.Script.IEventSubscription GetSubscription(     string methodName )

Get the IEventSubscription for any of the registered event subscription methods

ParameterTypeDescription
methodNamestringThe n

Returns: Sansar.Script.IEventSubscription — An IEventSubscription interface that can be used to unsubscribe from a registered event.

Example
GetSubscription("OnChat").Unsubscribe();

Init

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

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

OnAddUser

Deprecated
[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.

ParameterTypeDescription
agentAgentPrivateThe AgentPrivate for the user that has joined the scene.
Remarks

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

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

OnChat

Deprecated
[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.

ParameterTypeDescription
dataChatDataChatData for the message.
Remarks

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

OnCollision

Deprecated
[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.

ParameterTypeDescription
dataCollisionDataThe CollisionData about the collision.
Remarks

By default receives events for every collision. Use SimpleScript.OnCollisionOptionsAttribute to change which types of collision events will trigger OnCollision.

OnRemoveUser

Deprecated
[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.

ParameterTypeDescription
dataAgentInfoUserData for the user that has left the scene.
Remarks

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

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

OnScriptEvent

Deprecated
[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.

ParameterTypeDescription
sourceScriptIdSansar.Script.ScriptIdThe ScriptId of the script that sent the message
dataobjectThe data sent by another script, as an object.
Remarks

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 “MySimpleScript”

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:

class SimpleData : Reflective
{
public object Data;
}

Then send the data like this:

PostScriptEvent("simpleeventname",new SimpleData {Data = position});
Example
// 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);
}

OnTimer

Deprecated
[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.

Remarks

Defaults to one call per second. Change the rate with SimpleScript.OnTimerOptionsAttribute

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

SimpleInit

Deprecated
[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

Remarks

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.

Used in examples

Generated from the Sansar 47.10.0 client ScriptApi snapshot.