SimpleScript Class
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.
Syntax
[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(AgentPrivate) to handle AddUser events.
C# Example |
protected override OnAddUser(AgentPrivate agent) { agent.SendChat($"Welcome to the {ScenePrivate.SceneInfo.ExperienceName} scene!"); } |
Override SimpleScript.OnRemoveUser(AgentInfo) to handle AddUser events.
C# Example |
protected override OnRemoveUser(AgentInfo agent) { Log.Write($"{agent.Name} has left the region."); } |
Override SimpleScript.OnTimer to do something at regular intervals.
C# Example |
protected override OnTimer() { RigidBodyComponent?.SetPosition(ObjectPrivate?.InitialPosition); } |
Override SimpleScript.OnChat(ChatData) to handle Chat events when users or scripts chat.
C# Example |
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(CollisionData) to handle Collision events with the object this script is on.
C# Example |
protected override OnCollision(CollisionData data) { Log.Write($"Bumped {data.HitObject.ObjectId}"); } |
Override SimpleScript.OnScriptEvent(Sansar.Script.ScriptId, object) to handle ScriptEvent events from other scripts.
C# Example |
protected override OnScriptEvent(AgentPrivate agent) { ISimpleScriptEvent simple = data.Data.As<ISimpleScriptEvent>(); Log.Write($"Received message {data.Message} event with data {simple.Value}"); } |
Override Sansar.Script.SimpleScript.SimpleInit() for more advanced script initialization.
C# Example |
|
Requirements
Assembly: Sansar.Simulation (in Sansar.Simulation.dll)
Assembly Versions: 1.1.0.0
Members
See Also: Inherited members from Sansar.Script.ScriptBase.
Protected Constructors
|
SimpleScript ()
|
This constructor is called before any properties have been set. Override Sansar.Script.SimpleScript.SimpleInit() to initialize the script after properties have been set and events setup. |
Public Properties
|
Log | Sansar.Script.Log . Gets the script console. (Inherited from Sansar.Script.ScriptBase.) |
|
Memory | Sansar.Script.Memory . Memory information for the pool this script is in. (Inherited from Sansar.Script.ScriptBase.) |
|
ObjectPrivate | ObjectPrivate . The ObjectPrivate this script is attached to if it is attached to an object. |
|
ScenePrivate | ScenePrivate . The Scene API for the Scene this script is a part of if the script is attached to scene content. |
|
Script | Sansar.Script.ScriptHandle . Script handle to this script. (Inherited from Sansar.Script.ScriptBase.) |
Protected Properties
[read-only]
|
AllowedContexts | Sansar.Script.Reflective.Context . Internal Use Only. Overridden by subclasses to return only those contexts requested which are allowed for that type of script. (Inherited from Sansar.Script.Reflective.) |
[read-only]
|
CurrentCoroutine | Sansar.Script.ICoroutine . Gets the ICoroutine interface for the current coroutine. (Inherited from Sansar.Script.ScriptBase.) |
[read-only]
|
MaxCoroutines | int . The maximum number of coroutines that a single script can run. (Inherited from Sansar.Script.ScriptBase.) |
[read-only]
|
PendingEventCount | int . The number of events currently waiting to be processed. (Inherited from Sansar.Script.ScriptBase.) |
[read-only]
|
ReflectiveContexts | Sansar.Script.Reflective.Context . Override ReflectiveContexts to limit which contexts this Reflective interface is available in when registered with. (Inherited from Sansar.Script.Reflective.) |
[read-only]
|
ReflectiveName | string . Override ReflectiveName to change which name this class will be registered as in the Reflective system. (Inherited from Sansar.Script.Reflective.) |
[read-only]
|
RigidBodyComponent | RigidBodyComponent . The first RigidBodyComponent on ObjectPrivate |
Public Methods
|
AsInterface<TInterface> () : TInterface
Returns a TInterface object if one can be created, null otherwise (Inherited from Sansar.Script.Reflective.) |
|
|
FullInterface (string) : string
Generates a string which shows all the members which can be reflected. (Inherited from Sansar.Script.Reflective.) |
|
override
|
Init ()
Init() initializes all event subscriptions for the overridable methods in SimpleScript |
|
abstract
|
Init ()
Init() is called after all interfaces have been initialized. (Inherited from Sansar.Script.ScriptBase.) |
|
|
Register ()
Register this object to be found with ScenePrivate.FindReflective(string) (Inherited from Sansar.Script.Reflective.) |
|
|
Unregister ()
Unregister this object so it will not be found with ScenePrivate.FindReflective(string) (Inherited from Sansar.Script.Reflective.) |
|
|
Yield ()
Yield to let other coroutines or events run. (Inherited from Sansar.Script.ScriptBase.) |
Protected Methods
Member Details
SimpleScript Constructor
This constructor is called before any properties have been set. Override Sansar.Script.SimpleScript.SimpleInit() to initialize the script after properties have been set and events setup.Syntax
[Sansar.Script.Interface]
[System.Obsolete("SimpleScript is deprecated and will not receive new features. Please use SceneObjectScript or ObjectScript.", false)]
protected SimpleScript ()Remarks
Requirements
Namespace: Sansar.Simulation
Assembly: Sansar.Simulation (in Sansar.Simulation.dll)
Assembly Versions: 1.1.0.0
GetSubscription Method
Get the IEventSubscription for any of the registered event subscription methodsSyntax
protected Sansar.Script.IEventSubscription GetSubscription (string methodName)Parameters
- methodName
- The n
Returns
An IEventSubscription interface that can be used to unsubscribe from a registered event.Remarks
Documentation for this section has not yet been entered.Example
C# Example GetSubscription("OnChat").Unsubscribe();Requirements
Namespace: Sansar.Simulation
Assembly: Sansar.Simulation (in Sansar.Simulation.dll)
Assembly Versions: 1.1.0.0
Init Method
Init() initializes all event subscriptions for the overridable methods in SimpleScriptSyntax
[Sansar.Script.Api]
public override sealed void Init ()See Also
Remarks
Requirements
Namespace: Sansar.Simulation
Assembly: Sansar.Simulation (in Sansar.Simulation.dll)
Assembly Versions: 1.1.0.0
ObjectPrivate Property
The ObjectPrivate this script is attached to if it is attached to an object.Syntax
[Sansar.Script.NonReflective]
[get: Sansar.Script.Interface]
public ObjectPrivate ObjectPrivate { protected get; set; }Value
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.Requirements
Namespace: Sansar.Simulation
Assembly: Sansar.Simulation (in Sansar.Simulation.dll)
Assembly Versions: 1.1.0.0
OnAddUser Method
Code in OnAddUser will run whenever a user enters the scene.Syntax
[Sansar.Script.Interface]
[System.Obsolete("SimpleScript is deprecated and will not receive new features. Please use SceneObjectScript or ObjectScript.", false)]
protected virtual void OnAddUser (AgentPrivate agent)See Also
Parameters
- agent
- The AgentPrivate for the user that has joined the scene.
Remarks
Override OnAddUser to handle events when a user enters the scene.Example
C# Example protected override OnAddUser(AgentPrivate agent) { agent.SendChat($"Welcome to the {ScenePrivate.SceneInfo.ExperienceName} scene!"); }Requirements
Namespace: Sansar.Simulation
Assembly: Sansar.Simulation (in Sansar.Simulation.dll)
Assembly Versions: 1.1.0.0
OnChat Method
Code in OnChat will run whenever chat is heard.Syntax
[Sansar.Script.Interface]
[System.Obsolete("SimpleScript is deprecated and will not receive new features. Please use SceneObjectScript or ObjectScript.", false)]
protected virtual void OnChat (ChatData data)See Also
Parameters
- data
- 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.Requirements
Namespace: Sansar.Simulation
Assembly: Sansar.Simulation (in Sansar.Simulation.dll)
Assembly Versions: 1.1.0.0
OnCollision Method
Receive events whenever the object this script is on collides with something or someone.Syntax
[Sansar.Script.Interface]
[System.Obsolete("SimpleScript is deprecated and will not receive new features. Please use SceneObjectScript or ObjectScript.", false)]
protected virtual void OnCollision (CollisionData data)See Also
Parameters
- data
- The CollisionData about the collision.
Remarks
By default receives events for every collision. Use Sansar.Simulation.SimpleScript.OnCollisionOptionsAttribute to change which types of collision events will trigger OnCollision.Requirements
Namespace: Sansar.Simulation
Assembly: Sansar.Simulation (in Sansar.Simulation.dll)
Assembly Versions: 1.1.0.0
OnRemoveUser Method
Code in OnRemoveUser will run whenever a user leaves the scene.Syntax
[Sansar.Script.Interface]
[System.Obsolete("SimpleScript is deprecated and will not receive new features. Please use SceneObjectScript or ObjectScript.", false)]
protected virtual void OnRemoveUser (AgentInfo data)See Also
Parameters
- data
- UserData for the user that has left the scene.
Remarks
Override OnAddUser to handle events when a user enters the scene.Example
C# Example // This event occurs when a user leaves the scene protected override void OnRemoveUser(AgentInfo info) { Log.Write($"{info.Name} has left the region."); }Requirements
Namespace: Sansar.Simulation
Assembly: Sansar.Simulation (in Sansar.Simulation.dll)
Assembly Versions: 1.1.0.0
OnScriptEvent Method
Receive events from other scripts.Syntax
[Sansar.Script.Interface]
[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)See Also
Parameters
- sourceScriptId
- The Sansar.Script.ScriptId of the script that sent the message
- data
- 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"
C# Example 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:
C# Example class SimpleData : Reflective { public object Data; }Then send the data like this:
C# Example PostScriptEvent("simpleeventname",new SimpleData {Data = position});Example
C# 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); }Requirements
Namespace: Sansar.Simulation
Assembly: Sansar.Simulation (in Sansar.Simulation.dll)
Assembly Versions: 1.1.0.0
OnTimer Method
Code in OnTimer will run at regular intervals.Syntax
[Sansar.Script.Interface]
[System.Obsolete("SimpleScript is deprecated and will not receive new features. Please use SceneObjectScript or ObjectScript.", false)]
protected virtual void OnTimer ()Remarks
Defaults to one call per second. Change the rate with Sansar.Simulation.SimpleScript.OnTimerOptionsAttributeExample
C# Example // Set OnTimer to happen once every minute. [OnTimerOptions(Rate=60)] protected override void OnTimer()Requirements
Namespace: Sansar.Simulation
Assembly: Sansar.Simulation (in Sansar.Simulation.dll)
Assembly Versions: 1.1.0.0
RigidBodyComponent Property
The first RigidBodyComponent on ObjectPrivateSyntax
[get: Sansar.Script.Interface]
protected RigidBodyComponent RigidBodyComponent { get; }Value
The first SimpleScript.RigidBodyComponent on the object this script is attached to.Remarks
This is a convenience for:
C# Example RigidBodyComponent rigidbody; if (ObjectPrivate != null && ObjectPrivate.TryGetFirstComponent(out rigidbody)) { // Do something with rigidbody }Requirements
Namespace: Sansar.Simulation
Assembly: Sansar.Simulation (in Sansar.Simulation.dll)
Assembly Versions: 1.1.0.0
ScenePrivate Property
The Scene API for the Scene this script is a part of if the script is attached to scene content.Syntax
[Sansar.Script.NonReflective]
[get: Sansar.Script.Interface]
public ScenePrivate ScenePrivate { protected get; set; }Value
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.Requirements
Namespace: Sansar.Simulation
Assembly: Sansar.Simulation (in Sansar.Simulation.dll)
Assembly Versions: 1.1.0.0
SimpleInit Method
Override SimpleInit for script setup, such as subscribing to other events orSyntax
[Sansar.Script.Interface]
[System.Obsolete("SimpleScript is deprecated and will not receive new features. Please use SceneObjectScript or ObjectScript.", false)]
protected virtual void SimpleInit ()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.Requirements
Namespace: Sansar.Simulation
Assembly: Sansar.Simulation (in Sansar.Simulation.dll)
Assembly Versions: 1.1.0.0
0 Comments