Index Based Spawning

Hey Muhammad, sorry for the delay in replying. This thread slipped off my mind.

We actually realised there’s a small bug in the sample scene, which could lead to unexpected behaviour. So I went in and fixed it, that’s why it took me a bit longer.

Because the new version of this sample will probably be included in our upcoming release (1.4.0, probably), it might take some time… but I think you need it now, so I put it into a .zip file so you can explore it now:

Download: Package Samples 1.4.0 Preview

I want to mention though that, bug aside, the logic is kinda similar. I’ll re-explain here using the class names in the sample scene:

  1. There’s a network entity in the scene called SpawnManager.
  2. There are a bunch of spawn points. These are not networked. The SpawnManager knows about them (has them in a pre-populated list).
  3. First player to connect takes control of it. This player is the one to assign spawn points.
  4. They immediately create a player for themselves, using the method described below, by messaging their own ClientConnection:
AssignSpawnPointToClient(coherenceBridge.ClientConnections.GetMine()); // For this Client
  1. This player relies on ClientConnections to assign spawn points:
coherenceBridge.ClientConnections.OnCreated += AssignSpawnPointToClient; // For future Clients

So as soon as a new CC is created, it messages it with the spawn point position:

private void AssignSpawnPointToClient(CoherenceClientConnection clientConnection)
{
    clientConnection.SendClientMessage<Client>(nameof(Client.SpawnPlayer), MessageTarget.AuthorityOnly, spawnPosition);
}
  1. The ClientConnection Prefab of the other player who just connected receives the message, and creates a character for themselves at the position sent by the first player:
[Command]
public void SpawnPlayer(Vector3 spawnPosition) { ... }

So again: PlayerSpawner > sees ClientConnection, messages it > (on the remote client) ClientConnection Prefab receives message > Spawns its own player at position

If this is still unclear, I would strongly suggest to familiarise yourself with ClientConnections on their own first.

Hope it helps.

1 Like