Skip to content

Commit

Permalink
feat: adding bool for InitialState
Browse files Browse the repository at this point in the history
flag that can be used inside syncvar hooks to tell if it is the spawn payload or not
  • Loading branch information
James-Frowen committed Mar 16, 2023
1 parent 315825e commit cce01f8
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Assets/Mirage/Runtime/NetworkIdentity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,12 @@ public sealed class NetworkIdentity : MonoBehaviour
/// </summary>
public NetworkWorld World { get; internal set; }

/// <summary>
/// True while applying spawn payload within OnDeserializeAll
/// <para>Can be used inside syncvar hooks to tell if object has just spawned</para>
/// </summary>
public bool InitialState { get; private set; }

[Header("Runtime References")]

/// <summary>
Expand Down Expand Up @@ -714,6 +720,8 @@ private void OnDeserialize(NetworkBehaviour comp, NetworkReader reader, bool ini

internal void OnDeserializeAll(NetworkReader reader, bool initialState)
{
InitialState = initialState;

// deserialize all components that were received
var components = NetworkBehaviours;
// check if we can read at least 1 byte
Expand All @@ -729,6 +737,8 @@ internal void OnDeserializeAll(NetworkReader reader, bool initialState)
OnDeserialize(components[index], reader, initialState);
}
}

InitialState = false;
}

internal void SetServerValues(NetworkServer networkServer, ServerObjectManager serverObjectManager)
Expand Down
56 changes: 56 additions & 0 deletions Assets/Tests/Runtime/SyncVarInitialStateFlagTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using Mirage.Serialization;
using NUnit.Framework;

namespace Mirage.Tests.Runtime
{
public class SyncVarInitialStateFlagBehaviour : NetworkBehaviour
{
public event Action<int> Hook;

[SyncVar(hook = nameof(Hook))]
public int number;
}
public class SyncVarInitialStateFlagTest : TestBase
{
private readonly NetworkWriter ownerWriter = new NetworkWriter(1300);
private readonly NetworkWriter observersWriter = new NetworkWriter(1300);
private readonly NetworkReader reader = new NetworkReader();

[TearDown]
public void TearDown()
{
TearDownTestObjects();

ownerWriter.Reset();
observersWriter.Reset();
reader.Dispose();
}

[Test]
[TestCase(true)]
[TestCase(false)]
public void InitialStateHasCorrectValue(bool input)
{
var server = CreateBehaviour<SyncVarInitialStateFlagBehaviour>();
var client = CreateBehaviour<SyncVarInitialStateFlagBehaviour>();

server.number = 10;

var invoked = 0;
var result = false;
client.Hook += (_) =>
{
invoked++;
result = client.Identity.InitialState;
};

server.Identity.OnSerializeAll(input, ownerWriter, observersWriter);
reader.Reset(observersWriter.ToArraySegment());
client.Identity.OnDeserializeAll(reader, input);

Assert.That(invoked, Is.EqualTo(1));
Assert.That(result, Is.EqualTo(input));
}
}
}
11 changes: 11 additions & 0 deletions Assets/Tests/Runtime/SyncVarInitialStateFlagTest.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cce01f8

Please sign in to comment.