How to sync a gameobject with common values

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

How to sync a gameobject with common values

berke96
2022-03-27 22:25:23

So i have a card game where at the start any client(or master) do the shuffling and store the order of the cards in an game object's field, and another object stores a field that which client has which cards, in arrays. I added photonview to each of these scene objects, which are basic scripts. I raised an event after one of the clients finish the shuffling and distributing. My problem is after that it is not synched, the data about the ownership of the cards are only in the master.

Code's simplified version is like this. Can you tell me what i am missing?

public class GameManager : MonoBehaviourPunCallbacks     
{    
    //...    
    private void Start()    
    {    
        //...    
        if(PhotonNetwork.IsMasterClient)    
            deal();    
        }    
    }    
    public void deal()    
    {    
         for (int i = 0; i < 14; i++)    
         {    
             for (int id = 0; id < 4; id++)    
             {    
                 Card.card cardVal = cardstack.draw();    
                 playerCard.playerDealCard(id, i, cardVal); //each player with 4 different id will have cards stored in an array inside this scene object player card    
             }    
         }    
         RaiseEventOptions raiseEventOptions = new RaiseEventOptions { Receivers = ReceiverGroup.All };    
         PhotonNetwork.RaiseEvent(1, null, raiseEventOptions, SendOptions.SendReliable);    
     }    
     public void renderCardsForActor(int id)    
     {    
               

      //Insantiating gameobjects with values from array, but array field that inside playerCard is null when it got here, for other than the master

. the player card scene object is not synced!    
     }    
}    

And event controller:

public class EventController : MonoBehaviour, IOnEventCallback    
{    
    private void OnEnable()    
    {    
        PhotonNetwork.AddCallbackTarget(this);    
    }    

    private void OnDisable()    
    {    
        PhotonNetwork.RemoveCallbackTarget(this);    
    }    

    public void OnEvent(EventData photonEvent)    
    {    
        GameManager gameManager =     
             GameObject.Find("GameManager").GetComponent<GameManager>();    
        gameManager.renderCardsForActor(PhotonNetwork.LocalPlayer.ActorNumber - 1);    
    }    
}    

Comments

Tobias
2022-03-28 14:57:51

First things first: You use RaiseEvent. Does the callback OnEvent get called, as you planned?

So far, you are sending event 1 with no content. So, how do you think the others know which cards they got?

Have a look at the Synchronization and State doc.

berke96
2022-03-28 15:32:50

Hi Tobias.

Yes I get the callback, but the CardsInHand object that I use is not up to date to the other clients.

The cards are stored in an empty object,

public class PlayerCard : MonoBehaviourPunCallbacks, IPunObservable    
{    
   public Card.card[][,] CardsInHand;    
   //...    

}    

It's a scene object with PhotonView attached to it.

So my modifications on this cardsInHand on master actor won't be synced with others? Should i send all the data with my RaiseEvent? I thought marking them as photonview is making them sync with all clients 🤔

Tobias
2022-03-29 15:00:28

To avoid misconceptions like this, I'd recommend the PUN Basics Tutorial. It shows you what you can do with a PhotonView and how it syncs values. I won't be able to explain the basics one by one here.

A few notes:

PUN does not have "Sync Vars", where you modify values locally and they are automatically synced. Objects with a PhotonView can implement OnPhotonSerialiezView to do that.

Card is not a serializable datatype for PUN. If you try to send it, there should be errors in the log telling you about this failing.

Back to top