Early development: IFLoom can change until 1.0. What's stable
IFLoom
First-party plugins

Messages

The messages plugin delivers texts, emails or letters when the story allows, kept in threads with replies that work like choices.

@ifloom/plugin-messages delivers messages to the player when the story allows, such as texts, emails or letters on the mat, kept in threads with replies that work like choices. A message has a sender, a body of blocks and replies that carry effects. It arrives the first time its condition holds at the end of a command, or each time an effect draws it from a pool, and it raises a notice tagged with its channel. The game declares its channels and messages in data.messages, so a mod adds either. The plugin needs no other plugin and has no phone of its own. It cooperates with the cast for senders' names and faces, and with the calendar for days. Add a plugin installs it, and Build a phone shows a channel as an app.

In the showcase: examples/showcase/src/story/ava.ts, where Ava texts the day after you meet her, follows up a moment later, and sends a line of small talk from a pool each day once you've answered. examples/showcase/src/world/phone.ts declares the texts channel.

import {
  messages,
  messagesOf,
  channelsOf,
  type Arrival,
  type Channel,
  type ChannelView,
  type MessageDef,
  type MessagesState,
  type MessagesView,
  type MessageView,
  type ReplyDef,
  type ThreadView,
} from '@ifloom/plugin-messages'

messages

The plugin's factory, which returns the plugin with the id messages. It takes no options. Every field of its result is in messages, and its helpers are listed under Functions.

After every command that goes through, the plugin's onCommand hook delivers each message whose when now holds and that hasn't arrived, in the order the game declares them. It never runs on start, so a message whose when holds in a new game arrives after the player's first command. A day is the calendar's day, or a turn in a game without the calendar, so list messages after the calendar in plugins.

import { loadGame, type Content } from '@ifloom/engine'
import { calendar } from '@ifloom/plugin-calendar'
import { cast } from '@ifloom/plugin-cast'
import { messages } from '@ifloom/plugin-messages'

declare const town: Content

export const { game } = loadGame([town], { id: 'small-town', title: 'Small Town', plugins: [calendar(), cast(), messages()] })

Content

Channel

A channel the game declares in data.messages.channels, such as texts, email or the doormat. Every field is in Channel.

The loader merges channels by id, so a mod adds a channel or renames one, and the view lists them in order. A message names its channel by id, which is also the tag on the notices it raises.

import type { Content } from '@ifloom/engine'

export const phone: Content = { data: { messages: { channels: [{ id: 'texts', name: 'Messages' }, { id: 'letters', name: 'Letters' }] } } }

MessageDef

A message the game declares in data.messages.messages. Every field is in MessageDef.

The loader merges messages by id, so a mod adds a message or replaces one whole. A message arrives once, the first time its when holds at the end of a command, and stays whatever when does after. With no when it arrives after the first command. The body renders as a scene's does, with tokens, whens, speech and pictures, and the message's effects run each time it arrives, in the same command.

FieldMeaning
fromThe sender, an entity by reference or by a bare id read as a scene's speaker is, or a name shown as written, such as 'Town council'.
threadThe thread it goes in. The thread is the sender if left out, so every message from ava in a channel shares one thread.
drawnFromThe pools messages/draw draws it from. A message with pools arrives only when drawn, never by when alone, and arrives again each time it's drawn, while its when holds.
weightIts weight in a draw, against the others that can be drawn. 1 if left out, and 0 is never.
afterHow long it waits once its when holds, { ms: 1500 }, with its thread showing the sender typing unless typing is false. It arrives at once if left out.

A message with after runs on the engine's timers. The plugin keeps the waiting message in the save as due and starts its own timer, messages.due, which delivers it through the messages/due effect when it runs out. Only the logged tick command runs a timer down, so a replay delivers it at the same point and a save made while it waits keeps waiting. One message waits at a time. While one does, any other message with after and every later message in its thread wait behind it, and a message without after in another thread still arrives at once.

Ava's first text arrives a day after you meet her, and a second follows two seconds later:

import { cast } from '@ifloom/plugin-cast'
import { messages, type MessageDef } from '@ifloom/plugin-messages'

export const texts: MessageDef[] = [
  {
    id: 'ava.text',
    channel: 'texts',
    from: 'ava',
    when: { all: [{ seen: 'ava.hi' }, { since: 'ava.hi', gte: 1 }] },
    body: [{ text: 'Good to meet you yesterday! Coffee’s on me next time 🙂' }],
    replies: [
      { id: 'yes', label: 'Deal!', effects: [{ stat: 'friendship', by: 5, of: cast.character('ava') }] },
      { id: 'later', label: 'Maybe later in the week' },
    ],
  },
  { id: 'ava.text.ps', channel: 'texts', from: 'ava', when: messages.arrived('ava.text'), after: { ms: 2000 }, body: [{ text: 'The café, not the one by the bus stop.' }] },
]

A message has no picture field of its own. A picture or a video goes in its body, as markup or a { media } block, and the view's beats keep it in order with the words.

ReplyDef

A reply the player can send to a message, an entry of its replies. Every field is in ReplyDef.

The player sends one reply to each arrival, once. label is the reply's words, with tokens filled, and its id if left out. A translation rewords it under messages/messages.<message>.replies.<reply>.label in its words table. A reply is on offer only while its when holds. Its effects run after the plugin records the reply, and they can be plain effects or a macro.

Conditions

Each condition is a plugin call, written by the helper beside it, and asks about the message's latest arrival. A message that hasn't arrived, or that the game doesn't have, is false.

messages/arrived

Holds once the message has arrived, at least once.

{ 'messages/arrived': { message: 'ava.text' } } // messages.arrived('ava.text')

messages/since

Holds while the whole days since the message last arrived fall in the range. { not: messages.since(id, { lt: 3 }) } keeps a message out of a draw for three days after it comes, and holds for a message that has never come.

{ 'messages/since': { message: 'ava.text', gte: 2 } } // messages.since('ava.text', { gte: 2 })

messages/replied

Holds once the player has replied to the message, with the reply reply if given. With a range, the whole days since the reply was sent must fall in it too, so { gte: 2 } holds two days or more after the reply.

{ 'messages/replied': { message: 'ava.text', reply: 'yes' } } // messages.replied('ava.text', 'yes')

messages/read

Holds once the player has read the message, by opening its thread or by replying. Its helper is messages.messageRead, because messages.read writes the command.

{ 'messages/read': { message: 'ava.text' } } // messages.messageRead('ava.text')

Effects

messages/draw

Draws one message from the pool, among those whose when holds, by weight, and delivers it as a new arrival with its notice. Nothing arrives when none can be drawn. The draw is seeded by the save, the turn and the number of arrivals so far, so a replay or an undo draws the same message.

{ 'messages/draw': { pool: 'ava.chat' } } // messages.draw('ava.chat')

Something has to run the draw, such as a trigger with once: { per: 'turn' }, as the showcase's ava.chat trigger does once Ava has a reply:

import type { TriggerDef } from '@ifloom/engine'
import { messages } from '@ifloom/plugin-messages'

export const chat: TriggerDef = { id: 'ava.chat', once: { per: 'turn' }, when: messages.replied('ava.text'), effects: [messages.draw('ava.chat')] }

messages/due

Delivers the message that's waiting, if one is. The plugin's messages.due timer runs it when the wait is over, so content never needs to, and it has no helper.

{ 'messages/due': {} }

Commands

Both commands go through locks, which name them messages/reply and messages/read, or both as messages/*. Triggers and every plugin's onCommand hook run after them, so a reply whose effects make another message's when hold brings that message in the same command.

messages/reply

Sends the reply reply to the arrival message, which marks the arrival replied and read and then runs the reply's effects. message is the arrival's key, which is the message's id for its first arrival. The view gives each reply its command with the right key.

{ 'messages/reply': { message: 'ava.text', reply: 'yes' } } // messages.reply('ava.text', 'yes')

The plugin refuses it, in this order:

  1. With There is no such message. (messages/noSuchMessage) when no arrival has that key.
  2. With You have already replied. (messages/alreadyReplied) once a reply was sent.
  3. With That is not a reply on offer. (messages/notOnOffer) when the message has no such reply, or the reply's when doesn't hold.

messages/read

Marks every arrival in the thread id read, or with channel every arrival in that channel, or with neither every arrival. The plugin never refuses it. A UI sends it when the player opens a thread, and the view's read buttons send it.

{ 'messages/read': { channel: 'letters' } } // messages.read({ channel: 'letters' })

Values

Prose prints this as a token.

KeyMeaning
{{messages/unread}}The number of unread messages in every channel.
{{messages/unread channel=texts}}The number of unread messages in that channel, and 0 for a channel the game doesn't have.

The value works as a badge too, with the channel as its argument, as a devices app's badge: { value: 'messages/unread', with: { channel: 'texts' } } does. Code reads it with amountOf.

State

MessagesState

The messages a save keeps at state.plugins.messages: the arrivals, the reply sent to each and the day it was sent, the arrivals read, and the message waiting. Every field is in MessagesState.

The save keeps no message's words or replies, which the plugin reads from content. replied, repliedOn and read are by the arrival's key. due is the message waiting with its key, its thread and whether the thread shows typing, or null. A UI reads MessagesView instead.

Arrival

One arrival of a message, an entry of arrived, oldest first. Every field is in Arrival.

key is the message's id for its first arrival, then <id>~2, <id>~3 and on, so a message drawn again has its own reply and read mark each time. turn is the turn it arrived on, and day the calendar's day, or the turn in a game without the calendar.

View

The plugin adds nothing to the room or the screen. A UI shows a channel where the game wants it, such as in a devices app or as letters on the mat.

Each arrival raises a notice with the id messages.<key>, the sender's name as its title, the message's first line as its text (cut to 80 characters) and the channel as its tag. An arrival in a thread whose last notice is still unseen replaces that notice, so a burst of texts is one notice with the newest line, and view.notices.byTag.texts counts threads with news. Reading a message doesn't dismiss its notice, and dismissing a notice doesn't read the message.

MessagesView

The messages as the player sees them, in view.plugins.messages. Every field is in MessagesView.

channels lists every channel, in the order the game declares them, empty ones included. unread counts the unread messages in every channel, and waiting is true while a message with after is on its way.

{ channels: [{ id: 'texts', name: 'Messages', unread: 1, read: { … }, threads: [ … ] }], unread: 1, waiting: false }

ChannelView

One channel as the player sees it, an entry of channels. Every field is in ChannelView.

threads lists the threads with an arrival, the one with the newest first. A thread whose first message is still on its way comes first, with no messages and typing on. read is a RenderedChoice labelled with the messages/allRead string that marks the channel read, or null while nothing in it is unread.

ThreadView

One thread as the player sees it, an entry of a channel's threads. Every field is in ThreadView.

id is the messages' thread, or their sender. The plugin asks the cast's cast/sender query for name, image and portrait, a person's face for an avatar. Without the cast it reads an entity's name and picture with speakerOf, and anything else is shown as written with no picture. messages lists the arrivals, oldest first. read is a button labelled with the messages/markRead string that marks the thread read, or null while nothing in it is unread. typing is true while a message with after waits to arrive in this thread and its typing isn't false.

MessageView

One arrival as the player sees it, an entry of a thread's messages. Every field is in MessageView.

key is the arrival's key, the one to use as a list key, and a message drawn twice is in its thread twice with one id. beats is the body rendered as a scene's is, with context.beats, so pictures and videos sit in order with the words. text joins the beats' words for a UI that renders no beats. replies lists the replies on offer as RenderedChoice buttons until one is sent, and replied is then the sent reply's label.

Render messages renders this view in Plain JS, React and Vue.

React UI

The React UI is optional: the @ifloom/plugin-messages/react subpath renders a channel for a game that uses @ifloom/react, and a game rendered another way never loads it. Show plugins in the React UI adds it to a game.

messagesUi

The plugin's PluginUi, listed in mountGame's plugins. It renders a MessagesSection wherever a devices app has a messages section. Its type is in messagesUi.

MESSAGES_WORDS

The words the messages UI uses, by key, which a game rewords as ui.<key> in its strings as it does the React UI's own. Its value is in MESSAGES_WORDS. The back button is the React UI's own ui.back, and the read buttons use the plugin's strings.

KeyMeaning
ui.noMessagesNo messages yet., the line of a channel with nothing in it.
ui.typing{name} is typing, the typing bubble's label for screen readers.
ui.typingShorttyping…, a thread's last line in the list while the sender is typing.

MessagesSection

Renders the channel an app section's with.channel names, from view.plugins.messages, as a phone renders one. Its props are in MessagesSection.

It opens on the list of threads, newest first, each with the sender's portrait, name, last line and a dot while it has unread messages, under the channel's Mark all read button. Choosing a thread opens it, with a back arrow, its messages as bubbles, and under each the reply sent or the replies on offer. A message's beats render with Beat, so a picture or a clip shows in the bubble. A typing bubble follows the last message while the thread's typing is on. Opening a thread sends its read command, and an arrival in an open thread is marked read as it comes.

It renders nothing when the channel isn't in the view. A game replaces it as MessagesSection in components. Its parts are channel (with data-channel), conversations, conversation (with data-thread, and data-unread while unread), conversation-portrait, conversation-name, conversation-last, conversation-unread, messages-empty, thread, thread-back, thread-title, thread-portrait, message (with data-message, the arrival's key), bubble (with data-from, and data-typing on the typing bubble), typing and button.

useMessages

Returns the messages' view from the running game with a read that marks a channel read, or null in a game without the plugin. It re-renders when the view changes. Its signature is in useMessages.

MessagesControls

The value useMessages returns, a MessagesView with read(channel), which sends messages/read for the channel and returns the runner's Result. Every field is in MessagesControls. A reply has its own command, which a component sends with useGame's send.

Functions

The helpers are properties of messages and write its calls, typed:

  • messages.arrived(message), messages.since(message, range), messages.replied(message, reply?, range?) and messages.messageRead(message) return the conditions.
  • messages.draw(pool) returns the messages/draw effect.
  • messages.reply(message, reply) returns the messages/reply command, and messages.read(thread?) or messages.read({ channel }) the messages/read command, as a CommandRef.

A café line shows once the player said yes to Ava:

import type { Block } from '@ifloom/engine'
import { messages } from '@ifloom/plugin-messages'

export const coffee: Block = { text: 'Ava has a cup waiting for you.', when: messages.replied('ava.text', 'yes') }

messagesOf

Returns the game's messages from data.messages.messages, merged from its content and packs, in order. Its signature is in messagesOf.

channelsOf

Returns the game's channels from data.messages.channels, merged from its content and packs, in order. Its signature is in channelsOf.

import { channelsOf, messagesOf } from '@ifloom/plugin-messages'

channelsOf(game) // [{ id: 'texts', name: 'Messages' }]
messagesOf(game).filter((message) => message.drawnFrom?.includes('ava.chat')).length // 4

Checks

When the game loads, the plugin reports problems with each message against the content or pack it came from, with where as messages <id>:

ProblemCause
unknown channel "fax"channel names a channel the game doesn't have.
unknown storyline "ghost"when names something the game doesn't have.
no prose for "ava.none"A body block names something the game doesn't have, or its when does.
arrival: unknown storyline "ghost"The message's effects name something the game doesn't have.
reply yes: unknown storyline "ghost"A reply's when or effects name something the game doesn't have.
two replies are called "yes"Two replies of the message share an id.
weight must be 0 or moreweight is negative.
after needs ms above 0after has no ms above 0.
a message drawn from a pool arrives when drawn, so it can’t wait with afterA message has both drawnFrom and after.

A condition or messages/reply that names a message the game doesn't have gives unknown message "ava.txt" where it's written. A messages/draw that names a pool no message is in gives draws from "nobody", but no message is drawn from it, and a messages/read with an unknown channel gives unknown channel "fax".

Strings

A game, a pack or a translation rewords these by their full key in its strings.

KeyMeaning
messages/noSuchMessageThere is no such message.
messages/alreadyRepliedYou have already replied.
messages/notOnOfferThat is not a reply on offer.
messages/markReadMark read, the label of a thread's read button.
messages/allReadMark all read, the label of a channel's read button.

Caveats

  • The plugin tests when at the end of each command, so a message whose when held only partway through a command waits for a later command that ends while it holds. A when on something that stays true, such as a kv key, a day or a scene seen, arrives on time.
  • The engine runs triggers before the plugins' hooks, so a trigger on messages.arrived(id) fires on the command after the message came. The message's own effects run in the same command.
  • A message with after arrives only when tick commands run its timer out, so a test or a UI that sends no ticks sends { type: 'tick', ms } itself.
  • A from that names no entity shows as written, and the loader doesn't report it, so a typo in a sender's id shows the typo.

Last updated on

On this page