I am working on a 4 player online multiplayer card game. I am using Netcode for GameObjects to perform network operations without prefabs.
In the lobby manager scene, i wait for all player to join and once all 4 have joined the host can start the game. Once started, the I update the lobby with the flag that the game has started and all the clients also move to the Game scene. I have attached NetworkObject
to my GameManager
object. In the Start
method for the GameManager
, I check if i am the host, i start as host or else as client. I can see that the clients are getting connected and stay on connected state. I have some NetworkVariables such as currentPlayerIndex
, etc. Which I need to be sync on all clients.
I create a single build on my mac and copy that to create 4 instances. I run one of the builds as a host and others as clients.
I have added OnValueChanged
handler in the code but, the value on reflects on host but not on any client. To verify if the OnValueChanged
method is called on client i have also added some logs, even which are not displayed.
There is some commented code as i first tried with relay, and when that was not working i went with local setup. The local setup is still not working.
GameManager:
void Start()
{
if (commonSingleton.isHost)
{
CreateRelay();
}
else
{
joinCode = commonSingleton.lobby.Data["relayCode"].Value;
JoinRelay();
}
}
void CreateRelay()
{
try
{
//allocation = await RelayService.Instance.CreateAllocationAsync(numberOfPlayers);
//joinCode = await RelayService.Instance.GetJoinCodeAsync(allocation.AllocationId);
Debug.Log("=>>>>>>>>>>>Join Code :" + joinCode);
//var relayServerData = new RelayServerData(allocation, "dtls");
//NetworkManager.Singleton.GetComponent<UnityTransport>().SetRelayServerData(relayServerData);
NetworkManager.Singleton.StartHost();
Debug.Log("Host initialized. Waiting for clients...");
UpdateLobbyWithRelayCode();
}
catch (Exception e)
{
Debug.Log("Relay connection failed");
}
}
void JoinRelay()
{
try
{
//Debug.Log("=>>>>>>>>>>>Joining with Code :" + joinCode);
//JoinAllocation joinAllocation = await RelayService.Instance.JoinAllocationAsync(joinCode);
//Debug.Log("=>>>>>>>>>>>Joined with code :" + joinCode);
//var relayServerData = new RelayServerData(joinAllocation, "dtls");
//NetworkManager.Singleton.GetComponent<UnityTransport>().SetRelayServerData(relayServerData);
NetworkManager.Singleton.StartClient();
}
catch (Exception e)
{
Debug.LogError("Relay connection failed : " + e.Message);
}
}
public override void OnNetworkSpawn()
{
deckManager = GetComponent<DeckManager>();
commonSingleton = CommonSingleton.Singleton();
teamDetailsManager = GetComponent<TeamDetailsManager>();
numberOfPlayers = commonSingleton.numberOfPlayers;
if (IsServer)
{
NetworkManager.Singleton.OnClientConnectedCallback += HandleOnClientConnected;
NetworkManager.Singleton.OnClientDisconnectCallback += HandleOnClientDisconnected;
//foreach (NetworkClient client in NetworkManager.Singleton.ConnectedClientsList)
//{
// HandleOnClientConnected(client.ClientId);
//}
}
if (IsClient) {
currentClientId.OnValueChanged += ClientIdChanged;
}
}
void HandleOnClientConnected(ulong clientId)
{
players.Add(new PlayerData(clientId, players.Count, 0, players.Count % 2));
Debug.Log("Client connected with id:" + clientId);
// todo add player team index
if (players.Count == numberOfPlayers)
{
StartGameRpc();
//UpdateLobbyAsLocked();
}
}