Hi,
In the HealthUI lesson of the Unity Multiplayer course, I have the HandleHealthUpdate method that gets called by the SyncVar with the hook that assign the currenthealth variable value to the newHealth parameter of the HandleHealthUpdate
public class Health : NetworkBehaviour
{
[SerializeField] int maxHealth = 100;
public event Action serverOnDie;
public event Action<int, int> ClientOnHealthChange;
[SyncVar (hook = nameof(HandleHealthUpdate))]
int currentHealth;
#region Server
public override void OnStartServer()
{
currentHealth = maxHealth;
}
public void DealDamage(int damageAmount)
{
if(currentHealth == 0) { return;}
currentHealth = Mathf.Max(currentHealth - damageAmount, 0);
if(currentHealth != 0 ) {return;}
serverOnDie?.Invoke();
Debug.Log("we died");
}
#endregion
#region Client
void HandleHealthUpdate(int oldHealth, int newHealth)
{
ClientOnHealthChange?.Invoke(newHealth, maxHealth);
}
#endregion
}
The script works, but my question is how the SyncVar knows to which of the two method parameters it has to assign the currenthealth value? There is no part in the class where we specifically ask the SyncVar to hook the currenthealth value to the newHealth parameter.
Thank you for the help.