GameEvent
Since 0.6
A game event as delivered to onEvent and to events.on handlers.
Signature
export interface GameEvent<T = unknown> {
name: string
/** Whatever was passed to `emit`, JSON round-tripped. */
data: T
/** Id of the emitting entity, or null for events raised by the engine or the app. */
by: string | null
}
Import: import type { GameEvent } from "@argus/api"
Members
| Member | Signature | Description |
|---|---|---|
name | name: string | |
data | data: T | Whatever was passed to emit, JSON round-tripped. |
by | by: string | null | Id of the emitting entity, or null for events raised by the engine or the app. |
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>