com.unity.netcode.gameobjects icon indicating copy to clipboard operation
com.unity.netcode.gameobjects copied to clipboard

IsOwner is false on clients for player objects during OnNetworkSpawn()

Open dsaunder opened this issue 3 years ago • 1 comments

Description

Any update on this? I updated to Netcode v1.0.1 and IsOwner is false for my player objects during OnNetworkSpawn(). I've got a workaround for the few instances that I currently use this, but it's error prone and adds some complexity.

Reproduce Steps

  1. Create a new NetworkBehavior
  2. Override OnNetworkSpawn() with something like: public override void OnNetworkSpawn() { base.OnNetworkSpawn(); Debug.Log($"IsOwner: {IsOwner}"); }
  3. Create a prefab with the new NetworkBehavior script
  4. Make that prefab the default PlayerObject
  5. Start a host, observe the output of the OnNetworkSpawn() debug log line.
  6. Start a client, observe the output of the OnNetworkSpawn() debug log line.

Actual Outcome

The client log line is: "IsOwner: false"

Expected Outcome

The client log line is: "IsOwner: true"

Screenshots

N/A

Environment

  • OS: Windows 10
  • Unity Version: 2021.2.16f
  • Netcode Version: 1.0.1
  • Netcode Commit: [e.g. https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/commit/ba418fa5b600ad9eb61fab0575f12fbecc2c6520]

Additional Context

This looks like a possible regression as IsOwner used to be set correctly during OnNetworkSpawn().

dsaunder avatar Sep 09 '22 16:09 dsaunder

MTT-4717

ashwinimurt avatar Sep 22 '22 14:09 ashwinimurt

@dsaunder I just ran this test using this script:

        public override void OnNetworkSpawn()
        {
            var instanceType = IsServer ? "Server" : "Client";
            if (IsOwner)
            {
                Debug.Log($"[{instanceType}] Local player ({NetworkObject.OwnerClientId}) spawned!");
            }
            else
            {
                Debug.Log($"[{instanceType}] Remote player ({NetworkObject.OwnerClientId}) spawned!");
            }
        }

This was the output I got:

[Client] Remote player (0) spawned! [Client] Local player (2) spawned!

Does your script look similar to the above? If not, then make sure you are checking:

  • if the NetworkObject spawning belongs to the local player
  • what the NetworkObject's OwnerClientId is to determine which instance is being spawned on the client

you can also do something like this:

            if (NetworkObject.OwnerClientId == NetworkManager.LocalClientId)
            {
                Debug.Log("This is the local player's NetworkObject.");
            }

If you are trying to only execute code specific to the local player's instance.

Let me know if this resolves your issue?

NoelStephensUnity avatar Sep 23 '22 23:09 NoelStephensUnity

I thought i was having the same issue. I tried the debug script you provided and it seems that the client IsOwner. Although I still get "Client is not allowed to write to this networkvariable" when triyng to move. when editor is client.

If i have the server in the editor i get: "Trying to update interpolator when no data has beed added to it yet".

I am using the ClientNetworktTransform.

BaloghDaniel avatar Sep 25 '22 18:09 BaloghDaniel

I Debuggd all the way trough the "TrycommitTransformToServer" method in ClientNetworkTransform and i found this.

`        public bool CanClientWrite(ulong clientId)
        {
            switch (WritePerm)  --> (Writeperm = "server" in this switch)<--
            {
                default:
                case NetworkVariableWritePermission.Server:
                    return clientId == NetworkManager.ServerClientId;
                case NetworkVariableWritePermission.Owner:
                    return clientId == m_NetworkBehaviour.NetworkObject.OwnerClientId;
            }
        }`

I suppose this is supposed to be "Owner" so that my client have writeperm? But i cant figure out why its just server that has writeperm. I thought the ClientNetwork transform made it so that the owner hade permission?

EDIT You can override the OnIsServerAuthorative as such: "protected override bool OnIsServerAuthoritative() "

and just return false to manually set it to not be ServerAuthoritative. That did it for me. Although i think this should not be neccessary. Haven't seen anyone do this when checking tutorials and such for anwsers...

Hope that helps someone in the future!

BaloghDaniel avatar Sep 25 '22 19:09 BaloghDaniel

@BaloghDaniel Yeah, I see that we are missing some information in that NetworkTransform documentation on this indeed. I went ahead and submitted a PR to provide more details on this.

As a side note, the most recent version of the ClientNetworkTransform does actually have the OnIsServerAuthorative method.

I am going to close this issue as being resolved at this time. If you have any other questions regarding NetworkTransform please feel free to post here and I will do my best to assist you.

NoelStephensUnity avatar Sep 25 '22 21:09 NoelStephensUnity