How to propagate initial game data to all clients?

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:

public class PlayerCards
{
    public string PlayerId { get; private set; }
    public Suit Suit { get; private set; }
    public DrawPile DrawPile { get; private set; }
    public List<Card> DiscardPile { get; private set; }
    public TradingPile[] TradingPiles { get; private set; }
    public CastlePile CastlePile { get; private set; }
...
}

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!

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?

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.

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.

2 Likes