Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question (Mirror Network) Card Game: Drawing/Dropping Issue

Discussion in 'Multiplayer' started by Forgotten-Memory, Feb 28, 2024.

?

How make a Mirror function that allows the drawing of cards for 2 players

Poll closed Mar 6, 2024.
  1. Depends on what you card prefab and code look like

    0 vote(s)
    0.0%
  2. There is a way to resolve the given code given more information

    0 vote(s)
    0.0%
Multiple votes are allowed.
  1. Forgotten-Memory

    Forgotten-Memory

    Joined:
    Nov 1, 2022
    Posts:
    1
    Hello,
    I'm looking for some help on a card game, me and a few other classmates are working on. We are developing our project through the use of Mirror, but we are having trouble drawing cards (also giving both host and client authority) work for both players respectively. The code below is the one my teammate originally made before we fully began integrating Mirror. If anyone can give some guidance it would be appreciated.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerDeck : MonoBehaviour
    6. {
    7.    
    8.     public int x;
    9.     public static int deckSize;
    10.     public List<Card> deck = new List<Card>();
    11.     public static List<Card> staticDeck = new List<Card>();
    12.  
    13.     public GameObject cardInDeck1;
    14.     public GameObject cardInDeck2;
    15.     public GameObject cardInDeck3;
    16.     public GameObject cardInDeck4;
    17.  
    18.     public GameObject CardToHand;
    19.     public GameObject[] Clones;
    20.     public GameObject Hand;
    21.     // Start is called before the first frame update
    22.     void Start()
    23.     {
    24.         x = 0;
    25.         deckSize = 10;
    26.  
    27.         for(int i = 0; i < 10; i++){
    28.             //x = Random.Range(1, 10);
    29.             deck[i] = CardDatabase.cardList[i];
    30.         }
    31.  
    32.         StartCoroutine(StartGame());
    33.     }
    34.  
    35.     // Update is called once per frame
    36.     void Update()
    37.     {
    38.  
    39.         staticDeck = deck;
    40.  
    41.         if (deckSize < 8)
    42.         {
    43.             cardInDeck1.SetActive(false);
    44.         }
    45.         if (deckSize < 6)
    46.         {
    47.             cardInDeck2.SetActive(false);
    48.         }
    49.         if (deckSize < 4)
    50.         {
    51.             cardInDeck3.SetActive(false);
    52.         }
    53.         if (deckSize < 1)
    54.         {
    55.             cardInDeck4.SetActive(false);
    56.         }
    57.  
    58.         if(TurnSystem.startTurn == true)
    59.         {
    60.             StartCoroutine(Draw(1));
    61.             TurnSystem.startTurn = false;
    62.         }
    63.     }
    64.  
    65.     IEnumerator StartGame()
    66.     {
    67.         for(int i = 0; i < 3; i++)
    68.         {
    69.             yield return new WaitForSeconds(1);
    70.             //NEW
    71.             Instantiate(CardToHand, transform.position, transform.rotation);
    72.         }
    73.        
    74.     }
    75.  
    76.  
    77.     IEnumerator Draw(int x)
    78.     {
    79.         for(int i = 0; i < x; i++)
    80.         {
    81.             yield return new WaitForSeconds(1);
    82.             Instantiate(CardToHand, transform.position, transform.rotation);
    83.         }
    84.     }
    85.  
    86. }
     
  2. robert1997b

    robert1997b

    Joined:
    May 6, 2024
    Posts:
    2
    When a player draws a card, you need to assign authority of that card to the player. This can be done using AssignClientAuthority method on the NetworkIdentity of the card object. This way, the player who draws the card has control over it. When drawing cards, you should instantiate them on the server and then spawn them for all clients using NetworkServer.Spawn. If you want a specific client to have authority over the spawned card, you can pass the client’s connection as a parameter to the Spawn method. Make sure that any changes to the card’s state are synchronized across all clients. This includes the card’s position, ownership, and any other relevant state. You can use SyncVars or Command methods to communicate these changes from the client to the server. ADPWorkforceNow


    [Command]
    void CmdDrawCard()
    {
    GameObject card = Instantiate(CardToHand, transform.position, transform.rotation);
    NetworkServer.Spawn(card, connectionToClient); // Spawn the card for all clients with authority given to the drawing player
    RpcShowCard(card);
    }

    [ClientRpc]
    void RpcShowCard(GameObject card)
    {
    // This is called on all clients
    // You can add code here to visually show the card in the player's hand
    }
     
    Last edited: May 9, 2024