-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Syncvar): adding InitialOnly to syncvar (#941)
allows a syncvar to only be used for spawn message, meaning it wont be sent a 2nd time if changed later on server
- Loading branch information
1 parent
cda3795
commit abf4637
Showing
7 changed files
with
157 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
Assets/Tests/Runtime/ClientServer/SyncVarInitialOnlyTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
using System; | ||
using System.Collections; | ||
using NUnit.Framework; | ||
using UnityEngine; | ||
using UnityEngine.TestTools; | ||
|
||
namespace Mirage.Tests.Runtime.ClientServer | ||
{ | ||
public class SyncVarInitialOnly : NetworkBehaviour | ||
{ | ||
[SyncVar(initialOnly = true)] | ||
public int weaponIndex = 1; | ||
|
||
[SyncVar] | ||
public int health = 100; | ||
|
||
[SyncVar(initialOnly = true)] | ||
public float otherValue = 13f; | ||
} | ||
|
||
public class SyncVarInitialOnlyTest : ClientServerSetup<SyncVarInitialOnly> | ||
{ | ||
[Test] | ||
public void ChangingInitOnlyVarWontSetBehaviourDirty() | ||
{ | ||
serverComponent.weaponIndex = 10; | ||
Assert.That(serverComponent.IsDirty(), Is.False); | ||
|
||
serverComponent.otherValue = 5.2f; | ||
Assert.That(serverComponent.IsDirty(), Is.False); | ||
|
||
serverComponent.health = 20; | ||
Assert.That(serverComponent.IsDirty(), Is.True); | ||
} | ||
|
||
[UnityTest] | ||
public IEnumerator InitOnlyIsntSentToClient() | ||
{ | ||
serverComponent.weaponIndex = 10; | ||
serverComponent.health = 50; | ||
serverComponent.otherValue = 5.2f; | ||
|
||
yield return new WaitForSeconds(0.2f); | ||
|
||
Assert.That(clientComponent.weaponIndex, Is.EqualTo(1), "Should not have changed"); | ||
Assert.That(clientComponent.health, Is.EqualTo(50)); | ||
Assert.That(clientComponent.otherValue, Is.EqualTo(13f)); | ||
} | ||
|
||
[UnityTest] | ||
public IEnumerator BothSyncVarsAreSetIntially() | ||
{ | ||
var prefab = new GameObject("BothSyncVarsAreSet", typeof(NetworkIdentity), typeof(SyncVarInitialOnly)); | ||
NetworkIdentity identity = prefab.GetComponent<NetworkIdentity>(); | ||
identity.AssetId = Guid.NewGuid(); | ||
|
||
clientObjectManager.RegisterPrefab(identity); | ||
|
||
var clone = GameObject.Instantiate(prefab); | ||
SyncVarInitialOnly behaviour = clone.GetComponent<SyncVarInitialOnly>(); | ||
behaviour.weaponIndex = 3; | ||
behaviour.health = 20; | ||
behaviour.otherValue = 5.2f; | ||
|
||
serverObjectManager.Spawn(clone); | ||
uint netId = behaviour.NetId; | ||
|
||
yield return null; | ||
yield return null; | ||
|
||
client.World.TryGetIdentity(netId, out NetworkIdentity clientClient); | ||
SyncVarInitialOnly clientBehaviour = clientClient.GetComponent<SyncVarInitialOnly>(); | ||
|
||
Assert.That(clientBehaviour.weaponIndex, Is.EqualTo(3)); | ||
Assert.That(clientBehaviour.health, Is.EqualTo(20)); | ||
Assert.That(clientBehaviour.otherValue, Is.EqualTo(5.2f)); | ||
|
||
behaviour.weaponIndex = 2; | ||
behaviour.health = 30; | ||
behaviour.otherValue = 7.3f; | ||
|
||
yield return new WaitForSeconds(0.2f); | ||
|
||
Assert.That(clientBehaviour.weaponIndex, Is.EqualTo(3), "does not change"); | ||
Assert.That(clientBehaviour.health, Is.EqualTo(30)); | ||
Assert.That(clientBehaviour.otherValue, Is.EqualTo(5.2f)); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Assets/Tests/Runtime/ClientServer/SyncVarInitialOnlyTest.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
Assets/Tests/Weaver/SyncVarTests~/SyncVarsValidInitialOnly.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using Mirage; | ||
|
||
namespace SyncVarTests.SyncVarsValidInitialOnly | ||
{ | ||
class SyncVarsValidInitialOnly : NetworkBehaviour | ||
{ | ||
[SyncVar(initialOnly = true)] int var; | ||
[SyncVar(initialOnly = false)] int var2; | ||
} | ||
} |