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

Don't understand why it is not executed correctly from client

Discussion in 'Multiplayer' started by pKallv, Aug 7, 2016.

  1. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,129
    I am testing UNET by trying to shuffle cards. It all works correctly when executing from the host-client but not from the "remote" client.

    Here is the relevant code:

    Code (CSharp):
    1. [SyncVar(hook = "ProcessShuffleDeck")] public bool MP_ShuffleOrange = false;
    Code (CSharp):
    1. public void Btn_ShuffleDeck() {
    2.         if (!isLocalPlayer)
    3.             return;
    4.  
    5.  
    6.         foreach (string cardTag in MP_Singleton.Instance.master_Cards_List) {
    7.             GameObject dummy = GameObject.Find (cardTag);
    8.             Cmd_LocalAuthority (true, dummy);
    9.         }
    10.  
    11.  
    12.         Cmd_ProcessShuffleDeck ();
    13.  
    14.     }
    15.  
    16.     void ProcessShuffleDeck(bool _MP_Orange) {
    17.    
    18.         if (_MP_Orange) {
    19.             GameObject.Find ("btn_MenuShuffle").GetComponent<Button> ().image.color = orangeButtonColor;
    20.         } else if (!_MP_Orange) {
    21.             GameObject.Find ("btn_MenuShuffle").GetComponent<Button> ().image.color = yellowButtonColor;
    22.  
    23.  
    24.             DealTheDeckOfCards ("LAST POSITION");
    25.  
    26.         }
    27.     }
    28.  
    29.     [Command]
    30.     void Cmd_ProcessShuffleDeck() {
    31.         if (MP_ShuffleOrange)
    32.             MP_MainMenu = !MP_MainMenu;
    33.    
    34.         MP_ShuffleOrange = !MP_ShuffleOrange;
    35.     }
    Code (CSharp):
    1. public void DealTheDeckOfCards (string theMode) {
    2.  
    3.         //        if (!isLocalPlayer)
    4.         //            return;
    5.  
    6.  
    7.         int sortOrder = 0;
    8.         int zDepthCounter = 0;
    9.         float randomX = 0;
    10.         float randomY = 0;
    11.         float deckStartPosX = 0.0f;
    12.         float deckStartPosY = 0.0f;
    13.         float twistAngle = 0.0f;
    14.  
    15.         if (theMode == "LAST POSITION") { // Last known position
    16.             deckStartPosX = PlayerPrefs.GetFloat ("full_Deck_Last_X_Position");
    17.             deckStartPosY = PlayerPrefs.GetFloat ("full_Deck_Last_Y_Position");
    18.             twistAngle = PlayerPrefs.GetFloat ("full_Deck_Last_localEulerAngles");
    19.             deckStartPosX = 2f; // << Set the position for testing
    20.             deckStartPosY = 2f; // << Set the position for testing
    21.             print ("LastPosition: " + deckStartPosX);
    22.         }
    23.  
    24.         // SHUFFLE THE CARD DECK
    25.         MP_Singleton.Instance.master_Cards_List = MP_Fisher_Yates_CardDeck_Shuffle (MP_Singleton.Instance.master_Cards_List);
    26.  
    27.         // REPLACE THE CARDS ON THE GAME BOARD
    28.         tempCard_List = new List<string> (MP_Singleton.Instance.master_Cards_List);
    29.         /*
    30.         foreach (GameObject aGO in tempGameObject_List) {
    31.  
    32.             temp_GameObject = GameObject.FindWithTag(aGO.tag);
    33.  
    34.             if (temp_GameObject.tag.LastIndexOf ("B") != -1) {
    35.  
    36.             } else {
    37.  
    38.                 GameEngine_Script _gameEngine_Script = GameObject.FindObjectOfType (typeof(GameEngine_Script)) as GameEngine_Script;
    39.  
    40.                 temp_GameObject = _gameEngine_Script.FlipCardAndUpdatedMasterList(temp_GameObject);
    41.             }
    42. */
    43.  
    44.  
    45.         foreach (string cardTag in MP_Singleton.Instance.master_Cards_List) {
    46.  
    47.             temp_GameObject = GameObject.FindWithTag(cardTag);
    48.             temp_GameObject.GetComponent<Renderer>().sortingOrder = sortOrder;
    49.  
    50.             randomX = UnityEngine.Random.Range (-0.055f, 0.055f);
    51.             randomY = UnityEngine.Random.Range (-0.055f, 0.055f);
    52.             randomX = randomX + deckStartPosX;
    53.             randomY = randomY + deckStartPosY;
    54.  
    55.             temp_GameObject.transform.position = new Vector3 (randomX, randomY, zDepthCounter);
    56.             temp_GameObject.transform.localEulerAngles = new Vector3(0f, 0f, twistAngle);
    57.  
    58.             // Add to depth
    59.             zDepthCounter++;
    60.             sortOrder++;
    61.  
    62.         }
    63.  
    64.     }
    65.  
    66.     public static List<string> MP_Fisher_Yates_CardDeck_Shuffle (List<string>aList) {
    67.  
    68.         System.Random _random = new System.Random ();
    69.  
    70.         string str_myGO;
    71.  
    72.         int n = aList.Count;
    73.         for (int i = 0; i < n; i++)
    74.         {
    75.             // NextDouble returns a random number between 0 and 1.
    76.             // ... It is equivalent to Math.random() in Java.
    77.             int r = i + (int)(_random.NextDouble() * (n - i));
    78.             str_myGO = aList[r];
    79.             aList[r] = aList[i];
    80.             aList[i] = str_myGO;
    81.         }
    82.  
    83.         return aList;
    84.     }
    Here is what I do:

    1. Game starts and i call the "DealTheDeckOfCards" from " OnStartServer", with 0,0 position. and it deals the deck on the 0,0 pos on the scene. Here is the problem.
    2. Then, when i execute "Btn_ShuffleDeck()" from the "remote" client the deck is not positioned correctly.
    o I then use "deckStartPosX = 2f;" and "deckStartPosY = 2f;", which should position the
    deck on 2,2 pos, the print indicate the same
    o It position the deck still on 2,2
    o However if i first position the deck from the host-client it is positioned @ 2,2 and then
    if i execute #2 it stay at 2,2 pos

    I have a sync-pos script on all cards.​

    My problem is that i am not able to figure out what i am doing wrong here to be able to execute the 2,2 position when i initiate "Btn_ShuffleDeck()" from the remote-client.

    Anyone nice that can help and give some insight?
     
    Last edited: Aug 7, 2016
  2. Mytts

    Mytts

    Joined:
    Jun 10, 2015
    Posts:
    10
    I may be very bad at this, but I'm not entirely sure I understand the problem. The lines:

    o I then use "deckStartPosX = 2f;" and "deckStartPosY = 2f;", which should position the
    deck on 2,2 pos, the print indicate the same
    o It position the deck still on 2,2

    Seem to indicate to me that the code is working as intended?
     
  3. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,129
    Agree looks a bit confusing but what I mean is that it is only placed at 2,2, when pushing the button, if it is there already. If it is positioned at 0,0 and I push the button from client the print shows 2,2 but the prefab does not move to 2,2.