RequestOwnership doesn't work

Options
Here's the list of players in my current situation:

- Master Client (ViewID: 1001)
- Another Player (ViewID: 2001)

I will explain the situation as Another Player because requesting ownership from Another Player doesn't work at all.

Another player creates a weapon when it's instantiated by PhotonNetwork, and it has:
- ViewID: 2002
- IsMine: true
- Controller: [2]
- Owner: [2]
- Creator: [2]

Master can create extra weapons, and here's the new weapon just created:
- ViewID: 1017
- IsMine: false
- Controller: [1] (master)
- Owner: [1] (master)
- Creator: [1] (master)

Now Another Player picks up a new gun, this happens:
1. Another Player's old weapon will transfer ownership to the master.
2. Another Player requests ownership of the new weapon, so that Another Player can own it.

However, after calling RequestOwnership, the master still owns it:
- ViewID: 1017
- IsMine: false
- Controller: [1] (master)
- Owner: [1] (master)
- Creator: [1] (master)

But the original weapon that Another Player was dropped successfully transferred the ownership to master:
- ViewID: 2002
- IsMine: false
- Controller: [1] (master)
- Owner: [1] (master)
- Creator: [2]

Here's the code for pickup weapon:
public void PickupWeapon(WeaponBase weapon) {
    m_CurrentWeapon = weapon;
    m_CurrentWeapon.photonView.RequestOwnership();
    print("Request Ownership of: " + weapon.WeaponName);
}

In the console of Another Player, I can clearly see the log:
Request Ownership of: Hawk-50

Ownership Transfer is set to "Request" and also tried with "Take Over", but still the same. The only thing that worked was to transfer ownership to the master when Another Player switches the weapon.

Using Unity 2019.1.0f2 and the latest PUN 2.

Best Answer

  • modernator
    Answer ✓
    Options
    Change ownership transfer to "Takeover", and calling photonView.TransferOwnership(PhotonNetwork.LocalPlayer); solves my problem.

Answers

  • modernator
    edited August 2021
    Options
    I just turned on the pun logging and found this log:
    Ev OwnershipTransfer. ViewID 2002 to: 1 Time: 953
    

    Seems like 2002 refers to Another Player, and 1 refers to the master. So I just checked some internal photon code, and it seems like calling RequestOwnership passing the view ID and owner's actor number, not the caller's actor number.
    public void RequestOwnership()
    {
        if (OwnershipTransfer != OwnershipOption.Fixed)
        {
            PhotonNetwork.RequestOwnership(this.ViewID, this.ownerActorNr);
        }
        else
        {
            if (PhotonNetwork.LogLevel >= PunLogLevel.Informational)
            {
                Debug.LogWarning("Attempting to RequestOwnership of GameObject '" + name + "' viewId: " + ViewID +
                    ", but PhotonView.OwnershipTransfer is set to Fixed.");
            }
        }
    }
    

    So the new weapon is created by the master, so by default, the master is an owner. I thought that calling RequestOwnership will send the local player's actor number and which makes sense because Another Player request ownership of the new weapon from the master.

    Anyway, then I found that the TransferOwnership method can specify the new owner player, so I changed the code to like this:
    print("Request Ownership: " + WeaponName + " , " + ownerView.ViewID);
    // photonView.RequestOwnership();
    photonView.TransferOwnership(PhotonNetwork.LocalPlayer);
    

    However, still, the same log was found, something like ViewID 2002 to: 1, and I can clearly see still the master(1) owns it.
  • modernator
    Answer ✓
    Options
    Change ownership transfer to "Takeover", and calling photonView.TransferOwnership(PhotonNetwork.LocalPlayer); solves my problem.
  • Tobias
    Options
    Ah, yes, by default the PhotonViews are set to fixed ownership. Sorry you ran into this.