[PUN Classic] instantiating objects and syncing position one time

Options
Hi,

Recently trying out Photon and let me say it works fantastic however as learning goes there is always issues. I have read up a bit that you should try to avoid shoving a photonview on every object so I have tried to do so however I'm having quite a few issues getting each client to recognise the position of the object without using a photon transform view. I've tried numerous things and have not got very far but here's the code I've got at the moment. Hopefully i'm not totally of from the end result however any help would be much appreciated.
    void BuildSystem()
    {
        if (buildMode == true)
        {

            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            if (Input.GetMouseButtonDown(0))
            {
                if (Physics.Raycast(ray, out hit))
                {
                    photonView.RPC("BuildSystemNetworked", PhotonTargets.All);
                }
            }
        }
    }


    [PunRPC]
    void BuildSystemNetworked()
    {
        if (PhotonNetwork.isMasterClient)
        {
            Instantiate(blocks[0], hit.point, Quaternion.identity);
        }
    }
I mainly just one want transform.position and thats it. If i'm wrong and photonview is needed for every object i'll happily put it on however I like to make sure i'm doing the right things for the best performance.

Thanks,
Monsieur_Alpha

Comments

  • Just to add on I know there is no networking for positioning thats where i'm coming up a bit short. Just a point in the right direction or a slice of code is appreciated.

    Thanks
  • Hi @Monsieur_Alpha,

    I would recommend you taking a look at the RPCs and RaiseEvent documentation page. It shows you how to use the RPC function with optional parameters.

    In your case you would basically call photonView.RPC("BuildSystemNetworked", PhotonTargets.All, hit.point); which will call [PunRPC] void BuildSystemNetworked(Vector3 position) { } on all clients. Please note, that you only use types, that can get serialized by PUN by default. According to this example I have used a Vector3 (which can get serialized by PUN by default) instead of the RaycastHit.

    Please also note: using RPCs requires an attached PhotonView component. In the above mentioned documentation page is also a section about RaiseEvent and how it works. When you got the RPCs working, you can take an optional look at RaiseEvent and see, if you can and want to replace the RPCs. The advantage of RaiseEvent is, that you don't need an attached PhotonView component to raise custom events.
  • @Christian_Simon,

    Thank you very much for the reply. I read up that too many PhotonView is not the smartest move and instead you can use a master PhotonView to spawn objects e.g my blocks in a array. Did that mean that each object has to also have a PhotonView? I assume that might be using RaiseEvent.

    Anyway I'll be sure to look at the documentation and see if I can get this working.

    Thank you very much for your help