Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Network Lobby - How to sync ready flags?

Discussion in 'Multiplayer' started by Stratos-Mak, Jun 25, 2015.

  1. Stratos-Mak

    Stratos-Mak

    Joined:
    Aug 14, 2014
    Posts:
    11
    I can't seem to get the ready flags sycned across the players.

    I have a NetworkLobbyManager, and create a multiplayer matchmaking game, using either my own UI implementation or the manager hud. I can see on both that the client is connecting successfully, and the client's lobby player is spawned on the host in the editor.

    Afterwards, I use a UI toggle to change the ready variable of the local lobby player. That's where my problem starts.

    On the client's side, when I change the ready bool, the client's manager hud shows me that he is ready, but the host's manager hud still shows him as not ready. I thought that the syncing is done automatically by the NetworkLobbyManager.

    Then, searching the API through coding (cause there isn't much documentation yet), I tried calling SendReadyToBeginMessage when I change the ready flag, registering the MsgType.Ready and MsgType.ReadyToBegin messages on the server with functions, but nothing was called.

    Am I missing something and the ready variables are not synced?

    (I could aproach this by using just messages, but I want to use the lobby manager)
     
  2. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    yes, use SendReadyToBeginMessage . dont register handlers for those messages, there are already handlers.
     
    Guhanesh likes this.
  3. Indiefreaks

    Indiefreaks

    Joined:
    Nov 12, 2012
    Posts:
    89
    In order to set your players as ready, you just have to call the SendReadyToBeginMessage(). It all gets done by UNET HLAPI afterwards.
    You shouldn't catch the MsgType.Ready or MsgType.ReadyToBegin messages to get it working.
     
  4. Stratos-Mak

    Stratos-Mak

    Joined:
    Aug 14, 2014
    Posts:
    11
    I still can't get it to work... I now just call SendReadyToBeginMessage() but nothing happens on the other side as usual.
    Didn't mention that when the host starts or a client connects, it gives me this error:

    Maybe this is causing it?
     
  5. Stratos-Mak

    Stratos-Mak

    Joined:
    Aug 14, 2014
    Posts:
    11
    God damn it I found it. I was calling SendReadyToBeginMessage() outside of the NetworkLobbyPlayer script and the server wasn't accepting it.
     
  6. hengineer

    hengineer

    Joined:
    Mar 18, 2013
    Posts:
    13
    I'm having a similar problem just using the base NetworkLobbyManager.
    If Player 1 hosts and marks themself Ready and then Player 2 joins they will NOT see Player 1 as ready.
    If Player 1 then toggles their state the changes are synced properly but the initial state is not synced at all. This seems pretty basic. Am I missing something?
     
    MaxFrax96 likes this.
  7. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,507
    On my "Lobby Player Object" I make it so only the host player has the ability to start the game using this message. I setup a custom callback that gets passed to the UI start game button at runtime, this button is only enabled for the host. The other players must wait for the host to start the match. I suppose you can extend this for other players and add more callbacks to be assigned for different functions if you want to set a ready status for each player and check them all. you can also add more logic to check the player count if you want the room to be full before the game starts.

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using UnityEngine.EventSystems;
    5. using System.Collections;
    6. using UnityEngine.Networking;
    7.  
    8. public class RG_NetworkLobbyPlayer : MonoBehaviour {
    9.  
    10.     public byte slot;
    11.     public Button.ButtonClickedEvent buttonCallback;
    12.     public NetworkIdentity netId;
    13.     RG_NetworkManagerHUD netManagerHUD;
    14.  
    15.     // Use this for initialization
    16.     void Start () {
    17.         netManagerHUD = GameObject.Find ("LobbyManager").GetComponent<RG_NetworkManagerHUD> ();
    18.         netId = GetComponent<NetworkIdentity> ();
    19.         slot = GetComponent<NetworkLobbyPlayer> ().slot;
    20.  
    21.         if(slot == 0 && netId.isLocalPlayer){
    22.             //This player is host
    23.             GameObject.Find("LobbyManager").GetComponent<RG_NetworkManagerHUD>().EnableStartLobbyGameButton();
    24.             GameObject.Find ("StartLobbyGameButton").GetComponent<Button> ().onClick = buttonCallback;
    25.         }
    26.     }
    27.    
    28.     public void EnableLoadingImage(){
    29.         netManagerHUD.lobbyHUDReference.loadingImage.SetActive (true);
    30.         netManagerHUD.loadingMultiplayerScene = true;
    31.         Invoke ("SetStartFlag", 0.1f);
    32.     }
    33.  
    34.     public void SetStartFlag(){
    35.         GetComponent<NetworkLobbyPlayer> ().SendReadyToBeginMessage ();
    36.     }
    37. }
    38.