SceneObjectScript

SceneObjectScript Class

Extend SceneObjectScript 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

[Sansar.Script.Interface]
public abstract class SceneObjectScript : Sansar.Script.ScriptBase

Remarks

Override Sansar.Script.ScriptBase.Init() for script initialization, primarily event subscriptions.

Use SceneObjectScript.ObjectPrivate to access the Object the script is on.

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

C# Example
/* 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: "© 2018 Linden Research, 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. © 2018 Linden Research, 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)."
 */

/* Start a coroutine to wait for collision events.
 * Which types of collisions are handled may be set on the properties panel for
 * the object when editing the scene.
 */
using Sansar.Simulation;
using Sansar.Script;
using System;

// To get full access to the Object API a script must extend from ObjectScript
public class SceneObjectScriptExample : SceneObjectScript
{
    // Components can be set in the editor if the correct component types are added to the object
    [EditorVisible]
    private RigidBodyComponent RigidBody = null;

    [DefaultValue(true)]
    [DisplayName("Track Avatar Hits")]
    [EditorVisible]
    private bool TrackAgentHits = true;

    [DefaultValue(true)]
    [DisplayName("Track Object Hits")]
    [EditorVisible]
    private bool TrackObjectHits = true;

    public SceneObjectScriptExample()
    {
        // Script initialization order:
        //    Constructor (this method)
        //    Set base class members (ObjectScript's ExperienceInfo, SceneInfo, and ObjectPrivate)
        //    Set public fields that were configured at edit time (RigidBody, TrackAgentHist, TrackObjectHits)
        //    Init()
    }

    // Override Init to set up event handlers and start coroutines.
    public override void Init()
    {
        // When unhandled exceptions occur, they may be caught with this event handler.
        // Certain exceptions may not be recoverable at all and may cause the script to
        // immediately be removed.
        Script.UnhandledException += UnhandledException;

        // If the object had its components set in the editor they should now have the member initialized
        // The component can also be found dynamically
        if (RigidBody == null)
        {
            if (!ObjectPrivate.TryGetFirstComponent(out RigidBody))
            {
                // Since object scripts are initialized when the scene loads, no one will actually see this message.
                ScenePrivate.Chat.MessageAllUsers("There is no RigidBodyComponent attached to this object.");
                return;
            }
        }

        // Convert the supplied bools to the correct CollisionEventType to track
        CollisionEventType trackedEvents = 0;
        if (TrackAgentHits)
        {
            trackedEvents |= CollisionEventType.CharacterContact;
        }
        if (TrackObjectHits)
        {
            trackedEvents |= CollisionEventType.RigidBodyContact;
        }

        // StartCoroutine will queue CheckForCollisions to run
        // Arguments after the coroutine method are passed as arguments to the method.
        StartCoroutine(CheckForCollisions, trackedEvents);
    }

    private void CheckForCollisions(CollisionEventType trackedEvents)
    {
        while (true)
        {
            // This will block the coroutine until a collision happens
            CollisionData data = (CollisionData)WaitFor(RigidBody.Subscribe, trackedEvents, Sansar.Script.ComponentId.Invalid);
            if (data.EventType == CollisionEventType.CharacterContact)
            {
                ScenePrivate.Chat.MessageAllUsers("I hit an avatar!");
            }
            else
            {
                ScenePrivate.Chat.MessageAllUsers("I hit an object!");
            }
        }
    }

    private void UnhandledException(object sender, Exception e)
    {
        // Depending on the script scheduling policy, the exception may or may not be recoverable.
        // An unrecoverable exception that can be handled will only be given a short time to run
        // so the handler method needs to be kept small.
        // Any exception thrown from this method will terminate the script regardless of the value
        // of UnhandledExceptionRecoverable
        if (!Script.UnhandledExceptionRecoverable)
        {
            ScenePrivate.Chat.MessageAllUsers("Unrecoverable exception happened, the script will now be removed.");
        }
        else
        {
            ScenePrivate.Chat.MessageAllUsers("This script will be allowed to continue.");
        }
    }
}

Requirements

Namespace: Sansar.Simulation
Assembly: Sansar.Simulation (in Sansar.Simulation.dll)
Assembly Versions: 1.1.0.0

Members

See Also: Inherited members from Sansar.Script.ScriptBase.

Protected Constructors

 
This constructor is called before any properties have been set. Override Sansar.Script.ScriptBase.Init() to initialize the script after properties have been set.

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]
override
AllowedContexts Sansar.Script.Reflective.Context . Internal use only.
[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.)

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.)
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

 
GetAllCoroutines () : IReadOnlyList<Sansar.Script.ICoroutine>
A list of all coroutines for this script. (Inherited from Sansar.Script.ScriptBase.)
 
GetCoroutineCount () : int
The total number of coroutines running on this script. (Inherited from Sansar.Script.ScriptBase.)
 
Lock (object) : IDisposable
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. (Inherited from Sansar.Script.ScriptBase.)
 
PostScriptEvent (string)
Post the event for all scripts. (Inherited from Sansar.Script.ScriptBase.)
 
PostScriptEvent (string, Sansar.Script.Reflective)
Post the event for all scripts. (Inherited from Sansar.Script.ScriptBase.)
 
PostScriptEvent (Sansar.Script.ScriptId, string, Sansar.Script.Reflective)
Post the event for the target script. (Inherited from Sansar.Script.ScriptBase.)
 
PostSimpleScriptEvent (string, object)
Deprecated. Use Sansar.Script.ScriptBase.PostScriptEvent(Sansar.Script.ScriptId, string, Sansar.Script.Reflective) (Inherited from Sansar.Script.ScriptBase.)
 
PostSimpleScriptEvent (Sansar.Script.ScriptId, string, object)
Deprecated. Use Sansar.Script.ScriptBase.PostScriptEvent(Sansar.Script.ScriptId, string, Sansar.Script.Reflective) (Inherited from Sansar.Script.ScriptBase.)
 
ReleaseLock (object)
Release a lock if held. (Inherited from Sansar.Script.ScriptBase.)
 
StartCoroutine (Action, Action<Sansar.Script.OperationCompleteEvent>) : Sansar.Script.ICoroutine
Starts a coroutine on the current script. (Inherited from Sansar.Script.ScriptBase.)
 
StartCoroutine<T> (Action<T>, T, Action<Sansar.Script.OperationCompleteEvent>) : Sansar.Script.ICoroutine
Starts a coroutine on the current script. (Inherited from Sansar.Script.ScriptBase.)
 
StartCoroutine<T,T1> (Action<T, T1>, T, T1, Action<Sansar.Script.OperationCompleteEvent>) : Sansar.Script.ICoroutine
Starts a coroutine on the current script. (Inherited from Sansar.Script.ScriptBase.)
 
StartCoroutine<T,T1,T2> (Action<T, T1, T2>, T, T1, T2, Action<Sansar.Script.OperationCompleteEvent>) : Sansar.Script.ICoroutine
Starts a coroutine on the current script. (Inherited from Sansar.Script.ScriptBase.)
 
StartCoroutine<T,T1,T2,T3> (Action<T, T1, T2, T3>, T, T1, T2, T3, Action<Sansar.Script.OperationCompleteEvent>) : Sansar.Script.ICoroutine
Starts a coroutine on the current script. (Inherited from Sansar.Script.ScriptBase.)
 
SubscribeToScriptEvent (string, Action<Sansar.Script.ScriptEventData>, bool) : Sansar.Script.IEventSubscription
Subscribes to events sent by other scripts (Inherited from Sansar.Script.ScriptBase.)
 
SubscribeToScriptEvent (Sansar.Script.ScriptId, string, Action<Sansar.Script.ScriptEventData>, bool) : Sansar.Script.IEventSubscription
Subscribes to events sent only by a specific script. (Inherited from Sansar.Script.ScriptBase.)
 
Terminate (string)
Terminates this script immediately. (Inherited from Sansar.Script.ScriptBase.)
 
Wait (double)
Delays execution of the current coroutine for the specified time. (Inherited from Sansar.Script.ScriptBase.)
 
Wait (TimeSpan)
Delays execution of the current coroutine for the specified time. (Inherited from Sansar.Script.ScriptBase.)
 
WaitFor (Sansar.Script.ICoroutine)
Block the current coroutine until otherCoroutine finishes. (Inherited from Sansar.Script.ScriptBase.)
 
WaitFor (Action<Action<Sansar.Script.OperationCompleteEvent>>) : Sansar.Script.OperationCompleteEvent
Use to pause the script until an event happens. (Inherited from Sansar.Script.ScriptBase.)
 
WaitFor (Func<Action<Sansar.Script.EventData>, Action<Sansar.Script.CancelData>,System.Boolean,Sansar.Script.IEventSubscription>) : Sansar.Script.EventData
Use to pause the script until an event happens. (Inherited from Sansar.Script.ScriptBase.)
 
WaitFor (Func<Action<Sansar.Script.EventData>,System.Boolean,Sansar.Script.IEventSubscription>) : Sansar.Script.EventData
Use to pause the script until an event happens. (Inherited from Sansar.Script.ScriptBase.)
 
WaitFor (Func<Action<Sansar.Script.EventData>,System.Boolean,Sansar.Script.IEventSubscription>, Action) : Sansar.Script.EventData
Use to pause the script until an event happens. (Inherited from Sansar.Script.ScriptBase.)
 
WaitFor<ARG1> (Func<ARG1, Action<Sansar.Script.EventData>, Action<Sansar.Script.CancelData>,System.Boolean,Sansar.Script.IEventSubscription>, ARG1) : Sansar.Script.EventData
Use to pause the script until an event happens. (Inherited from Sansar.Script.ScriptBase.)
 
WaitFor<ARG1> (Func<ARG1, Action<Sansar.Script.EventData>,System.Boolean,Sansar.Script.IEventSubscription>, ARG1) : Sansar.Script.EventData
Use to pause the script until an event happens. (Inherited from Sansar.Script.ScriptBase.)
 
WaitFor<ARG1> (Func<ARG1, Action<Sansar.Script.EventData>,System.Boolean,Sansar.Script.IEventSubscription>, ARG1, Action) : Sansar.Script.EventData
Use to pause the script until an event happens. (Inherited from Sansar.Script.ScriptBase.)
 
WaitFor<T1> (Action<T1, Action<Sansar.Script.OperationCompleteEvent>>, T1) : Sansar.Script.OperationCompleteEvent
Use to pause the script until an event happens. (Inherited from Sansar.Script.ScriptBase.)
 
WaitFor<ARG1,ARG2> (Func<ARG1, ARG2, Action<Sansar.Script.EventData>, Action<Sansar.Script.CancelData>,System.Boolean,Sansar.Script.IEventSubscription>, ARG1, ARG2) : Sansar.Script.EventData
Use to pause the script until an event happens. (Inherited from Sansar.Script.ScriptBase.)
 
WaitFor<ARG1,ARG2> (Func<ARG1, ARG2, Action<Sansar.Script.EventData>,System.Boolean,Sansar.Script.IEventSubscription>, ARG1, ARG2) : Sansar.Script.EventData
Use to pause the script until an event happens. (Inherited from Sansar.Script.ScriptBase.)
 
WaitFor<ARG1,ARG2> (Func<ARG1, ARG2, Action<Sansar.Script.EventData>,System.Boolean,Sansar.Script.IEventSubscription>, ARG1, ARG2, Action) : Sansar.Script.EventData
Use to pause the script until an event happens. (Inherited from Sansar.Script.ScriptBase.)
 
WaitFor<T1,T2> (Action<T1, T2, Action<Sansar.Script.OperationCompleteEvent>>, T1, T2) : Sansar.Script.OperationCompleteEvent
Use to pause the script until an event happens. (Inherited from Sansar.Script.ScriptBase.)
 
WaitFor<ARG1,ARG2,ARG3> (Func<ARG1, ARG2, ARG3, Action<Sansar.Script.EventData>, Action<Sansar.Script.CancelData>,System.Boolean,Sansar.Script.IEventSubscription>, ARG1, ARG2, ARG3) : Sansar.Script.EventData
Use to pause the script until an event happens. (Inherited from Sansar.Script.ScriptBase.)
 
WaitFor<ARG1,ARG2,ARG3> (Func<ARG1, ARG2, ARG3, Action<Sansar.Script.EventData>,System.Boolean,Sansar.Script.IEventSubscription>, ARG1, ARG2, ARG3) : Sansar.Script.EventData
Use to pause the script until an event happens. (Inherited from Sansar.Script.ScriptBase.)
 
WaitFor<ARG1,ARG2,ARG3> (Func<ARG1, ARG2, ARG3, Action<Sansar.Script.EventData>,System.Boolean,Sansar.Script.IEventSubscription>, ARG1, ARG2, ARG3, Action) : Sansar.Script.EventData
Use to pause the script until an event happens. (Inherited from Sansar.Script.ScriptBase.)
 
WaitFor<T1,T2,T3> (Action<T1, T2, T3, Action<Sansar.Script.OperationCompleteEvent>>, T1, T2, T3) : Sansar.Script.OperationCompleteEvent
Use to pause the script until an event happens. (Inherited from Sansar.Script.ScriptBase.)
 
WaitFor<ARG1,ARG2,ARG3,ARG4> (Func<ARG1, ARG2, ARG3, ARG4, Action<Sansar.Script.EventData>, Action<Sansar.Script.CancelData>,System.Boolean,Sansar.Script.IEventSubscription>, ARG1, ARG2, ARG3, ARG4) : Sansar.Script.EventData
Use to pause the script until an event happens. (Inherited from Sansar.Script.ScriptBase.)
 
WaitFor<ARG1,ARG2,ARG3,ARG4> (Func<ARG1, ARG2, ARG3, ARG4, Action<Sansar.Script.EventData>,System.Boolean,Sansar.Script.IEventSubscription>, ARG1, ARG2, ARG3, ARG4) : Sansar.Script.EventData
Use to pause the script until an event happens. (Inherited from Sansar.Script.ScriptBase.)
 
WaitFor<ARG1,ARG2,ARG3,ARG4> (Func<ARG1, ARG2, ARG3, ARG4, Action<Sansar.Script.EventData>,System.Boolean,Sansar.Script.IEventSubscription>, ARG1, ARG2, ARG3, ARG4, Action) : Sansar.Script.EventData
Use to pause the script until an event happens. (Inherited from Sansar.Script.ScriptBase.)
 
WaitFor<T1,T2,T3,T4> (Action<T1, T2, T3, T4, Action<Sansar.Script.OperationCompleteEvent>>, T1, T2, T3, T4) : Sansar.Script.OperationCompleteEvent
Use to pause the script until an event happens. (Inherited from Sansar.Script.ScriptBase.)
 
WaitFor<ARG1,ARG2,ARG3,ARG4,ARG5> (Func<ARG1, ARG2, ARG3, ARG4, ARG5, Action<Sansar.Script.EventData>, Action<Sansar.Script.CancelData>,System.Boolean,Sansar.Script.IEventSubscription>, ARG1, ARG2, ARG3, ARG4, ARG5) : Sansar.Script.EventData
Use to pause the script until an event happens. (Inherited from Sansar.Script.ScriptBase.)
 
WaitFor<ARG1,ARG2,ARG3,ARG4,ARG5> (Func<ARG1, ARG2, ARG3, ARG4, ARG5, Action<Sansar.Script.EventData>,System.Boolean,Sansar.Script.IEventSubscription>, ARG1, ARG2, ARG3, ARG4, ARG5) : Sansar.Script.EventData
Use to pause the script until an event happens. (Inherited from Sansar.Script.ScriptBase.)
 
WaitFor<ARG1,ARG2,ARG3,ARG4,ARG5> (Func<ARG1, ARG2, ARG3, ARG4, ARG5, Action<Sansar.Script.EventData>,System.Boolean,Sansar.Script.IEventSubscription>, ARG1, ARG2, ARG3, ARG4, ARG5, Action) : Sansar.Script.EventData
Use to pause the script until an event happens. (Inherited from Sansar.Script.ScriptBase.)
 
WaitForLock (object)
WaitFor and take a lock on Token (Inherited from Sansar.Script.ScriptBase.)
 
WaitForSignal () : int
Wait the current coroutine until at least 1 signal is sent to it through the ICoroutine interface. (Inherited from Sansar.Script.ScriptBase.)

Member Details

SceneObjectScript Constructor

This constructor is called before any properties have been set. Override Sansar.Script.ScriptBase.Init() to initialize the script after properties have been set.

Syntax

[Sansar.Script.Interface]
protected SceneObjectScript ()

Remarks

 

Requirements

Namespace: Sansar.Simulation
Assembly: Sansar.Simulation (in Sansar.Simulation.dll)
Assembly Versions: 1.1.0.0

AllowedContexts Property

Internal use only.

Syntax

protected override sealed Sansar.Script.Reflective.Context AllowedContexts { get; }

Value

Documentation for this section has not yet been entered.

Remarks

Documentation for this section has not yet been entered.

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

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


Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request

0 Comments

Article is closed for comments.