Sansar Script API

Sansar uses C# scripts to provide dynamic behaviors and interactions.

Sansar uses a limited subset of the mono .net api. Click here for a list of C# libraries usable by Sansar scripts.

Documentation for the Sansar script API.


Creating a Sansar script:

  1. Choose a base class for your script depending on where the script will live and what APIs it needs access to. There are three core APIs: Object, Scene, and Agent. Each of these is split into two interfaces: a Public interface that is generally available and a Private interface that is a more complete superset of the Public interface. The base class chosen will determine which APIs are available to the script.

  2. Create a new class that extends the chosen base class.

    C# Example

    public class MyScript : SceneObjectScript

  3. Create public fields of supported types for any parameters that should be set in the object properties when editing the scene.

    C# Example

    public bool TrackAgentHits = true;

    public bool TrackObjectHits = true;

  4. Override init() to subscribe to events or initialize coroutines that will wait for events.

    C# Example

    public override void Init()

    {

    // Subscribe to Add User events. Use SessionId.Invalid to track all users.
    ScenePrivate.User.Subscribe(UserEvents.AddUser, SessionId.Invalid, AddUser);

    } |

  5. Write event handlers and coroutines as needed to process events.

    C# Example

    void AddUser(string Action, Sansar.Script.SessionId User, string Data)

    {

    // Lookup the name of the agent.
    string name = ScenePrivate.FindAgent(User).AgentInfo.Name;
    ScenePrivate.Chat.MessageAllUsers(string.Format("Welcome {0}!", name));

    } |

  6. Upload the script in Sansar via the "Upload" button in inventory while editing a scene.

    • Errors and Warnings will show in the upload window.

    • Some APIs are "Restricted": generally these are unstable APIs that are being tested. Since the APIs may change or even be removed in the future scripts that use them should not be sold or traded.

    • The allowed C# libraries are intitially severely limited but will be expanded over time.

  7. Once successully compiled the script will show up in inventory. Drag it from there onto an object in the scene.

    • Only successfully compiled scripts will be added to inventory.

  8. Edit the properties on the object to set any parameters from the public fields set in step 3 above.

  9. Save and publish the scene.


Have more questions? Ask in our Discord!

Last updated