UdonSynced variable gets reset for a brief moment on ownership change

Hi,
Not sure if a bug or something of my doing.

I’m trying to implement a flow like this:

  • We have Player ID=1 (let’s call them P1) and Player ID=2 (P2).
  • P1 joins
  • P2 joins
  • P2 takes ownership of an object and changes a variable annotated by UdonSynced
  • P1 listens for changes and takes back the ownership

Now what is happening:

  • the variable’s initial value is set to -1
  • P2 takes the ownership
  • P2 sets the variable value from -1 to 1
  • P1 receives an update from -1 to 1
  • P1 takes the ownership
  • P2 receives an update from 1 to -1
  • P2 receives an update from -1 to 1

In the end, everything ends up in a valid state, but why the value for player 2 decided to reset itself after the ownership change? Did I do something wrong?

Here’s the code. Repro.Execute() is belling called by a button pressed by P2:

public class Repro : UdonSharpBehaviour
{
    [UdonSynced, FieldChangeCallback(nameof(TestVariable))]
    private int _testVariable = -1;

    public int TestVariable
    {
        get => _testVariable;
        set
        {
            int previousValue = _testVariable;
            _testVariable = value;
            RequestSerialization();
            OnTestVariableChanged(previousValue, value);
        }
    }

    public void OnTestVariableChanged(int oldValue, int newValue)
    {
        Debug.Log("OnTestVariableChanged " + oldValue + " -> " + newValue);
        if (Networking.LocalPlayer.playerId == 1)
        {
            Networking.SetOwner(Networking.LocalPlayer, gameObject);
        }
    }

    public void Execute() {
        // need to be owner to set TestVariable
        Networking.SetOwner(Networking.LocalPlayer, gameObject);
        TestVariable = 1;
    }
}

I tried:

  • Manually listen for callbacks
  • Execute it with and without RequestSerialization() function call

I’ve also tried to put a 2-second delay between:

  • First ownership change and variable value set
  • variable value set and the second ownership change

TIA

I forgot to post the screenshot, and for some reason, I can’t edit the original message:

Are you using Manual sync or Continuous sync?

Continuous sync

Hmmm, could you try switching to manual sync to see if the issue still occurs? I’d be curious to see if that helps.

You’re already using RequestSerialization, which isn’t actually needed for continuous sync to work.

1 Like

Manual sync fixed it, thanks a lot!
Why could this be happening? I’ve read that Manual sync should be preferred over Continuous sync when I care about better control, but I did not expect that.

It’s definitely weird! You may want to check feedback.vrchat.com to see if any other users have been experiencing the same issue - then our engineers might want to take a look at it.