TeleportToBestMatchServer

Method

Teleport local actor to the best match server instance based on the specified properties. If no match can be found, a new server instance will be created with the specified properties.

For example:

  • A server exists that has a property "gameMode" with value "deathmatch"
  • You can call this method with serverProperties={"gameMode": "deathmatch"} and serverPropertiesToMatch={"gameMode"}
  • This will find the existing server and teleport the actor to that server
  • If no server exists with the specified properties, a new server will be created with the specified properties

Definition

void TeleportToBestMatchServer(int maxParticipants = 0, IReadOnlyCollection<KeyValuePair<string, object>> serverProperties = null, IReadOnlyCollection<string> serverPropertiesToMatch = null)
void TeleportToBestMatchServer(int maxParticipants = 0, IReadOnlyCollection<KeyValuePair<string, object>> serverProperties = null, IReadOnlyCollection<string> serverPropertiesToMatch = null)

Parameters

ParameterDescription
int maxParticipants

Find a server that matches this maxParticipants count; 0 means "any" and will default to the settings set for the space if no match was found

IReadOnlyCollection<KeyValuePair<string, object>> serverProperties

Specify the properties that you want to match for

IReadOnlyCollection<string> serverPropertiesToMatch

Specify which properties from "serverProperties" that should be used for matchmaking.

Examples

public Image loadingImage;
public GameObject lobbyArea;
public GameObject gameArea;
public Button joinGameButton;

void OnEnable()
{
SpatialBridge.networkingService.onConnectionStatusChanged += HandleConnectionStatusChanged;

joinGameButton.onClick.AddListener(JoinGame);
}

void OnDisable()
{
SpatialBridge.networkingService.onConnectionStatusChanged -= HandleConnectionStatusChanged;
}

void JoinGame()
{
SpatialBridge.networkingService.TeleportToBestMatchServer(
maxParticipants: 20,
serverProperties: new Dictionary<string, object> { { "isGame", true } }
);
}

private void HandleConnectionStatusChanged(ServerConnectionStatus status)
{
if (status == ServerConnectionStatus.Connected)
{
// Already connected, hide loading image
loadingImage.gameObject.SetActive(false);

// Check if we're on lobby or game server
if (SpatialBridge.networkingService.GetServerProperties().ContainsKey("isGame"))
{
HandleJoinedGameServer();
}
else
{
HandleJoinedLobbyServer();
}

}
else if (status == ServerConnectionStatus.Connecting)
{
loadingImage.gameObject.SetActive(true);
}
}

private void HandleJoinedLobbyServer()
{
lobbyArea.SetActive(true);
gameArea.SetActive(false);
}

private void HandleJoinedGameServer()
{
lobbyArea.SetActive(false);
gameArea.SetActive(true);

// Add additional game area setup here
}
public Image loadingImage;
public GameObject lobbyArea;
public GameObject gameArea;
public Button joinGameButton;

void OnEnable()
{
SpatialBridge.networkingService.onConnectionStatusChanged += HandleConnectionStatusChanged;

joinGameButton.onClick.AddListener(JoinGame);
}

void OnDisable()
{
SpatialBridge.networkingService.onConnectionStatusChanged -= HandleConnectionStatusChanged;
}

void JoinGame()
{
SpatialBridge.networkingService.TeleportToBestMatchServer(
maxParticipants: 20,
serverProperties: new Dictionary<string, object> { { "isGame", true } }
);
}

private void HandleConnectionStatusChanged(ServerConnectionStatus status)
{
if (status == ServerConnectionStatus.Connected)
{
// Already connected, hide loading image
loadingImage.gameObject.SetActive(false);

// Check if we're on lobby or game server
if (SpatialBridge.networkingService.GetServerProperties().ContainsKey("isGame"))
{
HandleJoinedGameServer();
}
else
{
HandleJoinedLobbyServer();
}

}
else if (status == ServerConnectionStatus.Connecting)
{
loadingImage.gameObject.SetActive(true);
}
}

private void HandleJoinedLobbyServer()
{
lobbyArea.SetActive(true);
gameArea.SetActive(false);
}

private void HandleJoinedGameServer()
{
lobbyArea.SetActive(false);
gameArea.SetActive(true);

// Add additional game area setup here
}