# api
earshotKit public API (require("~earshotKit.api") from a package, earshotKit.api from a world script): define/loadAll exchanges, play/stop, on(started|line|ended|heard), heard(id, peer?), list, get, status, configure({style, radius, showMeter}), settings, start.
# Within Earshot: Overheard Conversations and Eavesdropping Kit (earshotKit)
Let players **overhear NPC conversations** and **eavesdrop** on them for **gossip, intel and clues**. Two guards trade the vault rota at the gate, a merchant mutters about a buried crate at the well after dark, a pair of servants whisper a name: a player who walks close enough reads it, and one who stays in **earshot** for the line that matters gets a `heard` event with the clues the conversation grants. For stealth, detective, mystery, heist and social games, single player or multiplayer.
What a world gets from installing it:
- **Conversations as data.** One `<id>.data/values.yaml` per exchange (`contract: earshotKit.exchange`), or one `Earshot.define{...}` call: speakers (any entities, by name), lines, earshot radius, trigger, repeat, key line, grants.
- **Three triggers.** `meet`: every speaker within `meetRange` (3.5 m) of the others. `place`: speaker 1 within `placeRadius` of a named entity, optionally between hours `from`/`to` of townsfolkKit or worldClockKit time. `manual`: only `Earshot.play(id)`.
- **Multiplayer and late joiners.** A synced `EarshotHost` (spawned on demand by the room host) steps every line on `multiplayer.sessionTime()` and keeps the current exchange, line index and line start in Sync fields, so all players and anyone who joins halfway read the same line.
- **Three looks, one setting.** `bubbles` (speech bubbles over each speaker's head with a tail), `captions` (cinematic caption bar, speaker name in an accent colour), `whisper` (faint italic ticker along the top; words drop to dots near the edge of earshot). Fits phones (390x844) and desktops inside the safe area; reduced-motion aware; takes no input and blocks none.
- **Ear meter.** A small listening meter fills as lines are heard and turns gold on the key line.
- **Speakers face each other** while talking (moved only by the peer allowed to move them; townsfolkKit people are left to their own controller).
- **Events.** `started`, `line`, `ended` on every peer; `heard {id, grants, peer, avatar}` on the hearing peer and on the host for every player.
No dependencies. **townsfolkKit is optional**: its people work as speakers (entities found by name) and its clock gates `place` exchanges; without it `from`/`to` hours are ignored unless worldClockKit answers.
## Start in two steps
1. Put an `EarshotSettings` component on any entity in the scene (it picks the look and spawns the host), or call any `Earshot` function from a scene script.
2. Write an exchange:
```yaml
# exchanges/docks_gossip.data/values.yaml
contract: earshotKit.exchange
values:
id: docks_gossip
speakers: ["Guard A", "Guard B"]
trigger: meet
lines:
- { by: 1, text: "Captain's ship comes in at midnight." }
- { by: 2, text: "Pier nine again? The usual crates?" }
- { by: 1, text: "Same as always. Nobody looks at pier nine." }
grants: ["intel_docks_midnight"]
```
Walk two entities named `Guard A` and `Guard B` together in play and walk close: the lines appear, the meter fills, `heard` fires.
## API
```lua
local Earshot = require("~earshotKit.api") -- from a package; "earshotKit.api" from a world script
Earshot.define(def) -> (ok, problems) -- same shape as the .data values
Earshot.loadAll() -> count -- every exchange .data file in the world (automatic on first use)
Earshot.play(id, { restart? }) -> boolean -- ask the host to start it now (any trigger)
Earshot.stop(id) -> boolean
Earshot.on(name, fn) -> unsubscribe -- "started" {id}, "line" {id, index, text, speaker}, "ended" {id},
-- "heard" {id, grants, peer, avatar}, "*"
Earshot.heard(id, peer?) -> boolean -- this peer by default
Earshot.list() / Earshot.get(id)
Earshot.status() -- host, authority, running exchanges, what is on screen, heard
Earshot.configure({ style = "bubbles"|"captions"|"whisper", radius?, showMeter? })
Earshot.settings() / Earshot.start() / Earshot.problems()
```
`heard` fires once per run on the hearing peer (with `first = true` the first time this peer heard that id) and on the host once per player per exchange id (`host = true`). Hand grants to your own systems there: questKit flags, a clue board, saveKit unlocks.
## Exchange fields
| field | meaning |
|---|---|
| `id` | id for play, heard and events |
| `title` | short label (default: id) |
| `titles[]` | translations of the title: `{ lang, text }` rows (e.g. `{ lang: pt-BR, text: "..." }`) |
| `speakers` | entity names, in order |
| `lines[]` | `{ by = index, text, hold?, languages? }` or `{ speaker = name, text }` (in code `by` also takes a name); `hold` default = reading time from the word count (1.8 to 7.5 s), plus a 0.35 s beat |
| `radius` | earshot metres from the line's speaker (default 7, or EarshotSettings `radius`) |
| `trigger` | `meet` / `place` / `manual` |
| `meetRange` | metres for `meet` (3.5) |
| `at`, `placeRadius`, `from`, `to` | `place`: entity name, metres (3), hours like `"21:00"` (wraps past midnight) |
| `repeat`, `cooldown` | once only by default; `repeat: true` plays again `cooldown` s (30) after it ended |
| `key` | the line that counts as heard (default: last) |
| `grants` | strings given to listeners |
**Translations.** Each player reads a line in their own language, two ways, no code: a `text` (or `title`) of the form `"@<namespace>.<key>"` reads that line of a catalog (`/locales/<lang>/<namespace>.json` in the world; catalogs inside another package are not visible to earshotKit), or the line carries its translations inline, `languages: [{ lang: pt-BR, text: "Psiu. Fala baixo." }]` (the `text` is the English fallback). The ear meter's words come from `earshotKit.package/locales` (en, pt-BR).
## Contents
- `api.module`: the public API. `core.module`: pure timing / trigger / clarity logic. `defs.module`: data loading. `runtime.module`: host stepping, listening, events, facing. `view.module`: the three looks and the meter.
- `EarshotHost.component` (synced state; spawned for you), `EarshotSettings.component` (style, radius, showMeter).
- `exchange.dataType` and three example exchanges in `exchanges/` (meet, place, manual) used by the demo.
- `earshotDemo.scene`: two townspeople meet in a square and gossip, an old man mutters at the well; walk close to listen.
- `earshotKitTests.testSuite`: the pure parts (line timing, triggers, cooldowns, clarity, data checks).
- Guide: `how-to-let-players-overhear-npc-conversations-and-eavesdrop-for-clues`.
## Limits
- Speakers are found by entity name (`entity.find`); give them unique names.
- Code-defined exchanges should be defined on every peer; the host also carries each line's text and speaker in its Sync state, so other peers still show lines of an exchange only the host defined.
- One run per exchange id at a time; different exchanges run side by side.
- Lines are text only (no voice clips).