[SyncVar], [SyncVar(hook = "Function")]

Options
Hi all, I'm implementing Unity network project to Photon.
There is a script "Singleton" which has these variables that I can't understand. I can see them for what purpose they will use but I can't find any explanation or document about how them turn to Photon.
[SyncVar]
public int i;
[SyncVar(hook = "RoundTrumping")]
    public string RoundTrump;

public void RoundTrumping(string value)
    {
        RoundTrump = value;
    }

void Start ()
    {
        i = 0;
     }

 private void Update()
    {
 if (currentClubs != null && currentSpades != null && currentHearts != null && currentDiamonds != null)
        {
            if (RoundTrump == "Clubs")
            {
                currentClubs.GetComponent<SpriteRenderer>().enabled = true;
                currentSpades.GetComponent<SpriteRenderer>().enabled = false;
                currentHearts.GetComponent<SpriteRenderer>().enabled = false;
                currentDiamonds.GetComponent<SpriteRenderer>().enabled = false;
            }
            else if (RoundTrump == "Spades")
            {
                currentClubs.GetComponent<SpriteRenderer>().enabled = false;
                currentSpades.GetComponent<SpriteRenderer>().enabled = true;
                currentHearts.GetComponent<SpriteRenderer>().enabled = false;
                currentDiamonds.GetComponent<SpriteRenderer>().enabled = false;
            }
            else if (RoundTrump == "Hearts")
            {
                currentClubs.GetComponent<SpriteRenderer>().enabled = false;
                currentSpades.GetComponent<SpriteRenderer>().enabled = false;
                currentHearts.GetComponent<SpriteRenderer>().enabled = true;
                currentDiamonds.GetComponent<SpriteRenderer>().enabled = false;
            }
            else if (RoundTrump == "Diamonds")
            {
                currentClubs.GetComponent<SpriteRenderer>().enabled = false;
                currentSpades.GetComponent<SpriteRenderer>().enabled = false;
                currentHearts.GetComponent<SpriteRenderer>().enabled = false;
                currentDiamonds.GetComponent<SpriteRenderer>().enabled = true;
            }
            else
            {
                currentClubs.GetComponent<SpriteRenderer>().enabled = false;
                currentSpades.GetComponent<SpriteRenderer>().enabled = false;
                currentHearts.GetComponent<SpriteRenderer>().enabled = false;
                currentDiamonds.GetComponent<SpriteRenderer>().enabled = false;
            }
        }
}

Comments

  • Hi @NomadicWarrior,

    Photon doesn't have a one-to-one replacement for SyncVar. Instead you have to synchronize this value in another way, for example with a RPC or RaiseEvent call or within the OnPhotonSerializeView function.
  • Hi @NomadicWarrior,

    Photon doesn't have a one-to-one replacement for SyncVar. Instead you have to synchronize this value in another way, for example with a RPC or RaiseEvent call or within the OnPhotonSerializeView function.

    Thank you Christiam_Simon. I'll try to use RPC and OnPhotonSerializeView. Looks like it's not going to simple for me. There are too much int = i and SyncVar(hook)
  • > @Christian_Simon said:
    > Hi @NomadicWarrior,
    >
    > Photon doesn't have a one-to-one replacement for SyncVar. Instead you have to synchronize this value in another way, for example with a RPC or RaiseEvent call or within the OnPhotonSerializeView function.

    Hi may I have some code example just to ma
    > @Christian_Simon said:
    > Hi @NomadicWarrior,
    >
    > Photon doesn't have a one-to-one replacement for SyncVar. Instead you have to synchronize this value in another way, for example with a RPC or RaiseEvent call or within the OnPhotonSerializeView function.

    if I want to call PunRPC, do I need call these:

    [SyncVar(hook = "RoundTrumping")]
    public string RoundTrump;

    public void RoundTrumping(string value)
    {
    RoundTrump = value;
    }

    in Update?
  • In order to use RPCs or OnPhotonSerializeView the object must have an attached PhotonView component. If it has this component attached, you can use the RPC function from this component. To see how this works I recommend you taking a look at the RPCs and RaiseEvent documentation page. If it doesn't have this component attached RaiseEvent should be the way to go. This is also described in the previously mentioned documentation page.

    Calling RPCs or RaiseEvent every time when Unity's Udpate function runs, will generate a lot of messages which should be avoided. Instead you should just call RPCs or RaiseEvent whenever a certain value has changed.
  • In order to use RPCs or OnPhotonSerializeView the object must have an attached PhotonView component. If it has this component attached, you can use the RPC function from this component. To see how this works I recommend you taking a look at the RPCs and RaiseEvent documentation page. If it doesn't have this component attached RaiseEvent should be the way to go. This is also described in the previously mentioned documentation page.

    Calling RPCs or RaiseEvent every time when Unity's Udpate function runs, will generate a lot of messages which should be avoided. Instead you should just call RPCs or RaiseEvent whenever a certain value has changed.


    wow thank you for tip.
    Like this right?
    void Update ()
    {
    if(something == true)
    {
    if(PhotonNetwork.isMasterClient)
    {
    RPC("RpcFunction", PhotonTargets.All);
    }
    }
    }
    
    [PunRPC]
    void RpcFunction ()
    {
    // do my stuff here?
    }
    
  • Yes, looks good so far.
  • Hi @NomadicWarrior,

    Photon doesn't have a one-to-one replacement for SyncVar. Instead you have to synchronize this value in another way, for example with a RPC or RaiseEvent call or within the OnPhotonSerializeView function.

    IT DOES NOW FAM!