Validated SyncVar
private ValidatedSyncVar<int> _testVar = new();
private void OnEnable()
{
_testVar.serverValidation += ServerValidator;
_testVar.onValidationFail += OnValidationFail;
_testVar.onChangedWithOld += OnChanged;
}
private void OnDisable()
{
_testVar.serverValidation -= ServerValidator;
_testVar.onValidationFail -= OnValidationFail;
_testVar.onChangedWithOld -= OnChanged;
}
private bool ServerValidator(int oldValue, int newValue)
{
//Only the server will reach this. This is called whenever a change occurs.
if (oldValue > newValue)
return false;
return true;
}
private void OnValidationFail(int failedValue, int authoritativeValue)
{
//Only the owner will receive this if the validation fails
Debug.Log($"Validation failed on {failedValue}. Returning to {authoritativeValue}");
}
private void OnChanged(int oldValue, int newValue, bool validated)
{
//This is called immediately with validated false for the owner,
//and everyone received with validated = true if it goes through
Debug.Log($"Value changed: {oldValue} -> {newValue} | Validated: {validated}");
}Behavior & Expectations
Last updated