Referencing object beloning to certain photonView

My players each have an inventory object. This object is instantiated locally in each player object across all clients (regardless of if the client owns the player or not). The inventory has a List<Equipment> equipmentList, which holds the gear that the player is currently carrying.

When I add a new item to the inventory, I do this (only called on the owning player):
		GameObject go;
		go = PhotonNetwork.Instantiate("Revolver38", transform.position, transform.rotation, 0);
		AddEquipment(go.GetComponent&lt;Revolver&gt;());
(Revolver inherits from the base class Equipment. Revolver38 is the prefab, which holds a Revolver script)

In my AddEquipment function, I simply add the equipment to the list equipmentList. This works fine in single player. My problem is that on all other clients, the equipment is NOT added to the equipmentList.

I'm looking for a way to synchronize the list over all clients. Since the gameObject is instantiated with Photon.Instantiate, the object (and its Revolver-script component, which I want to reference) already exists over all clients. I cannot figure out how to reference it on another client though. Can I send the newly instantiated equipment object's PhotonViewID (maybe in some Int-form?) with an RPC and have the receiving clients find the gameObjects from that, or something?

Any help is much appreciated!

Comments

  • For every gameobject spawned via PhotonNetwork.Instantiate you can use the following callback
    void OnPhotonInstantiate(PhotonMessageInfo info){ 
       Debug.Log("I was just spawned!"+gameObject.name);
    }
    

    One solution would be to use the callback event...check the attached photonView's owner and add the gameObject to that players equipment list.

    Or another way to do it would be to have the player who spawns his new equipment send an RPC with the PhotonViewID which should be added to it's list: A PhotonViewID can be send via an RPC, and for remote players to get the right PhotonView you can use PhotonView.Get(int photonviewID); (Maybe I should add a PhotonView.Get(PhotonViewID) ). Finally it's just "photonView.gameObject" to get the attached gameObject.
  • That is beautiful. Didn't know about the callback thing. Very useful. I think I'll try the second solution first. Thank you very much for your help, Leepo.
Sign In or Register to comment.