How can I PhotonNetwork.Instantiate and add Preferences?

Options
Hello. i currently have an inventory system placed in my game. i am currently trying to sync the dropped items so when they are picked up the information is given to the client that is trying to pick up the item to then be displayed in the inventory. but i am running into an issue. I am instantiating the object without any of the item values. i am creating the object then adding the values. so at the moment the client that drops the item can pick up the item with the values and the other client cant.

This is my code :
void dropItem(Item item){

		Vector3 playerPos = this.transform.position;
		Vector3 playerDirection = this.transform.forward;
		Quaternion playerRotation = this.transform.rotation;
		float spawnDistance = 1;
		
		Vector3 spawnPos = playerPos + playerDirection*spawnDistance;
		string itemname = item.itemName;


		GameObject itemAsGameObject = (GameObject)PhotonNetwork.Instantiate ("DroppedItem", spawnPos, playerRotation, 0);
		itemAsGameObject.GetComponent<DroppedItem> ().item = item;

how can I drop the item and have the preferences synced to be picked up?

Comments

  • Tobias
    Options
    PhotonNetwork.Instantiate has a final parameter for instantiation values. You can use that to set the properties of the item.
    When the item is recreated on the other clients, they also have access to those values via the PhotonView.instantiationData.

    A script on your prefabs can implement OnPhotonInstantiate(PhotonMessageInfo info). This is then called when the object got instantiated. This should be a good moment to apply the instantiationData.
  • @Tobias Hello thanks for replying. the item data that i am storing is in a form of an Enum. and it is expecting an object[]. Is there anyway to correct this?
  • Tobias
    Options
    You can cast the enum to it's base type (int or byte) and send that. PUN does not automatically cast the values back to the enum you use but you can do that easily.
    If you have a single value, you can still put it in a new object[] as first / only element.