# Characters your game builds

A character can run a card someone published on Yumina (`card: { id }`), or a card your game builds itself and sends with the turn (`card: { world }`). Build your own when your characters come from the game: generated townsfolk, a sect of randomly rolled disciples, every villager in a mod. Rebuild it as often as you like, even every turn. Memory belongs to the character id and the save, not to the card.

## The smallest card

```json
{
  "name": "Old Huang",
  "entries": [
    { "content": "You are Old Huang, 75, the player's foster father. Gruff, proud, quietly fond of the player. You speak in short, dry sentences." }
  ]
}
```

That is a whole card. Each entry is a piece of text the character reads; one entry is enough.

## Entries

| Field | |
| --- | --- |
| `content` | Required. What the character reads. |
| `name` | A label for you; shown nowhere to the player. |
| `keywords` | `["Crimson Sect", "red banner"]`: the entry is read only when one of these words comes up in the conversation. Good for lore that would crowd every turn. |
| `conditions` | `[{ "variableId": "chapter", "operator": "gte", "value": 3 }]`: the entry is read only while your game's inputs match. `operator` is `eq`, `neq`, `gt`, `gte`, `lt`, `lte` or `contains`. |
| `conditionLogic` | `"all"` (default) or `"any"` of the conditions. |
| `position` | Order among entries, lowest first. Defaults to the order you list them. |

An entry with neither `keywords` nor `conditions` is read every turn.

Your manifest's `rules`, inputs, outputs and actions are added to every card on every turn, so a card never repeats them: write only the person.

## What goes in the card, and what in inputs

- **The card:** who this person is, and what doesn't change from line to line. Name, age, sect, temperament, how they speak, what they want, their past with the player.
- **Inputs:** what is true right now. Friendship, time and place, what they carry, who else is there. Your game sends these every turn, the character reads them and can't change them.

A good rule: if it would be wrong after the player walks into the next room, it is an input.

## Keep spoilers back

A condition on an input holds lore back until your game says so. Send how far the player has got (`chapter`, a story flag), and gate what the character may not know yet:

```json
{
  "name": "Old Huang",
  "entries": [
    { "content": "You are Old Huang, the player's foster father…" },
    { "name": "secret", "content": "Thirty years ago you served the Crimson Sect, and you still carry its token.",
      "conditions": [{ "variableId": "chapter", "operator": "gte", "value": 3 }] }
  ]
}
```

Before chapter 3 that entry never reaches the character, so it can't let it slip.

## Building one from game data

A game whose people are generated builds each card from what it knows about them:

```csharp
JsonValue CardFor(Npc npc) =>
    JsonValue.NewObject()
        .Set("name", npc.Name)
        .Set("entries", JsonValue.NewArray()
            .Add(JsonValue.NewObject().Set("content",
                $"You are {npc.Name}, {npc.Age}, {npc.Gender}, of {npc.Sect} ({npc.Rank}). " +
                $"Temperament: {npc.Temperament}. Traits: {string.Join(", ", npc.Traits)}. " +
                $"Martial arts: {string.Join(", ", npc.Skills)}. " +
                "Speak as this person would, in a few sentences."))
            .Add(JsonValue.NewObject().Set("content", npc.History)          // what the game recorded between them
                .Set("keywords", JsonValue.From(new[] { "past", "before", "remember" }))));

var turn = await yumina.Character($"npc:{npc.Id}", CardRef.Inline(CardFor(npc)))
    .SayAsync(text, inputs: new { favor = npc.FavorToPlayer, place = here.Name, carrying = npc.ItemNames },
              allow: new object[] { Allow.Action("give_item").Only("item", npc.ItemNames) });
```

Use a stable character id (`npc:<the game's id>`), so the same person keeps their memory across conversations and saves.

Build the card from the player's installed game at runtime rather than shipping the game's text in your mod. The [Stardew example](./example-stardew) does this for every villager.

## Try it before you write code

**Developer → your game → Try it → Your card (JSON)**: paste the card your game would send, fill in the inputs, and talk. It runs exactly as a turn from your game, with your draft manifest.

## Limits and errors

- At most 256 KB per card (`card_too_large`).
- `invalid_card` says what is missing, for example `card.world.entries[2] needs content (a string).`
- Cards your game builds run only in your game and are never listed on Yumina.
