Help with Sync a Deck of Cards

Good day. I am trying to sync a deck of cards over the network. The deck is made up of 52 playing cards on which i have photonViews on the card object and also on the Deck object.

I want to use the master client to Instantiate the deck, intialize it, shuffle it and kick the top card of the deck then send this shuffled deck to each client accross the network. I am able to instantiate it on each client but each one has it shuffled differently so when The kick is done each click has a different kick card.

I am using RaiseEvent to spawn the deck and an RPC to try to Sync it.

SpawnDeck Method

public void SpawnDeck()
{
gameDeck = Instantiate(deckPrefab, Vector3.zero, Quaternion.identity);
gameDeck.transform.SetParent(gameDeckUI.transform, false);
gameDeck.GetComponent().InitializeDeck();
gameDeck.GetComponent().Shuffle();
gameDeck.name = "Deck";
Deck deckScript = gameDeck.GetComponent();
GameObject deckGO = GameObject.Find("Deck");

UpdateDeckUI(deckScript, deckGO);

PhotonView photonView = gameDeck.GetComponent();

if (PhotonNetwork.AllocateViewID(photonView))
{
object[] data = new object[]
{
gameDeck.transform.position, gameDeck.transform.rotation, photonView.ViewID
};

RaiseEventOptions raiseEventOptions = new RaiseEventOptions()
{
Receivers = ReceiverGroup.All,
CachingOption = EventCaching.AddToRoomCache
};

SendOptions sendOptions = new SendOptions
{
Reliability = true
};

PhotonNetwork.RaiseEvent(CustomManualInstantiateEventCode, data, raiseEventOptions, sendOptions);
}
else
{
print("Failed to allocate a ViewID");
Destroy(gameDeck);
}
}


OnEvent CallBack

public void OnEvent(EventData photonEvent)
{
if(photonEvent.Code == CustomManualInstantiateEventCode)
{
object[] data = (object[])photonEvent.CustomData;

GameObject GD = (GameObject)Instantiate(deckPrefab, (Vector3)data[0], (Quaternion)data[1]);
PhotonView photonView = GD.GetComponent();

photonView.ViewID = (int)data[2];

}
}

My StartMethod

private void Start()
{
PV = GetComponent();
Photon.Realtime.Player[] players = PhotonNetwork.PlayerList;
if(players != null && players.Length == 2)
{
if (players[0].IsLocal)
{
LocalPlayerName.text = players[0].NickName;
RemotePlayerName.text = players[1].NickName;
}
else
{
LocalPlayerName.text = players[1].NickName;
RemotePlayerName.text = players[0].NickName;
}
}

SpawnDeck();

PV.RPC("RPC_Kick", RpcTarget.All);

}


My RPC_Kick Method

[PunRPC]
private void RPC_Kick()
{

kickCard = gameDeck.GetComponent().Kick();
kickCard.transform.SetParent(kickCardsGO.transform, false);
kickCard.transform.position = new Vector3(-5.7f, -1.6f, 0);
kickCard.GetComponent().ToggleFace(true);

}


Can someone help me with syncing the deck across the network?

Comments

  • Hi @Punisher,

    is it necessary that each client knows the shuffled deck? You can also use the MasterClient to deal cards to the other players. In this case only the MasterClient has to know the shuffled deck. Dealing cards to other players can be for example done by using the RaiseEvent function. This would also have the benefit, that the deck doesn't run out of synchronization because only the MasterClient manages it.

    If you still want to have the deck synchronized on all clients, you should think about giving the cards unique identifier, for example unique IDs from 0 to 51 or similar. You can "shuffle" these numbers locally on the MasterClient and add them to an array. These array can be sent to other clients by using the RaiseEvent function.

    One other thing: right now each client calls the above shown Start function and furthermore each client calls the SpawnDeck function as far as I can see. This results in each client allocating a ViewId for the PhotonView component and raising an event. It is enough if only the MasterClient does that.
Sign In or Register to comment.