Mirror: Host SyncVar not set on server before client hook is called
Describe the bug If you host a game, then set a syncvar, the hook on the syncvar will be called before the syncvar is updated, as expected, but this is still true if the host executes a command. To be clear, this is an issue for the host only. A client connecting to a dedicated server will exhibit the expected behavior.
Repro project Minimal project with a single player script (with local authority checked) as follows:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;
public class NetPlayer : NetworkBehaviour
{
[SyncVar(hook = nameof(OnXSet))]
private int x = 5;
public override void OnStartAuthority() {
print("Authority started");
CmdSetX();
}
[Command]
private void CmdSetX() {
x = 10;
}
private void OnXSet(int newX) {
CmdCheckX(newX);
}
[Command]
private void CmdCheckX(int newX) {
print(x);
print(newX);
}
}
To Reproduce
- Start the scene
- Click “Host” on your NetworkManager
- Observe that the console prints out “5” and “10”
Expected behavior I would expect the console to print out “10” and “10”
Desktop (please complete the following information):
- OS: Windows
- Build target: PC
- Unity version: 2019.2.8f
- Mirror branch: master
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 24 (8 by maintainers)
Commits related to this issue
- breaking: fix: #1151 - assign SyncVars before calling the Hook. Hook now passes old and new value instead of changing it from new value to old value, as this would break all SyncVar Hook projects sile... — committed to MirageNet/Mirage by miwarnec 4 years ago
- breaking: fix: #1151 - assign SyncVars before calling the Hook. Hook now passes old and new value instead of changing it from new value to old value, as this would break all SyncVar Hook projects sile... — committed to MirageNet/Mirage by miwarnec 4 years ago
If we are going to break hooks, I vote for option 3, pass both old and new value, that is the most intuitive in my opinion