Your games will likely want to react to people joining and leaving the game. To help with this you can implement Component.INetworkListener or Component.INetworkSpawn on a component and place it in the scene.
Here's an example, where on joining the game a player object is created and the incoming player is assigned as the owner.
public sealed class GameNetworkManager : Component, Component.INetworkListener
{
[Property] public GameObject PlayerPrefab { get; set; }
[Property] public GameObject SpawnPoint { get; set; }
/// <summary>
/// Called on the host when someone successfully joins the server (including the local player)
/// </summary>
public void OnActive( Connection connection )
{
// Spawn a player for this client
var player = PlayerPrefab.Clone( SpawnPoint.WorldTransform );
// Find the NameTag component and set their name correctly
var nameTag = player.Components.Get<NameTagPanel>( FindMode.EverythingInSelfAndDescendants );
if ( nameTag is not null )
{
nameTag.Name = connection.DisplayName;
}
// Spawn it on the network, assign connection as the owner
player.NetworkSpawn( connection );
}
}
The interface INetworkListener has multiple methods that you can optionally override. These are all called on the host only.
| Method | Description |
|---|---|
AcceptConnection |
Called before a connection is let in, so you can turn it away. |
OnConnected |
The client has connected to the server. They're about to start handshaking, in which they'll load the game and download all the required packages. |
OnDisconnected |
The client has disconnected from the server. |
OnActive |
The client is fully connected and completed the handshake. After this call they will close the loading screen and start playing. |
OnBecameHost |
The previous host left and you are now the host. |
Return false to deny the connection, and set reason to the message the client is shown. If any component implementing this returns false, the connection is denied.
public bool AcceptConnection( Connection channel, ref string reason )
{
if ( BannedSteamIds.Contains( channel.SteamId ) )
{
reason = "You're banned from this server.";
return false;
}
if ( Connection.All.Count >= MaxPlayers )
{
reason = "Server is full.";
return false;
}
return true;
}
In a lobby, the host can leave. When that happens one of the remaining clients is promoted, and gets this callback with the connection of the host that left. This is where you'd pick up anything only the host was doing.
public void OnBecameHost( Connection previousHost )
{
Log.Info( $"{previousHost.DisplayName} left, we're the host now" );
}
The interface INetworkSpawn has a method to react to objects spawning on the network.
| Method | Description |
|---|---|
OnNetworkSpawn |
Called when this object is spawned on the network. |