- Home /
Photon Networking: how to take over ownership?
Hi, I'm using Photon to instantiate 2 world objects. Since I don't want duplicates, I made sure that only the first client to join the room will trigger the spawn by:
if(PhotonNetwork.isMasterClient)
{
GameObject cad = PhotonNetwork.Instantiate("CAD_Photon", Vector3.zero, Quaternion.identity, 0);
GameObject cad2 = PhotonNetwork.Instantiate("CAD_Photon2", Vector3.zero, Quaternion.identity, 0);
}
So that works fine. I used the "isMasterClient" condition and not "InstantiateSceneObject" directly, since I understand that this makes the MasterClient the permanent owner of that object.
Now I can manipulate the object on the host's side and these manipulations will be synced to the client. On the client side, I can also manipulate the objects, but these changes are not synced to the host.
From reading and researching I figured out, I need to be able to switch ownership to the client so his changes will be synced to the host. But I just couldn't find any proper instructions on how to actually do it in a dynamic way. The "Owner" setting of the "PhotonView" components are set to "Takeover", but I guess I will have to use some kind of dynamic method on my object grabbing script that automatically changes ownership to the one who is grabbing the object.
Here is my script for simple object manipulation (grabbing it for reposition and rotation):
public class ObjectController : MonoBehaviour
{
private Valve.VR.EVRButtonId triggerButton = Valve.VR.EVRButtonId.k_EButton_SteamVR_Trigger;
private SteamVR_Controller.Device controller { get { return SteamVR_Controller.Input((int)trackedObj.index); } }
private SteamVR_TrackedObject trackedObj;
private GameObject pickup;
void Start()
{
trackedObj = GetComponent<SteamVR_TrackedObject>();
}
void Update()
{
if (controller == null)
{
Debug.Log("Controller not initialized");
return;
}
if (controller.GetPressDown(triggerButton) && pickup != null)
{
pickup.transform.parent = this.transform;
}
if (controller.GetPressUp(triggerButton) && pickup != null)
{
pickup.transform.parent = null;
}
}
private void OnTriggerEnter(Collider collider)
{
pickup = collider.gameObject;
}
private void OnTriggerExit(Collider collider)
{
pickup = null;
}
}
So how do I actually implement a change in ownership? I guess it's not that hard and I am just missing a simple function, but I can't get it to work.
Thank you very much for every help or advice! I really appreciate it!
Answer by Sab44 · Aug 04, 2017 at 02:52 PM
I managed to fix it. The solution is to check if you're the owner, and if you aren't, to transfer ownership to yourself.
if (controller.GetPressDown(triggerButton) && pickup != null)
{
if (pickup.GetComponent<PhotonView>().ownerId == PhotonNetwork.player.ID)
{
grabObject();
}
else
{
pickup.GetComponent<PhotonView>().TransferOwnership(PhotonNetwork.player.ID);
grabObject();
}
}
Hope this helps someone who had the same struggle as me.
Did you try to set "Takeover" option to the object? I tried but it didn't work.
just to make sure ,you place this inside ontriggerenter right ?
can you explain the seconds " if stantement " is for what ?
hitobject.GetComponent().Owner.ActorNumber == PhotonNetwork.LocalPlayer.ActorNumber
this is for what
This is placed inside ontriggerenter
There first if statement is to check if you have an object, if it's on the right layer, the second if statement is to check ownership, if player owns it grabobject(), else make player own it then grabobject()
To see if the owners number is = to the current players number. Hence, ownership.