Get SpatialOS

Sites

Menu

Connecting to and disconnecting from SpatialOS

Connection and disconnection is already set up in all of our example projects, using the convention in Bootstrap.cs. See examples:

This page is for those who want to customise or learn more about the connection process.

Customising the connection and disconnection process allows you to do things like:

Connect to SpatialOS

To connect to SpatialOS, you must configure your worker before calling SpatialOS.Connect.

public class Bootstrap : MonoBehaviour
{
    private void Start()
    {
        SpatialOS.ApplyConfiguration(new WorkerConfigurationData());
        SpatialOS.Connect(gameObject);
    }
}

Disconnect from SpatialOS

To disconnect from SpatialOS, call SpatialOS.Disconnect().

private void ConnectionTimeout()
{
    if (SpatialOS.IsConnected)
    {
        SpatialOS.Disconnect();
    }
}

Optional callbacks

Handle a successful attempt to connect

To register a callback in the event of a successful connection to SpatialOS, use OnConnected.

public class Bootstrap : MonoBehaviour
{
    private void Start()
    {
        SpatialOS.ApplyConfiguration(new WorkerConfigurationData());
        SpatialOS.OnConnected += CreatePlayer;
        SpatialOS.Connect(gameObject);
    }

    private void CreatePlayer()
    {
        //CreatePlayer
    }
}

Handle a failed attempt to connect

To register a callback in the event of an unsuccessful connection to SpatialOS, use OnConnectionFailed.

public class Bootstrap : MonoBehaviour
{
    private void Start()
    {
        SpatialOS.ApplyConfiguration(new WorkerConfigurationData());
        SpatialOS.OnConnectionFailed += () => Debug.LogError("Connection Failed.");
        SpatialOS.Connect(gameObject);
    }
}

Handle a disconnection

To register a callback in the event of a disconnection from SpatialOS, use OnDisconnected.

public class Bootstrap : MonoBehaviour
{
    private void Start()
    {
        SpatialOS.ApplyConfiguration(new WorkerConfigurationData());
        SpatialOS.OnDisconnected += reason => UIHandler.ShowDisconnectionScreen(reason);
        SpatialOS.Connect(gameObject);
    }
}

Was this page helpful?

Thanks for letting us know!

Thanks for your feedback

Need more help? Ask on the forums