Transfer Ownership
The whole answer can be found below.
Try Our
Documentation
Please check if you can find an answer in our extensive documentation on PUN.
Join Us
on Discord
Meet and talk to our staff and the entire Photon-Community via Discord.
Read More on
Stack Overflow
Find more information on Stack Overflow (for Circle members only).
Transfer Ownership
deadlysnix
2018-06-28 08:13:53
Hello guys =)
I am trying to Change the ownership of my objects to the scene when no one is interacting with the object. The idea is, that you can only start to interact with an object if it is not used by another player.
All of my objects have a Photon View and a Photon Transform View Component.
Problem is, that the synchronization of the interaction of the clients is only synchronized the second time you grap the object. The first time grapping the masterclient sees no movement, but the client does, and the second time it works fine. Than I is not working again, and after that it is synchronized fine again. So only every second interaction of the client with the object is synchronized over the network and ownership set right.
I tried to set the PhotonView.isMine to false, but this is not possible, and I also tried experimenting putting the script in the observed components of photon view, change from monobehaviour to punbehaviour etc. but nothing worked so far.
Thanks for helping =)
This is my script for the ownership-Transfer:
[RequireComponent(typeof(PhotonView))]
public class ChangeOwner : Photon.PunBehaviour {
private ControllerSkript attachedWand;
// Use this for initialization
void Start () {
//this.photonView.ownerId = 0;
this.photonView.TransferOwnership(0);
print(photonView.isMine);
photonView.RPC("Uebergabe", PhotonTargets.All, "Ende");
print("Current Owner: " + this.photonView.ownerId);
print(photonView.isMine);
}
public void InteraktionGriffAnfang(ControllerSkript wand)
{
print(photonView.isMine);
print("start Interaction, Current Owner: " + this.photonView.ownerId);
attachedWand = wand;
if (this.photonView.ownerId == 0)
{
this.photonView.TransferOwnership(PhotonNetwork.player.ID);
print("Ownership transfered from scene to player");
}
else
{
Debug.Log("Object is already used by player" + this.photonView.ownerId);
return;
}
}
public void InteraktionGriffEnde(ControllerSkript wand)
{
print("ende interaktion");
if (wand == attachedWand)
{
attachedWand = null;
//this.photonView.ownerId = 0;
this.photonView.TransferOwnership(0);
print("end ineraction and owner transfered back to scene, current Owner;" + this.photonView.ownerId);
photonView.RPC("Uebergabe", PhotonTargets.All, "Ende");
}
}
[PunRPC]
void Uebergabe(string a)
{
if (a == "Ende")
{
print("owner vom Objekt:" + this.photonView.ownerId + "playerID:" + PhotonNetwork.player.ID);
//this.photonView.ownerId = 0;
this.photonView.TransferOwnership(0);
print("owner after rpc: " + this.photonView.ownerId);
print("Hallo Test");
}
}
}
Comments
[Deleted User]
2018-06-28 10:16:35
Hi @deadlysnix,
you basically have to wait until the Ownership Transfer is completed. This includes requesting the ownership of a certain object by a certain client and transferring the ownership to this certain client by the current owner. Since these are all network messages, you have to wait a 'moment' before the client can interact with the object.
deadlysnix
2018-06-29 09:48:47
Thanks for helping @Christian_Simon
but it doesn't matter how long I am interacting with the object, it simply only synchronizes ever second Interaction. I added an RPC, now it works, but the synchronization is not very smooth.
I think the ownerTransfer problem has something to do with the photon transform view script of the objects.
I do not want to request anything but just enable takeover IF no one else is currently interacting with the object I want to start to interact with.
Is there a way to smooth the RPC or change something in the Transform view to solve the problem with the synchronization just every second time?
Thanks =)
[Deleted User]
2018-07-03 13:36:56
Have you tried to request the ownership instead of directly trying to transfer it? If not, I would recommend you trying the Request option as well. You can see how this works by taking a look at the Ownership Transfer Demo documentation page. There is a section with a code snippet to demonstrate how requesting and transferring works.
deadlysnix
2018-07-03 14:01:52
The problem was the zero. I tried to transfer the ownership back to the scene by doing this.photonView.TransferOwnership(0). Changing the 0 to for example 5 works. I do not know why, maybe the zero is not allowed.
Problem is now, that the first time trying to take away an object another player is currently interacting with is not working (like I want it to be), but the second, third ... try allows me to take away the object and than both of the players see the object in their hands. It is kind of jumping between the two.