Unity-Technologies / com.unity.netcode.gameobjects

Netcode for GameObjects is a high-level netcode SDK that provides networking capabilities to GameObject/MonoBehaviour workflows within Unity and sits on top of underlying transport layer.
MIT License
2.1k stars 430 forks source link

re-parenting done during scene load doesn't sync to clients #2857

Open zachstronaut opened 2 months ago

zachstronaut commented 2 months ago

Re-parenting done during scene load doesn't sync to clients, it just silently fails. As best I can tell, there's no bool in NetworkManager that corresponds to this state. You have to subscribe to NetworkManager.SceneManager.OnSceneEvent yourself and implement your own bool.

zachstronaut commented 2 months ago

The object I was re-parenting was IsSpawned == true but the re-parenting was still silently failing. It's possible the NetworkObject I was targeting as a parent was not itself IsSpawned yet and there was a race condition there?

NetworkBehaviour could have a virtual method that's like OnSceneSycned or OnPostNetworkSpawn something... it would be called after OnNetworkSpawn has completed for all scene spawned network objects.

NoelStephensUnity commented 2 months ago

@zachstronaut Yeah, that is a tricky one to solve for while in the middle of a scene loading event especially if you are handling the parenting within the OnNetworkSpawn method since it will be greatly dependent on both the parent and child/children being spawned before handling parenting.

A few things you could do:

Let me know if this helps resolve your issue?

zachstronaut commented 2 months ago

@NoelStephensUnity yes, I solved it via creating my own callback from a scene event handler sort of like your suggestion.

I think it is a bug though that re-parenting can silently fail.

And consider it a feature request for all NetworkBehaviours to have both an OnNetworkSpawn and also some sort of OnPostNetworkSpawn ... consistent with Awake()/Start() and how having those be separate allows for avoiding a certain type of race condition.

NoelStephensUnity commented 2 months ago

And consider it a feature request for all NetworkBehaviours to have both an OnNetworkSpawn and also some sort of OnPostNetworkSpawn ... consistent with Awake()/Start() and how having those be separate allows for avoiding a certain type of race condition.

Similar to my previous replay in your other filed issue, I am very much in alignment with:

It provides the opportunity to handle:

💯

NoelStephensUnity commented 2 months ago

This is a user pain area so leaving as bug but also marking it as a feature. Note for implementation: Target v2.0.0 and back port to v1.x.x