Talos docs

ListenOptions

Since 0.7

Options of {@link EventBus.on} and {@link EventBus.once}.

Plain text

Signature

export interface ListenOptions {
  /**
   * Also hear `name` when it is meant for another entity: an `emitTo` to someone else, and another
   * entity's character controller event (`land`, `jump`, `moving`, `idle`), which by default only
   * that entity's own behaviours hear. A referee or a crowd director that watches everyone opts in:
   *
   * ```ts
   * init(ctx) { ctx.events.on("land", (ev) => ctx.log("landed", { who: ev.by }), { every: true }) }
   * ```
   */
  every?: boolean
}

Import: import type { ListenOptions } from "@argus/api"

Members

MemberSignatureDescription
everyevery?: booleanAlso hear name when it is meant for another entity: an emitTo to someone else, and another entity's character controller event (land, jump, moving, idle), which by default only that entity's own behaviours hear. A referee or a crowd director that watches everyone opts in: ts init(ctx) { ctx.events.on("land", (ev) => ctx.log("landed", { who: ev.by }), { every: true }) }

Example

The tested fixture fixtures/recipes/collectible-and-goal/scenes/behaviors/collectible.ts.

import type { Behavior } from "@argus/api"

/**
 * A coin the player collects by walking into it.
 *
 * It says so once, as `collected`, and leaves the world. Counting is somebody else's job —
 * `goal-exit.ts` — so a coin never needs to know how many there are.
 *
 * The contact is `onTrigger`, which needs three things in the scene: a collider with
 * `isTrigger: true` (the volume), a `trigger` component (what fires), and a body on the player —
 * the character controller's capsule is one.
 */
interface State {
  collected: boolean
}

interface Props {
  /** Tag the toucher must carry. Default `player`. */
  playerTag?: string
}

export default {
  init(ctx) {
    if (typeof ctx.state.collected !== "boolean") ctx.state.collected = false
  },

  onTrigger(ctx, trigger) {
    if (ctx.state.collected || !trigger.entering) return
    if (!trigger.other.hasTag(ctx.props.playerTag ?? "player")) return
    ctx.state.collected = true
    // The `goal` component on this coin has the same id, so the playtester and the HUD agree.
    ctx.events.emit("collected", { goal: ctx.entity.id })
    // Last: after `despawn` every member of this entity's handles throws.
    ctx.entity.despawn()
  },
} satisfies Behavior<State, Props>