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.ScriptBaseRemarks
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.
/* (C) Linden Research, Inc. All Rights Reserved. */
using Sansar.Script;using Sansar.Simulation;
// Updated from deprecated SimpleScript to SceneObjectScriptpublic 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
| Kind | Members |
|---|---|
| Constructors | SimpleScript |
| Properties | ObjectPrivate · RigidBodyComponent · ScenePrivate |
| Methods | GetSubscription · Init · OnAddUser · OnChat · OnCollision · OnRemoveUser · OnScriptEvent · OnTimer · SimpleInit |
Constructors
SimpleScript
[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
| Parameter | Type | Description |
|---|---|---|
methodName | string | The 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
[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 | The 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
[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 | ChatData 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
[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 | The 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
[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 | UserData 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
[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 | The ScriptId of the script that sent the message |
data | object | The 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
[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
[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.