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

  1. Start the scene
  2. Click “Host” on your NetworkManager
  3. 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

Most upvoted comments

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