# Deprecations

### Room Player Spawn

The number of scripts required to create players in the room has been reduced. It was made more useful by writing a utility class.

**New Usage:**

```csharp
public class RoomPlayerSpawn : MonoBehaviour
{
    [SerializeField] private GameObject _gamePlayerPrefab;
    [SerializeField] private GameObject _lobbyPlayerPrefab;

    private int _playerCount;

    [SerializeField] private bool _useLobby;
    
    private void Start()
    {
        RoomManagerBase.Singleton.Events.OnServerJoinedRoom += CreateGamePlayer;
        
        if (_useLobby)
            RoomManagerBase.Singleton.Events.OnServerExitedRoom += CreateLobbyPlayer;
        else
            RoomManagerBase.Singleton.Events.OnServerExitedRoom += PlayerCreatorUtilities.RemovePlayer;
    }
    
    private void CreateGamePlayer(NetworkConnection conn)
    {
        var player = PlayerCreatorUtilities.TryCreatePlayerOrReplace(conn, _gamePlayerPrefab);

        if (player.TryGetComponent(out SimpleCharacterController controller))
        {
            controller.ID = conn.connectionId;
        }
    }
    
    private void CreateLobbyPlayer(NetworkConnection conn)
    {
        PlayerCreatorUtilities.ReplacePlayer(conn, _lobbyPlayerPrefab);
    }
}
```

**Old Usage:**

```csharp
 public class RoomPlayerSpawn : MonoBehaviour
    {
        [SerializeField] private GameObject _gamePlayerPrefab;
        [SerializeField] private GameObject _lobbyPlayerPrefab;

        private int _playerCount;

        [SerializeField] private bool _useLobby;

        private void Start()
        {
            RoomManagerBase.Singleton.Events.OnServerJoinedRoom += CreateGamePlayer;

            if (_useLobby)
                RoomManagerBase.Singleton.Events.OnServerExitedRoom += CreateLobbyPlayer;
            else
                RoomManagerBase.Singleton.Events.OnServerExitedRoom += RemovePlayer;
        }


        private void CreateGamePlayer(NetworkConnection conn)
        {
            if (conn.identity != null)
            {
                StartCoroutine(ReplacePlayer(conn, _gamePlayerPrefab));

                return;
            }

            CreatePlayer(conn, _gamePlayerPrefab);
        }

        private void CreateLobbyPlayer(NetworkConnection conn)
        {
            if (conn.identity == null) return;

            StartCoroutine(ReplacePlayer(conn, _lobbyPlayerPrefab));
        }

        private IEnumerator ReplacePlayer(NetworkConnection conn, GameObject prefab)
        {
            var oldPlayer = conn.identity.gameObject;
            
            var newPlayer = SpawnPlayer(conn, prefab);

            NetworkServer.ReplacePlayerForConnection(conn.identity.connectionToClient, newPlayer, true);

            yield return new WaitForEndOfFrame();
            
            if (newPlayer.TryGetComponent(out SimpleCharacterController controller))
            {
                controller.ID = conn.connectionId;
            }
            
            if(oldPlayer != null)
                NetworkServer.Destroy(oldPlayer);
        }

        private void CreatePlayer(NetworkConnection conn, GameObject prefab)
        {
            if (conn is not NetworkConnectionToClient connectionToClient) return;
            
            var player = SpawnPlayer(conn, prefab);

            NetworkServer.AddPlayerForConnection(connectionToClient, player);
            
            if (player.TryGetComponent(out SimpleCharacterController controller))
            {
                controller.ID = conn.connectionId;
            }
        }

        private void RemovePlayer(NetworkConnection conn)
        {
            if (conn is not NetworkConnectionToClient connectionToClient) return;

            var player = connectionToClient.identity.gameObject;
            
            NetworkServer.RemovePlayerForConnection(conn, false);
            
            NetworkServer.Destroy(player);
        }
        
        private GameObject SpawnPlayer(NetworkConnection conn, GameObject prefab)
        {
            var room = RoomManagerBase.Singleton.GetRoomOfPlayer(conn);

            var scene = room?.Scene ?? gameObject.scene;
            
            var player = NetworkSpawnUtilities.SpawnObjectForScene(scene, prefab, Vector3.zero, Quaternion.identity);

            return player;
        }
    }
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://reflective-roommanager.gitbook.io/docs/user-manual/general/deprecations.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
