Commit 81d53df
fix: host client SyncVar hooks ignoring AOI visibility (#4079)
* Initial plan
* Fix host client SyncVar hook AOI bugs
- Modified GeneratedSyncVarSetter methods to only call hooks in host mode if object is visible (in NetworkClient.spawned)
- Modified OnHostClientSpawn to deserialize spawn payload, triggering SyncVar hooks when objects enter AOI range
- This fixes both issues: hooks no longer fire at spawn for out-of-range objects, and hooks now fire when entering range
Co-authored-by: MrGadget1024 <9826063+MrGadget1024@users.noreply.github.com>
* Replace placeholder issue numbers with descriptive text
Co-authored-by: MrGadget1024 <9826063+MrGadget1024@users.noreply.github.com>
* Fix comment capitalization per code review
Co-authored-by: MrGadget1024 <9826063+MrGadget1024@users.noreply.github.com>
* Fix regression: restore SyncVar hooks in host mode with AOI
Previous fix prevented all SyncVar hooks from firing by checking NetworkClient.spawned too early.
The check happened when the value was set on the server, before the object was added to spawned.
New approach:
- Revert setter checks to allow hooks during server-side value assignment
- Add hostInitialSpawn flag to NetworkIdentity
- Set flag in OnHostClientSpawn before deserializing
- Modify all GeneratedSyncVarDeserialize methods to force hooks during initial spawn even if value unchanged
- Clear flag after deserialization
This ensures hooks fire when objects enter AOI range in host mode, addressing the original issue without breaking normal hook execution.
Co-authored-by: MrGadget1024 <9826063+MrGadget1024@users.noreply.github.com>
* Fix issue 1: prevent hooks from firing at spawn for out-of-range objects
Added NetworkClient.spawned.ContainsKey() check to all four GeneratedSyncVarSetter methods.
This prevents hooks from firing when SyncVars are set during OnStartServer() for objects not yet visible to host client.
Combined with the hostInitialSpawn flag (which forces hooks during deserialization when entering AOI),
this now fixes both issues:
- Issue 1: No hooks fire at spawn for out-of-range objects
- Issue 2: Hooks fire when objects enter AOI range
The check works because objects are added to NetworkClient.spawned in OnHostClientSpawn BEFORE deserialization,
so the deserialize path can fire hooks while the setter path is blocked for invisible objects.
Co-authored-by: MrGadget1024 <9826063+MrGadget1024@users.noreply.github.com>
* Fix unit tests: only check hostInitialSpawn in actual host mode
Added NetworkServer.activeHost check before using hostInitialSpawn flag in all four GeneratedSyncVarDeserialize methods.
This prevents the flag from being checked in non-host mode tests, which was causing NullReferenceExceptions when hooks were called during deserialization even though values hadn't changed.
The hostInitialSpawn flag should only force hooks in host mode. In regular client mode, hooks should only fire when values actually change.
Co-authored-by: MrGadget1024 <9826063+MrGadget1024@users.noreply.github.com>
* Fix host mode tests: only force hooks when AOI is active
Changed hostInitialSpawn flag to only be set when aoi != null in OnHostClientSpawn.
This ensures hooks are only forced during deserialization when AOI is actually hiding objects.
In regular host mode without AOI, hooks fire normally via setters when values change.
This fixes the host mode unit tests while maintaining the AOI fix for the original issue.
Co-authored-by: MrGadget1024 <9826063+MrGadget1024@users.noreply.github.com>
* perf: optimize GeneratedSyncVarDeserialize methods to skip work when no hook
Check OnChanged != null before calculating changed and hostInitialSpawnInHostMode booleans.
No point doing that work if there's no hook to call.
Applied to all 4 GeneratedSyncVarDeserialize methods:
- GeneratedSyncVarDeserialize<T>
- GeneratedSyncVarDeserialize_GameObject
- GeneratedSyncVarDeserialize_NetworkIdentity
- GeneratedSyncVarDeserialize_NetworkBehaviour<T>
Co-authored-by: MrGadget1024 <9826063+MrGadget1024@users.noreply.github.com>
---------
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: MrGadget1024 <9826063+MrGadget1024@users.noreply.github.com>1 parent caaf7e1 commit 81d53df
File tree
3 files changed
+70
-13
lines changed- Assets/Mirror/Core
3 files changed
+70
-13
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
565 | 565 | | |
566 | 566 | | |
567 | 567 | | |
568 | | - | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
569 | 571 | | |
570 | 572 | | |
571 | 573 | | |
| |||
592 | 594 | | |
593 | 595 | | |
594 | 596 | | |
595 | | - | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
596 | 600 | | |
597 | 601 | | |
598 | 602 | | |
| |||
619 | 623 | | |
620 | 624 | | |
621 | 625 | | |
622 | | - | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
623 | 629 | | |
624 | 630 | | |
625 | 631 | | |
| |||
647 | 653 | | |
648 | 654 | | |
649 | 655 | | |
650 | | - | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
651 | 659 | | |
652 | 660 | | |
653 | 661 | | |
| |||
795 | 803 | | |
796 | 804 | | |
797 | 805 | | |
798 | | - | |
| 806 | + | |
| 807 | + | |
| 808 | + | |
799 | 809 | | |
800 | | - | |
| 810 | + | |
| 811 | + | |
| 812 | + | |
| 813 | + | |
801 | 814 | | |
802 | 815 | | |
803 | 816 | | |
| |||
856 | 869 | | |
857 | 870 | | |
858 | 871 | | |
859 | | - | |
| 872 | + | |
| 873 | + | |
| 874 | + | |
860 | 875 | | |
861 | | - | |
| 876 | + | |
| 877 | + | |
| 878 | + | |
| 879 | + | |
862 | 880 | | |
863 | 881 | | |
864 | 882 | | |
| |||
918 | 936 | | |
919 | 937 | | |
920 | 938 | | |
921 | | - | |
| 939 | + | |
| 940 | + | |
| 941 | + | |
922 | 942 | | |
923 | | - | |
| 943 | + | |
| 944 | + | |
| 945 | + | |
| 946 | + | |
924 | 947 | | |
925 | 948 | | |
926 | 949 | | |
| |||
982 | 1005 | | |
983 | 1006 | | |
984 | 1007 | | |
985 | | - | |
| 1008 | + | |
| 1009 | + | |
| 1010 | + | |
986 | 1011 | | |
987 | | - | |
| 1012 | + | |
| 1013 | + | |
| 1014 | + | |
| 1015 | + | |
988 | 1016 | | |
989 | 1017 | | |
990 | 1018 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1384 | 1384 | | |
1385 | 1385 | | |
1386 | 1386 | | |
1387 | | - | |
| 1387 | + | |
| 1388 | + | |
| 1389 | + | |
| 1390 | + | |
| 1391 | + | |
| 1392 | + | |
| 1393 | + | |
| 1394 | + | |
| 1395 | + | |
| 1396 | + | |
| 1397 | + | |
| 1398 | + | |
| 1399 | + | |
| 1400 | + | |
| 1401 | + | |
| 1402 | + | |
| 1403 | + | |
| 1404 | + | |
| 1405 | + | |
| 1406 | + | |
| 1407 | + | |
| 1408 | + | |
| 1409 | + | |
| 1410 | + | |
| 1411 | + | |
1388 | 1412 | | |
1389 | 1413 | | |
1390 | 1414 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
118 | 118 | | |
119 | 119 | | |
120 | 120 | | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
121 | 126 | | |
122 | 127 | | |
123 | 128 | | |
| |||
0 commit comments