Search Unity

  1. Check out the Unite LA keynote for updates on the Visual Effect Editor, the FPS Sample, ECS, Unity for Film and more! Watch it now!
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  4. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Dices over network.

Discussion in 'Connected Games' started by DreamingWorld, Jul 12, 2018.

  1. DreamingWorld

    DreamingWorld

    Joined:
    Dec 29, 2013
    Posts:
    6
    Hi everyone,
    I've got an big issues, and I spend days to find an answer but nothing works..
    So at the beginning of my game, I've got an small mini-game where everyone roll a dice to decide which player is the first.

    And I found no way to synchronise the number with every client! I tried, RPC, Sync Hook, everything I found on the internet..
    Here is my current not working script..

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.Networking;
    6.  
    7. public class RandomDice : NetworkBehaviour {
    8.     public Text Number;
    9.     //[SyncVar(hook = "TextOnServer")]
    10.     public string RndmNumber;
    11.  
    12.     public void RandomNumber(){
    13.         TextOnServer(Random.Range(1, 7).ToString());
    14.     }
    15.     private void Update()
    16.     {
    17.         Number.text = RndmNumber;
    18.     }
    19.  
    20.     void TextOnServer(string DiceNumber){
    21.         Debug.Log(DiceNumber);
    22.         RndmNumber = DiceNumber;
    23.         Number.text = RndmNumber;
    24.  
    25.     }
    26.  
    27. }
    It would be very kind of you to explain to me, show me exemples, to make it work.. thank you very much.
    :D
     
  2. Jos-Yule

    Jos-Yule

    Joined:
    Sep 17, 2012
    Posts:
    265
    Untested, unsupported, un-everything, but might help put you on the right track.

    Code (CSharp):
    1. public class RandomDice : UnityEngine.Networking.NetworkBehaviour {
    2.  
    3.     int rollsRequiredBeforeStarting = 4;
    4.     Dictionary<int, int> diceRolls = new Dictionary<int, int>();
    5.     int playerId = -1;
    6.     bool pickedFirstPlayer = false;
    7.  
    8.     public override void OnStartClient()
    9.     {
    10.         //playerId = GetMyPlayerId();
    11.         CmdMyDiceRoll(Random.Range(0, 6), playerId);
    12.     }
    13.  
    14.     void Update()
    15.     {
    16.         if(isServer && !pickedFirstPlayer)
    17.         {
    18.             if(diceRolls.Count >= rollsRequiredBeforeStarting)
    19.             {
    20.                 var firstPlayer = -1;
    21.                 var highestRoll = -1;
    22.  
    23.                 foreach(var item in diceRolls)
    24.                 {
    25.                     if(item.Value > highestRoll)
    26.                     {
    27.                         highestRoll = item.Value;
    28.                         firstPlayer = item.Key;
    29.                     }  
    30.                 }
    31.  
    32.                 RpcPlayerToGoFirst(firstPlayer);
    33.  
    34.                 pickedFirstPlayer = true;
    35.             }
    36.         }
    37.     }
    38.  
    39.     [Command]
    40.     void CmdMyDiceRoll(int roll, int playerId)
    41.     {
    42.         diceRolls[playerId] = roll;
    43.     }
    44.  
    45.     [ClientRpc]
    46.     void RpcPlayerToGoFirst(int playerId)
    47.     {
    48.         if(playerId == this.playerId)
    49.         {
    50.             // wee i get to go first!
    51.         }
    52.     }
    53. }
     
  3. Vegoo89

    Vegoo89

    Joined:
    May 11, 2018
    Posts:
    6
    Command / ClientRpc is by far simplest synchronise method as shown in Jos-Yule example.