Skip to content

ResponsePacket and ResponsePatternCollection

ResponsePacket (from scenerystack/utterance-queue) is the standard way to give an Utterance structured content for the Voicing feature instead of a single flat string. It splits a response into four independently-enable-able categories — name, object, context, and hint — so a user's response-level preferences (toggling "hear hints" off, say) apply automatically without every call site re-implementing the logic. ResponsePatternCollection supplies the string template used to stitch whichever categories are enabled into one final string, controlling their order and punctuation.

ts
import { ResponsePacket, ResponsePatternCollection, Utterance } from 'scenerystack/utterance-queue';

A minimal example

ts
const packet = new ResponsePacket( {
  nameResponse: 'Ball',
  objectResponse: 'now at the left wall',
  contextResponse: 'Sound is playing',
  hintResponse: 'Press space to launch again'
} );

const utterance = new Utterance( { alert: packet } );
utteranceQueue.addToBack( utterance );

When the Utterance is announced, its content is resolved through the process-wide responseCollector, which reads each category's Property (e.g. "are hints enabled") and assembles only the enabled parts using the matching pattern from packet.responsePatternCollection — by default ResponsePatternCollection.DEFAULT_RESPONSE_PATTERNS, e.g. ', , ' when all four are present.

ResponsePacketOptions

OptionDefaultEffect
nameResponsenullLabels the element — usually the same content as its accessible name
objectResponsenullDescribes the element's current state (e.g. a slider's value)
contextResponsenullDescribes surrounding effects of the interaction
hintResponsenullA hint about how to interact further
ignorePropertiesfalseIf true, all four responses are always spoken regardless of responseCollector's enable/disable Properties
responsePatternCollectionResponsePatternCollection.DEFAULT_RESPONSE_PATTERNSWhich template set to combine enabled categories with

Each of the four responses accepts a plain string/number, a TReadOnlyProperty<string>, or a zero-arg function returning either (VoicingResponse) — evaluated fresh each time the packet is spoken, so a response can reflect live model state.

Methods

MethodDescription
.nameResponse / .objectResponse / .contextResponse / .hintResponse (getters)Resolve the corresponding field to its current text/value
setNameResponse() / setObjectResponse() / setContextResponse() / setHintResponse()Replace a field's content in place, so one long-lived ResponsePacket can be reused as state changes
copy()Returns a new ResponsePacket with the same (resolved) field values
serialize()Returns a plain options object with all four fields resolved, plus ignoreProperties and responsePatternCollection

ResponsePatternCollection

A ResponsePatternCollection holds one template string per possible combination of the four categories (name, nameObject, nameObjectContextHint, ..., 15 combinations total), keyed with /// placeholders. Construct one with only the templates you want to override — anything omitted falls back to DEFAULT_RESPONSE_PATTERNS's value for that key:

ts
const packet = new ResponsePacket( {
  nameResponse: 'Ball',
  objectResponse: 'moving fast',
  responsePatternCollection: ResponsePatternCollection.OBJECT_RESPONSE_FIRST_PATTERNS
} );
Static instanceBehavior
ResponsePatternCollection.DEFAULT_RESPONSE_PATTERNSName, then object, then context, then hint
ResponsePatternCollection.OBJECT_RESPONSE_FIRST_PATTERNSObject response spoken before the name response
ResponsePatternCollection.CONTEXT_RESPONSE_FIRST_PATTERNSContext response spoken before both name and object

Mutate the packet, not the Utterance, as state changes

Because ResponsePacket's setters let you update nameResponse/objectResponse/etc. in place, the idiomatic pattern is one long-lived ResponsePacket (often owned by the Node it describes) whose fields you update as model state changes, wrapped once in a long-lived Utterance that gets re-added to the queue — mirroring the same "reuse the instance" pattern Utterance itself expects. Building a brand-new ResponsePacket and Utterance on every change works but discards the queue's ability to recognize "this is the same alert, just updated."