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

Rpc and NetworkVariable won't work after PlayerPrefab respawned

Open Claude-Leo opened this issue 1 year ago • 3 comments

Description

When the client’s Player is destroyed it’s also removing the client as an observer on other network objects. Here is a simple test scene

Environment

  • OS: Windows 10 Version 22H2
  • Unity Version: 6000.0.17f1
  • Netcode Version: 2.0.0-pre.4

Additional Context

This happens in version 2.0.0-pre.4, 1.11 has no such issue. Cerestorm and I found this bug.

Claude-Leo avatar Sep 11 '24 11:09 Claude-Leo

I'm not seeing the issue on either 2.0.0-pre.4 or latest NGO. When running the sample (I retrieved it from, the discussions thread) I see the client continually destroy and recreate itself and I see the RPC being sent for both clients. Given your description of your issue I was expecting after the respawn the client would no longer receive its RPCs. I've closed out the issue but if you can still reproduce it please re-open it with any more reproduction steps you have and I will take another look.

samlucas-unity avatar Sep 24 '24 16:09 samlucas-unity

Hi @samlucas-unity, it was me who replied to that thread (cerestorm) and I guess I wasn't clear enough to the OP.

The issue is if the client Player object is Destroyed by the host (as opposed to being Despawned) the client is removed as an observer from any other network objects. NGO 1.11 copes with this with the client still observing but the references to the client under Observers goes blank in the Unity editor, which makes sense as they're a reference to the client's Player object. In NGO 2.0.0 the client is removed under the Observers list for any network object it was observing at the time the client Player was destroyed.

I've attached the code I used to test this if it helps. lostobserver.unitypackage.zip

ezoray avatar Sep 30 '24 12:09 ezoray

Thanks for the reply, that really helped me get a solid repro of this issue. It is a regression from 1.11 and we have identified the code and I have a fix ready I'm just getting some tests around this to catch it in the future. For now you can remove this code:

// DANGO-TODO: When we fix the issue with observers not being applied to NetworkObjects,
// (client connect/disconnect) we can remove this hacky way of doing this.
// Basically, when a player disconnects and/or is destroyed they are removed as an observer from all other client
// NetworkOject instances.
if (networkObject.IsPlayerObject && !networkObject.IsOwner && networkObject.OwnerClientId != NetworkManager.LocalClientId)
{
    foreach (var netObject in SpawnedObjects)
    {
        if (netObject.Value.Observers.Contains(networkObject.OwnerClientId))
        {
            netObject.Value.Observers.Remove(networkObject.OwnerClientId);
        }
    }
}

In NetworkSpawnManager.cs.

I've reopened this issue and will close it again once I land the PR.

Thanks,

Sam

samlucas-unity avatar Oct 04 '24 09:10 samlucas-unity