Commands on Events

Is it possible to make it work? Because so far it doesn’t work as I wish it for.

    [SyncVar (hook = "OnHealthChanged")] public float healthPoints = 100f;
    [SyncVar] public bool isAlive = true;
    public delegate void DeathDelegate(bool isAlive);
    public event DeathDelegate OnDeathEvent;

    public override void PreStartClient()
    {
        OnDeathEvent += CmdIsAlive;
    }

    [Command]
    private void CmdIsAlive(bool isAliveStatus)
    {
        isAlive = isAliveStatus;
    }

    private void OnHealthChanged(float newHealthPoints)
    {
        if(isLocalPlayer)
        {
            healthPoints = newHealthPoints;
            if(healthPoints <= 0)
            {
                //CmdIsAlive(false); <--- everything works if I run it like that
                if (OnDeathEvent != null)
                {
                    OnDeathEvent(false); <-- only works on host
                }
            }
        }
    }

Any ideas?

I don’t know exactly why, but if you handover the CmdFunction as the Eventhandler, it seems to call it as a normal function on the client instead of on the server. I usually use lambda expressions to work around this, like so:

public override void OnStartClient()
{
  OnDeathEvent += () => { CmdIsAlive(); };
}

This wraps the CmdFunction and ensures it to be called on the server.

Additional:
Instead of PreStartClient(), you should use OnStartClient(), since PreStartClient() is an internal function.

why are you using PreStartClient()? Dont use that… it is an internal function that can be replaced by code generation…