Search Unity

One Code Two Measure

Discussion in 'Netcode for GameObjects' started by VKyuzel, Jan 11, 2023.

  1. VKyuzel

    VKyuzel

    Joined:
    Mar 6, 2022
    Posts:
    21
    Hello guys!

    I created a board game with Netcode and Relay to connect two different player.
    I spawn and re-parent a new object in this way:

    Code (CSharp):
    1.  
    2.     [ServerRpc(RequireOwnership = false)]
    3.     public void LoadCardsServerRpc(string panelName, string tagName)
    4.     {
    5.  
    6.         GameObject cardToSpawn = Resources.Load("PrefabToLoad\\Cards\\Dog", typeof(GameObject)) as GameObject;
    7.         GameObject serverHand = GameObject.Find($"CoreGame/CanvasHandPlayer/{panelName}");
    8.         NetworkObject cardToSpawnNetwork = Instantiate(cardToSpawn.GetComponent<NetworkObject>(), serverHand.transform);
    9.         cardToSpawnNetwork.GetComponent<NetworkObject>().tag = tagName;
    10.         serverHand.GetComponent<DeckLoad>().LoadCards();
    11.     }
    I call the method by server and by client without problem, dropping the cards (Network GameObjects) instanciated by the method above.

    This is the hierarchy: upload_2023-1-11_11-52-34.png
    and every prefab is instanciated in the NetworkManager.
    This is the gameboard:
    upload_2023-1-11_11-53-41.png
    when I press STARTGAME (I'm the server now), I call the method above and I generate a GO but it is not sync with the client, because the client can't see it.
    upload_2023-1-11_11-55-16.png
    Image above: I pressed start game and the server instanciated the card.
    The next Image I show you what the client see:
    upload_2023-1-11_11-57-4.png
    as you can see, the client see that is spawned a card (I dragged it from the server the card) and both players can see the dog card. But, using the same code, I didn't see the new spawned card in the right panel.
    Hierarchy of the right panel:
    upload_2023-1-11_11-59-1.png
    I tried with and without NetworkObject in PanelRightPlayer.

    So I would ask: why the client see the card spawned in the grid and not in the hand?

    Unity version: 2021.3.4f1
    My packages in next message
     
  2. VKyuzel

    VKyuzel

    Joined:
    Mar 6, 2022
    Posts:
    21
    Here my packages upload_2023-1-11_12-3-10.png
     
  3. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    440
    Hi @VKyuzel ! I'd recommend that you change your approach and do not use NetworkObjects for your cards, as these are a lot and don't really need to be synced at all.

    All you need to do is synchronize the state of the board, and make each client visualize/animate it.

    If you architect your game in this way, you'll have way less headaches when it comes to move cards around, animate them and so on. Bonus point: you'll consume way less bandwidth and save $$$.
     
  4. VKyuzel

    VKyuzel

    Joined:
    Mar 6, 2022
    Posts:
    21
    Thank you so much Riku for your suggestion! But how can I show the cards to the players if they are not NetworkObjects and sync them to show them for both client and server? (for example, the cards can change their "weight" number) I just need to remove the NetworkObjects script to them?
     
  5. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    440
    You can have one NetworkObject that is the "board". This object has no visual components and holds data about all the cards (I.E: if they're visible, their "weight" number, the ID of the image to display).
    Think of it as the "backend" of the board.

    You then have a boardUI which is a separate, non-networked object, that helps clients visualize the board and acts as the frontend.

    Whenever the status of the board changes, the clients receive the updated data and update the boardUI to match the data.
    The result is that you only sync one object, and clients reconstruct the appeareance of the board based on the data of that object.

    Yes, and instead you create a single "board" NetworkObject as I mentioned above
     
  6. VKyuzel

    VKyuzel

    Joined:
    Mar 6, 2022
    Posts:
    21
    Should I remove all the cards from NetworkManager? upload_2023-1-11_14-12-6.png
    And than, add them on runtime inside the NetworkManager only the cards I will play?
     
  7. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    440
    Yes

    No. Your cards don't need to be networked at all. All you need to do is send IDs of the cards that a client has played, retrieve the associated card from a local list, and create an instance of that card on each client that needs to see it
     
    VKyuzel likes this.