Events & tokens

AzEventRef<T>

Use ref payload events for large struct payloads that should be delivered by readonly reference.

  • Large structs
  • Readonly ref
  • Copy avoidance
  • Handler shape

Use AzEventRef<T> when T is a large struct and handlers should receive it by readonly reference.

Use AzEventRef<T> for Large Struct Payloads

Use AzEventRef<T> when T is a large struct and you want handlers to receive it by readonly reference.

public readonly struct HitScanReport
{
    public readonly Vector3 Origin;
    public readonly Vector3 Direction;
    public readonly Vector3 HitPoint;
    public readonly float Distance;
    public readonly int TargetId;

    public HitScanReport(Vector3 origin, Vector3 direction, Vector3 hitPoint, float distance, int targetId)
    {
        Origin = origin;
        Direction = direction;
        HitPoint = hitPoint;
        Distance = distance;
        TargetId = targetId;
    }
}

public static readonly AzEventRef<HitScanReport> HitScanCompleted = new();

public void Fire()
{
    var report = new HitScanReport(origin, direction, hitPoint, distance, targetId);
    HitScanCompleted.Invoke(in report);
}

private void OnEnable()
{
    GameEvents.HitScanCompleted.Subscribe(OnHitScanCompleted).AddTo(_subscriptions);
}

private void OnHitScanCompleted(in HitScanReport report)
{
    Debug.Log($"Target {report.TargetId} at {report.Distance}");
}

AzEventRef<T> handlers must use in T:

private void OnPayload(in MyLargeStruct payload)
{
}