Skip to content

A story that plays itself

A story doesn’t need a person at the keyboard. Your server can pick each action itself, and Hidden Door still resolves it against the world, narrates the result and updates the instance’s history. Use this for characters who live their own lives while the player is away, for a “previously on” recap your client can present, or for building a library of stories in a world before anyone plays it.

The loop uses the same operations as a played story:

  1. If the story is waiting for input, answer it.
  2. Otherwise, ask for action suggestions and submit one of them.
  3. Stop when an END_STORY event arrives, then complete the story.

Create the lead and start a story as usual. Pick one of the three options yourself, for example the first. Keep the returned storyId.

This sketch uses JavaScript with fetch. It answers each input request with the first eligible choice, and always takes the first suggestion that isn’t marked impossible or too big:

const api = (path, body) =>
fetch(
`https://api.hiddendoor.co/experience/v1/instances/${instanceId}${path}`,
{
method: body ? 'POST' : 'GET',
headers: {
Authorization: `Bearer ${process.env.HD_API_KEY}`,
'Content-Type': 'application/json',
},
body: body && JSON.stringify(body),
},
).then(async (response) => {
const json = await response.json();
if (!response.ok) throw new Error(`${response.status}: ${json.message}`);
return json;
});
const answerFor = (request) => {
switch (request.type) {
case 'multipleChoice':
return { type: 'multipleChoice', optionId: request.options[0].id };
case 'scene':
return { type: 'scene', sceneName: request.options[0].sceneName };
case 'entity':
return { type: 'entity', entityId: request.eligibleEntityIds[0] };
case 'text':
return { type: 'text', value: writeTextFor(request.prompt) };
}
};
// startResult is the response from starting the story.
let result = { inputRequests: startResult.inputRequests, events: [] };
while (!result.events.some(({ event }) => event === 'END_STORY')) {
const [request] = result.inputRequests;
if (request) {
result = await api(`/stories/${storyId}/inputs/${request.eventId}`, {
answer: answerFor(request),
});
continue;
}
const { phrases } = await api(`/stories/${storyId}/action-suggestions`, {});
const action = phrases.find(
({ difficulty, scope, talkToEntityId }) =>
!talkToEntityId && difficulty !== 'impossible' && scope !== 'too_big',
) ?? { phrase: fallbackAction };
result = await api(`/stories/${storyId}/turns`, {
intentPhrase: { phrase: action.phrase },
});
}
await api(`/stories/${storyId}/complete`, {});

writeTextFor is yours to supply: a fixed value, a list to draw from or your own generator, depending on what the prompt asks for. A few more things to handle in a real version:

  • An entity request can arrive with no eligible entities. Create one that fits, using the request’s entityType with the matching character, item or location operation, then answer with its identifier.
  • A suggestion with talkToEntityId is an invitation to have a conversation, not a turn, so the sketch skips those. If none of the suggestions can be taken as a turn, it submits fallbackAction, any free-text action you choose, such as “Look around for what to do next”. A real version could open the conversation instead.
  • Steer the story by passing playerText when asking for suggestions, such as “Keep looking for the missing cobbler”. Suggestions follow that guidance.
  • A lost response doesn’t mean the action failed. Read the story and its outstanding input before retrying, so you don’t take the same turn twice.

The finished story is part of the instance like any other. Its turns hold every event in order, its objectives show what the lead set out to do and whether they managed it, and the world’s characters, places and relationships reflect what happened. A player who arrives later inherits all of it.

To steer a story without a player more closely, such as approving its plan or redoing a scene, the MCP has early access tools for imagined stories. They save the result to the instance as if it had been played.