# Sansar Script API

Sansar uses a limited subset of the mono .net api. Click [here](https://help.sansar.com/hc/en-us/articles/115003662127) 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.
   * Sansar currently supports:
     * [Scene Object Script](https://help.sansar.com/hc/en-us/articles/115003361306) for scripts attached to objects that are part of the scene.\
       This will give it access to [Sansar.Simulation.ObjectPrivate](https://help.sansar.com/hc/en-us/articles/115003361366) for the object the script is on, and [Sansar.Simulation.ScenePrivate](https://help.sansar.com/hc/en-us/articles/115003342683) for the scene the object is a part of.
   * There is planned support for:
     * [Sansar.Simulation.AgentScript](https://help.sansar.com/hc/en-us/articles/115003552648-AgentScript) for scripts that are attached directly to agents.\
       This will give it access to [Sansar.Simulation.ObjectPrivate](https://help.sansar.com/hc/en-us/articles/115003361366) for the avatar object, [Sansar.Simulation.AgentPrivate](https://help.sansar.com/hc/en-us/articles/115003361386) for the agent the script is on, and [Sansar.Simulation.ScenePublic](https://help.sansar.com/hc/en-us/articles/115003342723) for the scene the agent is currently in.
     * **Sansar.Simulation.SceneScript** for scripts that are attached directly to the experience.\
       This will give it access to the [Sansar.Simulation.ScenePrivate](https://help.sansar.com/hc/en-us/articles/115003342683) interface for the scene the script is attached to.
     * **Sansar.Simulation.ObjectScript** for scripts attached to objects that are not part of the scene.\
       This will give it access to [Sansar.Simulation.ObjectPrivate](https://help.sansar.com/hc/en-us/articles/115003361366) for the object the script is on, and [Sansar.Simulation.ScenePublic](https://help.sansar.com/hc/en-us/articles/115003342723) for the scene the object is currently in.
2. Create a new class that extends the chosen base class.

   <table data-header-hidden><thead><tr><th></th></tr></thead><tbody><tr><td><strong>C# Example</strong></td></tr><tr><td><pre><code>public class MyScript : SceneObjectScript
   </code></pre></td></tr><tr><td></td></tr></tbody></table>
3. Create public fields of supported types for any parameters that should be set in the object properties when editing the scene.

   <table data-header-hidden><thead><tr><th></th></tr></thead><tbody><tr><td><strong>C# Example</strong></td></tr><tr><td><pre><code>public bool TrackAgentHits = true;
   </code></pre></td></tr><tr><td>public bool TrackObjectHits = true;</td></tr><tr><td></td></tr></tbody></table>
4. Override init() to subscribe to events or initialize coroutines that will wait for events.

   <table data-header-hidden><thead><tr><th></th></tr></thead><tbody><tr><td><strong>C# Example</strong></td></tr><tr><td><pre><code>public override void Init()
   </code></pre></td></tr><tr><td>{</td></tr></tbody></table>

   ```
   // 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.

   <table data-header-hidden><thead><tr><th></th></tr></thead><tbody><tr><td><strong>C# Example</strong></td></tr><tr><td><pre><code>void AddUser(string Action, Sansar.Script.SessionId User, string Data)
   </code></pre></td></tr><tr><td>{</td></tr></tbody></table>

   ```
   // 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!](http://discord.gg/sansarofficial)
