Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Calling NetworkShow from OnNetworkSpawn can cause a duplicate CreateObjectMessage to be sent #2964

Copy link
Copy link
Open
@J0shhT

Description

@J0shhT
Issue body actions

Description

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

  1. Setup a minimal project in which a remote client can join a host.
  2. Create a new network prefab with a component that calls NetworkShow on its owner in OnNetworkSpawn, like so:
using Unity.Netcode;

public class NgoBugComponent : NetworkBehaviour
{
    public override void OnNetworkSpawn()
    {
        base.OnNetworkSpawn();
        if (IsServer && !NetworkObject.IsNetworkVisibleTo(OwnerClientId))
            NetworkObject.NetworkShow(OwnerClientId);
    }
}
  1. Make sure SpawnWithObservers is set to false on the network prefab created in the previous step.
  2. 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;

public class NgoBugSetup : NetworkBehaviour
{
    [SerializeField] private NetworkObject m_networkPrefab;

    public override void OnNetworkSpawn()
    {
        base.OnNetworkSpawn();
        if (!IsServer)
            return;
        NetworkManager.OnClientConnectedCallback += _onClientConnected;
        foreach (var clientId in NetworkManager.ConnectedClientsIds)
            _onClientConnected(clientId);
    }

    public override void OnNetworkDespawn()
    {
        if (IsServer)
            NetworkManager.OnClientConnectedCallback -= _onClientConnected;
        base.OnNetworkDespawn();
    }

    private void _onClientConnected(ulong clientId)
    {
        if (clientId == NetworkManager.ServerClientId)
            return;
        StartCoroutine(_waitAndThenSpawnNetworkObject(clientId));
    }

    private IEnumerator _waitAndThenSpawnNetworkObject(ulong clientId)
    {
        yield return new WaitForSeconds(1f);
        _spawnNetworkObjectForClient(clientId);
    }

    private void _spawnNetworkObjectForClient(ulong clientId)
    {
        NetworkManager.SpawnManager.InstantiateAndSpawn(m_networkPrefab, clientId, true);
    }
}
  1. Point the m_networkPrefab serialized field to the network prefab that was created in step two.
  2. Run the host and connect a remote client to it.
  3. 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.

Environment

  • OS: Windows 11 Pro 23H2
  • Unity Version: 2022.3.22f1
  • Netcode Version: 1.8.1
  • Netcode Commit: eb21383

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.

NetworkManager.SpawnManager.SpawnNetworkObjectLocally(this, NetworkManager.SpawnManager.GetNetworkObjectId(), IsSceneObject.HasValue && IsSceneObject.Value, playerObject, ownerClientId, destroyWithScene);
for (int i = 0; i < NetworkManager.ConnectedClientsList.Count; i++)
{
if (Observers.Contains(NetworkManager.ConnectedClientsList[i].ClientId))
{
NetworkManager.SpawnManager.SendSpawnCallForObject(NetworkManager.ConnectedClientsList[i].ClientId, this);
}
}

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:

NetworkManager.SpawnManager.MarkObjectForShowingTo(this, clientId);
Observers.Add(clientId);

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:

if (m_FinishedSpawning)
    NetworkManager.SpawnManager.MarkObjectForShowingTo(this, clientId); 
 Observers.Add(clientId); 

and then m_FinishedSpawning would be set to true at the end of SpawnInternal. A similar thing should probably be done for NetworkHide as well.

I haven't tested this fix myself - just a thought.

Metadata

Metadata

Labels

stat:awaiting-responseAwaiting response from author. This label should be added manually.Awaiting response from author. This label should be added manually.stat:importedStatus - Issue is tracked internally at UnityStatus - Issue is tracked internally at Unitytype:supportQuestions or other supportQuestions or other support

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    Morty Proxy This is a proxified and sanitized view of the page, visit original site.