API Reference › Annotations › @OnAttach
annotation

@OnAttach

@OnAttach(Property) handler Name { … }

@OnAttach(Property) fires when the named property is structurally added to a live model instance with attach P on e — the property's fields are seeded first, then the hook runs in the context of that instance so it can read and adjust them. This is the structural birth of a property, the counterpart to @OnDetach; it differs from @OnEnable, which only un-pauses a property that already exists. Use it to react when a capability appears on an instance at runtime — a shield goes up, a status effect lands. The property name is a checked reference. Add @Public to also emit a prop_<Property>_attach event.

Example

program AttachHook {
  property Tag    { value: int = 0 }
  property Shield { amount: int = 0 }
  model Unit { Tag }

  @OnAttach(Shield) handler RaiseShield {            # fires when Shield is attached
    print(Shield.amount + 10)                        # 15 — reads the seeded amount
  }

  handler Seed phase Start { spawn Unit { Tag { value: 1 } } }

  handler AddShield phase Render {
    for (unit) in query [Unit] {
      attach Shield on self() { amount: 5 }          # triggers @OnAttach
    }
    quit()
  }
}
← All symbols