Search Unity

Resolved How to propagate initial game data to all clients?

Discussion in 'Netcode for GameObjects' started by MidniteOil, Jan 27, 2023.

  1. MidniteOil

    MidniteOil

    Joined:
    Sep 25, 2019
    Posts:
    345
    Okay, I've made lots of single player games, both 2D and 3D and for my latest project I'm making my dream game. It's a multiplayer card game using Lobby and Relay.

    I've got my Lobby all setup and now I'm getting into the nitty-gritty netcode issues. I want the host to be the "source of truth" for all the cards. It's a realtime multiplayer version of Solitaire (i.e. Dutch Blitz or Nertz) so it's not turn-based.

    A big part of the strategy is seeing the cards other players have on their Trading Pile and Discard pile so you can tell if they're about to play something to the Clan Piles (i.e. the "foundations" in Solitaire) so basically, all the players need a real-time list of all the other player's cards.

    TL/DR
    So when the game starts, I want the server to initialize the cards for all the players. I've defined a PlayerCards class as follows:
    Code (CSharp):
    1. public class PlayerCards
    2. {
    3.     public string PlayerId { get; private set; }
    4.     public Suit Suit { get; private set; }
    5.     public DrawPile DrawPile { get; private set; }
    6.     public List<Card> DiscardPile { get; private set; }
    7.     public TradingPile[] TradingPiles { get; private set; }
    8.     public CastlePile CastlePile { get; private set; }
    9. ...
    10. }
    I am hydrating that in my GameManager's OnNetworkSpawn() method (for the host only) and I want to propagate it out to the clients.

    I'm wondering the best way to do that? Should I use a ClientRpc method and setup a bunch of serialization? Or is there a better way? Thanks!
     
  2. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    440
    Hi @MidniteOil , for synchronizing values you should use NetworkVariable and NetworkList (and similar networked structures). The documentation explains how to use them and how to deal with "complex" types you want to serialize.

    Does this help?
     
  3. MidniteOil

    MidniteOil

    Joined:
    Sep 25, 2019
    Posts:
    345
    Thanks, that's not really what I want in this case because I don't just want the data to be synchronized. I want the initial data state to be propagated to all the players but then I want to use events so each client can animate cards being dragged from one pile to another. I can't use network transforms either because it's players UI is from the local player's perspective.

    I think I'm just going to generate some primitive string representation of the cards/stacks that I can send in a ClientRPC and use to instantiate them on the client side.
     
  4. MidniteOil

    MidniteOil

    Joined:
    Sep 25, 2019
    Posts:
    345
    My solution mentioned above worked like a charm. I basically formed a delimited string representing the bare minimum data necessary to reconstitute the decks on each client and sent that string via a ClientRpc call. The clients just parse the string when they receive the message. For the rest of the game play simple ServerRpc and ClientRpc calls have worked wonderfully since the amount of data being synchronized is pretty light.
     
    Fawroe and RikuTheFuffs like this.