You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Calling NetworkShow on a NetworkObject that is in the process of spawning (such as from OnNetworkSpawn) can cause a duplicate CreateObjectMessage to be sent to the remote client. This only applies if the target client is not already an observer of the NetworkObject (such as when SpawnWithObservers is set to false).
Reproduce Steps
Setup a minimal project in which a remote client can join a host.
Create a new network prefab with a component that calls NetworkShow on its owner in OnNetworkSpawn, like so:
using Unity.Netcode;publicclassNgoBugComponent:NetworkBehaviour{publicoverridevoidOnNetworkSpawn(){base.OnNetworkSpawn();if(IsServer&&!NetworkObject.IsNetworkVisibleTo(OwnerClientId))
NetworkObject.NetworkShow(OwnerClientId);}}
Make sure SpawnWithObservers is set to false on the network prefab created in the previous step.
Create a scene-placed network object with a component that dynamically spawns the network prefab with ownership set to the remote client. For example:
using System.Collections;using Unity.Netcode;using UnityEngine;publicclassNgoBugSetup:NetworkBehaviour{[SerializeField]privateNetworkObjectm_networkPrefab;publicoverridevoidOnNetworkSpawn(){base.OnNetworkSpawn();if(!IsServer)return;
NetworkManager.OnClientConnectedCallback +=_onClientConnected;foreach(var clientId in NetworkManager.ConnectedClientsIds)
_onClientConnected(clientId);}publicoverridevoidOnNetworkDespawn(){if(IsServer)
NetworkManager.OnClientConnectedCallback -=_onClientConnected;base.OnNetworkDespawn();}privatevoid_onClientConnected(ulongclientId){if(clientId== NetworkManager.ServerClientId)return;
StartCoroutine(_waitAndThenSpawnNetworkObject(clientId));}private IEnumerator _waitAndThenSpawnNetworkObject(ulongclientId){yieldreturnnew WaitForSeconds(1f);
_spawnNetworkObjectForClient(clientId);}privatevoid_spawnNetworkObjectForClient(ulongclientId){
NetworkManager.SpawnManager.InstantiateAndSpawn(m_networkPrefab, clientId,true);}}
Point the m_networkPrefab serialized field to the network prefab that was created in step two.
Run the host and connect a remote client to it.
Observe that the remote client receives a warning about trying to spawn a network object that is already spawned.
Actual Outcome
Two CreateObjectMessage are sent to the remote client for the same NetworkObjectId, which causes a warning for trying to spawn a network object that is already spawned. Two copies of the spawned network object exist in the scene for the remote client (the original and the duplicate).
Expected Outcome
Only one CreateObjectMessage is sent to the remote client for the NetworkObjectId. No warning occurs and only one copy of the network object exists in the scene for the remote client.
Line 773 will eventually lead to OnNetworkSpawn being called, which in turn will result in our call to NetworkShow. The code for NetworkShow defers creating the CreateObjectMessage until the end of the frame, but also adds the client ID to the NetworkObject.Observers list:
The problem is, after the call to NetworkManager.SpawnManager.SpawnNetworkObjectLocally returns in NetworkObject.SpawnInternal, it will then loop through the observers and add a CreateObjectMessage for them (since NetworkShow would have added them to the observers list). Then, at the end of the frame, another CreateObjectMessage will be added due to the call to NetworkManager.SpawnManager.MarkObjectForShowingTo inside NetworkShow.
I think a potential solution to this issue would be to only call NetworkManager.SpawnManager.MarkObjectForShowingTo inside NetworkShow if the NetworkObject has finished spawning (i.e., SpawnInternal has returned). Something like this:
I have attached a project which provides you with two possible ways to handle hidden object spawning. The primary difference in from your approach is that showing the object should happen while the object is being spawned but afterwards.
The primary area I adjusted:
privatevoid_spawnNetworkObjectForClient(ulongclientId){if(!SpawnWithServerAsOwner){// Option 1: You can just spawn the object with the client already set as the ownervarinstance= NetworkManager.SpawnManager.InstantiateAndSpawn(m_networkPrefab, clientId,true);// You then should just show the object here and not in the spawn method of the object being spawned.
instance.NetworkShow(clientId);}else{// Option 2: You can spawn the object only on the server side with it not being visible to any client// This is sort of a "round about" way to handle this and is a bit more troublesome than it is worth (imo)varinstance= Instantiate(m_networkPrefab.gameObject);varnetworkObjectInstance= instance.GetComponent<NetworkObject>();
networkObjectInstance.Spawn();// The not recommended path:// You have to first "show" the object (i.e. send the CreateObjectMessage to the targeted client)
networkObjectInstance.NetworkShow(clientId);// You then have to change the ownership to the targeted client
networkObjectInstance.ChangeOwnership(clientId);}// Option 1: Only 1 message is sent (i.e. the CreateObjectMessage when you show it to the client)// Option 2: Two messages are sent (i.e. the CreateObjectMessage when it is shown and the ChangeOwnershipMessage)}
As well, I didn't use the NgoBugComponent you provided since you should only handle showing something after it has run through the spawn process (it just changes the "NetworkShow" from happening within the component to afterwards).
Here is the project with the modified code you provided (I renamed NgoBugSetup to HiddenObjectSpawner). HiddenObjectSpawner.zip
As a side note, in NGO v2.0.0 there are new NetworkBehaviour virtual methods and I believe the NetworkPostSpawn would allow you to handle showing the object from a component attached to the network prefab.
Description
Calling
NetworkShow
on aNetworkObject
that is in the process of spawning (such as fromOnNetworkSpawn
) can cause a duplicateCreateObjectMessage
to be sent to the remote client. This only applies if the target client is not already an observer of theNetworkObject
(such as whenSpawnWithObservers
is set tofalse
).Reproduce Steps
NetworkShow
on its owner inOnNetworkSpawn
, like so:SpawnWithObservers
is set tofalse
on the network prefab created in the previous step.m_networkPrefab
serialized field to the network prefab that was created in step two.Actual Outcome
Two
CreateObjectMessage
are sent to the remote client for the sameNetworkObjectId
, which causes a warning for trying to spawn a network object that is already spawned. Two copies of the spawned network object exist in the scene for the remote client (the original and the duplicate).Expected Outcome
Only one
CreateObjectMessage
is sent to the remote client for theNetworkObjectId
. No warning occurs and only one copy of the network object exists in the scene for the remote client.Environment
Additional Context
After investigating this issue on my end by stepping through code in a debugger, I believe I identified the reason for this bug.
com.unity.netcode.gameobjects/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs
Lines 773 to 781 in eb21383
Line 773 will eventually lead to
OnNetworkSpawn
being called, which in turn will result in our call toNetworkShow
. The code forNetworkShow
defers creating theCreateObjectMessage
until the end of the frame, but also adds the client ID to theNetworkObject.Observers
list:com.unity.netcode.gameobjects/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs
Lines 558 to 559 in eb21383
The problem is, after the call to
NetworkManager.SpawnManager.SpawnNetworkObjectLocally
returns inNetworkObject.SpawnInternal
, it will then loop through the observers and add aCreateObjectMessage
for them (sinceNetworkShow
would have added them to the observers list). Then, at the end of the frame, anotherCreateObjectMessage
will be added due to the call toNetworkManager.SpawnManager.MarkObjectForShowingTo
insideNetworkShow
.I think a potential solution to this issue would be to only call
NetworkManager.SpawnManager.MarkObjectForShowingTo
insideNetworkShow
if theNetworkObject
has finished spawning (i.e.,SpawnInternal
has returned). Something like this:and then
m_FinishedSpawning
would be set totrue
at the end ofSpawnInternal
. A similar thing should probably be done forNetworkHide
as well.I haven't tested this fix myself - just a thought.
The text was updated successfully, but these errors were encountered: