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