FirstGearGames / FishNet

FishNet: Unity Networking Evolved.
Other
1.23k stars 138 forks source link

SyncType does not send changes when NetworkObject is despawned on the same tick it's spawned #692

Open ooonush opened 1 month ago

ooonush commented 1 month ago

General Unity version: 6000.0.3 Fish-Networking version: 4.3.5

Description SyncType does not send changes to client when NetworkObject is despawned on the same tick it's spawned.

Replication

  1. Place the NetworkObject with the TestSpawner component on the scene:

    public class TestSpawner : NetworkBehaviour
    {
    [SerializeField] private TestNetworkBehaviour _prefab;
    
    public override void OnStartClient()
    {
        if (IsHostInitialized)
        {
            NetworkObject no = NetworkManager.GetPooledInstantiated(_prefab.NetworkObject, true);
            Spawn(no);
            // and despawn this:
            Despawn(no);
        }
    }
    }
  2. Create a NetworkObject prefab with a TestNetworkBehaviour component:

    public class TestNetworkBehaviour : NetworkBehaviour
    {
    private readonly SyncVar<int> _syncVar = new();
    
    public override void OnStartServer()
    {
        Debug.Log("OnStartServer");
        _syncVar.Value = 10;
    }
    
    public override void OnStartClient()
    {
        Debug.Log("OnStartClient " + _syncVar.Value);
    }
    }
  3. Start server
  4. Start other clients
  5. Start client-host

Expected behavior

On client started SyncType should be 10, not 0. Since OnStartClient is being called, the SyncTypes should also be synchronized. This is a problem for me when the projectile is despawned on the same tick it`s spawned.

Screenshots image

ooonush commented 1 month ago

What works for me is this despawn method:

private uint _tickSpawned;
private NetworkObject _no;

public override void OnStartClient()
{
    if (IsHostInitialized)
    {
        _no = NetworkManager.GetPooledInstantiated(_prefab.NetworkObject, true);
        Spawn(_no);
        _tickSpawned = TimeManager.Tick;
    }
}

private void TimeManager_OnTick()
{
    if (_no && TimeManager.Tick > _tickSpawned + 2)
    {
        Despawn(_no);
        _no = null;
    }
}

That is, I have to do a check for TimeManager.Tick > _tickSpawned + 2.

ooonush commented 1 month ago

For clients-only the check TimeManager.Tick > _tickSpawned works. But on the client-host I have to check TimeManager.Tick > _tickSpawned + 2, which is not very elegant.

FirstGearGames commented 1 month ago

I'm fairly certain your desired setup should work without any changes. There's even code specific to sending pending SyncType data through on despawn for this exact scenario.

I'll make this is a bug today and take a look, most likely something small.

On Thu, Jun 6, 2024, 5:07 AM Alven @.***> wrote:

For clients-only the check TimeManager.Tick > _tickSpawned works. But on the client-host I have to check TimeManager.Tick > _tickSpawned + 2, which is not very elegant.

— Reply to this email directly, view it on GitHub https://github.com/FirstGearGames/FishNet/issues/692#issuecomment-2151785262, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGPJC3RIGMIPJTF4KJN6QVTZGARGNAVCNFSM6AAAAABI4HYCVOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCNJRG44DKMRWGI . You are receiving this because you are subscribed to this thread.Message ID: @.***>

FirstGearGames commented 1 week ago

Do not have time to get this into 4.3.6 but making it high priority for 4.3.7