- Home /
Photon multiplayer, getting information from photonView.owner
I'm attempting to sync weapons equipped between players, so you can see what everyone is holding, I'm really struggling, I've been trying to figure this out for days, this is my script for syncing and equipping weapons:
using UnityEngine;
using System.Collections;
public class InventoryManager : Photon.MonoBehaviour {
public GameObject holder;
public GameObject[] equippable;
GameObject player;
//string localequipped;
string equipped;
GameObject playerchanged;
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info) {}
void Update() {
if (Input.GetKeyDown(KeyCode.Alpha1)) Equip(0,photonView.owner);
if (Input.GetKeyDown(KeyCode.Alpha2)) Equip(1,photonView.owner);
}
void Equip(int wepid, PhotonPlayer sender) {
for (int i = 0; i < equippable.Length; i++)
{
equippable[i].SetActive(false);
}
equippable[wepid].SetActive(true);
equipped = equippable[wepid].name;
GetComponent<PhotonView>().RPC("EquipOther", PhotonTargets.Others, sender);
}
[PunRPC]
void EquipOther(PhotonPlayer sender) {
Debug.Log(sender + "is changing wep");
}
}
I've passed in the senders photon id/name and from there i want to get its gameobject so i can enable the weapon for all other players on the server, so that they can see the change.
but, i cant seem to find a way to get this to work, only possible way i thought would be to pass the game object through the rpc function, but photon does not support this, please help.
if my system of changing weapon or syncing it is wrong, dont fix it, please help/tell me a better or easier way to do it.
P.s im very new to networking and its hurting my brain.
Answer by SaurabhStudio · Mar 10, 2016 at 09:07 AM
Hello @timothy92 you can try this
void Equip(int wepid, PhotonPlayer sender) {
for (int i = 0; i < equippable.Length; i++)
{
equippable[i].SetActive(false);
}
equippable[wepid].SetActive(true);
equipped = equippable[wepid].name;
PhotonView photonView = PhotonView.Get(this);
photonView.RPC("EquipOther", PhotonTargets.Others,wepid );
}
[PunRPC]
void EquipOther(int wepid)
{
equippable[wepid].SetActive(true);
equipped = equippable[wepid].name;
}
Thank you for your reply, I had done this in a previous version of my script, but the problem is that this method of doing it sets that weapon equipped for both players, and the main problem is I don't have (or cant think of) a way to differ between players so I can say which player is changing there weapon and share that information with the other users.
Any ideas?
Thanks, $$anonymous$$thy.
This script is attached to Player or this is common between players? If it is common then you can give different name to your player. and can pass that name in RPC call. And you can check the name at receive call. if(gameObject.name == "$$anonymous$$yPlayer") { // your code }
Hmm, seems do-able, the script is common, how would I go about giving each player a unique .name, usernames perhaps?
I'll think of something, thanks for your help man :P really appreciate it.