How to sync a gameobject with common values
The whole answer can be found below.
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).
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
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.
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 🤔
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