-
-
Notifications
You must be signed in to change notification settings - Fork 841
feat: SyncVar hooks can have 2, 1 or no params #4077
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -16,6 +16,56 @@ void OnValueChanged(int oldValue, int newValue) | |||||||||||
| HookCalled.Invoke(oldValue, newValue); | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| class HookBehaviourOneParam : NetworkBehaviour | ||||||||||||
| { | ||||||||||||
| [SyncVar(hook = nameof(OnValueChanged))] | ||||||||||||
| public int value = 0; | ||||||||||||
|
|
||||||||||||
| public event Action<int> HookCalled; | ||||||||||||
|
|
||||||||||||
| void OnValueChanged(int oldValue) | ||||||||||||
| { | ||||||||||||
| HookCalled.Invoke(oldValue); | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| class HookBehaviourNoParams : NetworkBehaviour | ||||||||||||
| { | ||||||||||||
| [SyncVar(hook = nameof(OnValueChanged))] | ||||||||||||
| public int value = 0; | ||||||||||||
|
|
||||||||||||
| public event Action HookCalled; | ||||||||||||
|
|
||||||||||||
| void OnValueChanged() | ||||||||||||
| { | ||||||||||||
| HookCalled.Invoke(); | ||||||||||||
| } | ||||||||||||
|
Comment on lines
+39
to
+42
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| class HookBehaviourPrecedence : NetworkBehaviour | ||||||||||||
| { | ||||||||||||
| [SyncVar(hook = nameof(OnValueChanged))] | ||||||||||||
| public int value = 0; | ||||||||||||
|
|
||||||||||||
| public event Action<int> HookCalled; | ||||||||||||
|
|
||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove blank line
Suggested change
|
||||||||||||
|
|
||||||||||||
| void OnValueChanged(int oldValue) | ||||||||||||
| { | ||||||||||||
| HookCalled.Invoke(1); | ||||||||||||
| } | ||||||||||||
|
Comment on lines
+53
to
+56
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
|
|
||||||||||||
| void OnValueChanged(int oldValue, int newValue) | ||||||||||||
| { | ||||||||||||
| HookCalled.Invoke(2); | ||||||||||||
| } | ||||||||||||
|
Comment on lines
+58
to
+61
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
|
|
||||||||||||
| void OnValueChanged() | ||||||||||||
| { | ||||||||||||
| HookCalled.Invoke(0); | ||||||||||||
| } | ||||||||||||
|
Comment on lines
+63
to
+66
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
|
|
||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| class GameObjectHookBehaviour : NetworkBehaviour | ||||||||||||
| { | ||||||||||||
|
|
@@ -247,6 +297,81 @@ public void Hook_OnlyCalledOnClientd() | |||||||||||
| Assert.That(serverCalled, Is.EqualTo(0)); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove blank line
Suggested change
|
||||||||||||
|
|
||||||||||||
| [Test] | ||||||||||||
| public void HookOneParam_CalledWhenSyncingChangedValued() | ||||||||||||
| { | ||||||||||||
| CreateNetworkedAndSpawn( | ||||||||||||
| out _, out _, out HookBehaviourOneParam serverObject, | ||||||||||||
| out _, out _, out HookBehaviourOneParam clientObject); | ||||||||||||
|
|
||||||||||||
| const int serverValue = 24; | ||||||||||||
|
|
||||||||||||
| // change it on server | ||||||||||||
| serverObject.value = serverValue; | ||||||||||||
|
|
||||||||||||
| // hook should change it on client | ||||||||||||
| int callCount = 0; | ||||||||||||
| clientObject.HookCalled += (oldValue) => | ||||||||||||
| { | ||||||||||||
| callCount++; | ||||||||||||
| Assert.That(oldValue, Is.EqualTo(0)); | ||||||||||||
| }; | ||||||||||||
|
|
||||||||||||
| ProcessMessages(); | ||||||||||||
| Assert.That(callCount, Is.EqualTo(1)); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove blank line
Suggested change
|
||||||||||||
|
|
||||||||||||
| [Test] | ||||||||||||
| public void HookNoParams_CalledWhenSyncingChangedValued() | ||||||||||||
| { | ||||||||||||
| CreateNetworkedAndSpawn( | ||||||||||||
| out _, out _, out HookBehaviourNoParams serverObject, | ||||||||||||
| out _, out _, out HookBehaviourNoParams clientObject); | ||||||||||||
|
|
||||||||||||
| const int serverValue = 24; | ||||||||||||
|
|
||||||||||||
| // change it on server | ||||||||||||
| serverObject.value = serverValue; | ||||||||||||
|
|
||||||||||||
| // hook should change it on client | ||||||||||||
| int callCount = 0; | ||||||||||||
| clientObject.HookCalled += () => | ||||||||||||
| { | ||||||||||||
| callCount++; | ||||||||||||
| }; | ||||||||||||
|
|
||||||||||||
| ProcessMessages(); | ||||||||||||
| Assert.That(callCount, Is.EqualTo(1)); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| [Test] | ||||||||||||
| public void HookPrecedence_CalledWhenSyncingChangedValued() | ||||||||||||
| { | ||||||||||||
| CreateNetworkedAndSpawn( | ||||||||||||
| out _, out _, out HookBehaviourPrecedence serverObject, | ||||||||||||
| out _, out _, out HookBehaviourPrecedence clientObject); | ||||||||||||
|
|
||||||||||||
| const int serverValue = 24; | ||||||||||||
|
|
||||||||||||
| // change it on server | ||||||||||||
| serverObject.value = serverValue; | ||||||||||||
|
|
||||||||||||
| // hook should change it on client | ||||||||||||
| int callCount = 0; | ||||||||||||
| clientObject.HookCalled += (paramCount) => | ||||||||||||
| { | ||||||||||||
| callCount++; | ||||||||||||
| // method with 2 params has the highest precedence | ||||||||||||
| Assert.AreEqual(2, paramCount); | ||||||||||||
| }; | ||||||||||||
|
|
||||||||||||
| ProcessMessages(); | ||||||||||||
| Assert.That(callCount, Is.EqualTo(1)); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| [Test] | ||||||||||||
| public void StaticMethod_HookCalledWhenSyncingChangedValued() | ||||||||||||
| { | ||||||||||||
|
|
||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,16 @@ | ||||||||||||
| using Mirror; | ||||||||||||
| using UnityEngine; | ||||||||||||
|
|
||||||||||||
| namespace WeaverSyncVarHookTests.FindsHookWithOtherOverloadsInOrder | ||||||||||||
| { | ||||||||||||
| class FindsHookWithNoParams : NetworkBehaviour | ||||||||||||
| { | ||||||||||||
| [SyncVar(hook = nameof(onChangeHealth))] | ||||||||||||
| int health; | ||||||||||||
|
|
||||||||||||
| void onChangeHealth() | ||||||||||||
| { | ||||||||||||
|
|
||||||||||||
| } | ||||||||||||
|
Comment on lines
+11
to
+14
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
| } | ||||||||||||
| } | ||||||||||||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,16 @@ | ||||||||||||
| using Mirror; | ||||||||||||
| using UnityEngine; | ||||||||||||
|
|
||||||||||||
| namespace WeaverSyncVarHookTests.FindsHookWithOtherOverloadsInOrder | ||||||||||||
| { | ||||||||||||
| class FindsHookWithOneParam : NetworkBehaviour | ||||||||||||
| { | ||||||||||||
| [SyncVar(hook = nameof(onChangeHealth))] | ||||||||||||
| int health; | ||||||||||||
|
|
||||||||||||
| void onChangeHealth(int oldValue) | ||||||||||||
| { | ||||||||||||
|
|
||||||||||||
| } | ||||||||||||
|
Comment on lines
+11
to
+14
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
| } | ||||||||||||
| } | ||||||||||||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,16 @@ | ||||||||||||
| using Mirror; | ||||||||||||
| using UnityEngine; | ||||||||||||
|
|
||||||||||||
| namespace WeaverSyncVarHookTests.FindsHookWithOtherOverloadsInOrder | ||||||||||||
| { | ||||||||||||
| class FindsHookWithTwoParams : NetworkBehaviour | ||||||||||||
| { | ||||||||||||
| [SyncVar(hook = nameof(onChangeHealth))] | ||||||||||||
| int health; | ||||||||||||
|
|
||||||||||||
| void onChangeHealth(int oldValue, int newValue) | ||||||||||||
| { | ||||||||||||
|
|
||||||||||||
| } | ||||||||||||
|
Comment on lines
+11
to
+14
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
| } | ||||||||||||
| } | ||||||||||||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -7,12 +7,12 @@ class ErrorWhenNoHookWithCorrectParametersFound : NetworkBehaviour | |||||||||||
| [SyncVar(hook = nameof(onChangeHealth))] | ||||||||||||
| int health; | ||||||||||||
|
|
||||||||||||
| void onChangeHealth(int someOtherValue) | ||||||||||||
| void onChangeHealth(int someOtherValue, int moreValue, bool anotherValue) | ||||||||||||
| { | ||||||||||||
|
|
||||||||||||
| } | ||||||||||||
|
Comment on lines
+10
to
13
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
|
|
||||||||||||
| void onChangeHealth(int someOtherValue, int moreValue, bool anotherValue) | ||||||||||||
| void onChangeHealth(int someOtherValue, int moreValue, int anotherValue, int moreValues) | ||||||||||||
| { | ||||||||||||
|
|
||||||||||||
| } | ||||||||||||
|
Comment on lines
+15
to
18
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
|
|
||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.