API Reference › Entities & the ECS › attach
keyword

attach

attach Prop on entity { overrides }

attach adds a property to a live model instance that did not have it, seeding the fields from their defaults plus any overrides in the { … } block, and fires the property's @OnAttach(Prop) hook. It is a structural change — it changes what the instance has — as opposed to enable, which merely resumes a property the instance already carries. Attaching a property the instance already has is a no-op, so the hook only runs on a real transition. Use it to grant something new at runtime: give a player a shield pickup, tag an enemy as enraged, or add a component only some instances need.

Example

program Pickups {
  property Position { column: int = 0, row: int = 0 }
  property Shield   { amount: int = 0 }
  model Player { Position }

  handler Boot phase Start {
    spawn Hero { Position { column: 6, row: 6 } }
  }

  handler GrabShield phase Update {
    if Input.key() == ' ' {
      for (position) in query [Position, {Player}] {
        attach Shield on self() { amount: 3 }
      }
    }
  }
}
← All symbols