---
title: "昼夜循环"
description: "让时间每隔几回合自己往前走，AI 的氛围和音乐跟着时段变，适合冒险和生活模拟类的世界。"
---
<div v-pre>

# 昼夜循环

> 时间自己往前走：每 3 回合从早晨到中午、到傍晚、到夜晚，再回到早晨。每个时段有自己的氛围给 AI 看，有自己的音乐，玩家随时看得到现在几点。用行为、条件设定和音乐规则搭出来，不用写代码。

---

## 你要做的东西

- **四个时段**：早晨 → 中午 → 傍晚 → 夜晚 → 早晨
- **每 3 回合推进一次时间**，玩家什么都不用做
- **氛围跟着变**：每个时段一条设定，写光线、温度、人们在干什么，只有当前时段那条会发给 AI
- **音乐跟着变**：早上鸟叫，夜里虫鸣，一首淡出一首淡入
- **时间显示在画面上**，玩家看得到

### 怎么运作

```
Turn 3 ends
  → The behavior "Morning → Noon" fires (Every N turns: 3, ONLY IF Time of day is Morning)
  → It sets Time of day to Noon and tells the AI time has moved on
  → The lore entry "Noon" (condition: Time of day is Noon) now reaches the AI; "Morning" no longer does
  → The music rule for Noon matches, and the Noon track fades in
  → Time of day on the player's screen reads Noon
```

时间推进时只变一样东西：这个变量。设定和音乐各自盯着变量，自己就跟上了。

---

## 一步一步来

### 第 1 步：建变量

点卡底下 **还可以加** 里的 **＋ 变量**。

| 字段 | 值 |
|-------|-------|
| 名称 | Time of day |
| 类型 | 文字 |
| 初始值 | `Morning` |
| 行为规则 | `The time of day: Morning, Noon, Evening or Night. Describe light, weather and people to match it.` |

展开 **AI 怎么用它**，把 **AI 权限** 设成 **AI 只读**。时钟由行为来拨，AI 只负责看。

点 **放到玩家界面上**，时间就会显示在聊天页上。

新开一局总是从 **初始值** 开始，所以每次都从早晨开始，不用另外加行为去重置。

---

### 第 2 步：写四条时段设定

每个时段加一条设定。然后把 **Time of day** 变量拖到每条设定上，点那根线，设好条件：Time of day **等于** `Morning`（其余依此类推）。每条设定会挪到条件设定那一块，只在条件满足时发给 AI。

#### 早晨

```
[Time of day: Morning]
It's early morning. Bring this into scene descriptions:
- Soft light from the east; the air is fresh and cool
- Dew on the grass and petals catches the light
- Birds in the branches, a rooster somewhere in the distance
- People are just getting up; shops open one by one
- The mood is calm and hopeful
```

#### 中午

```
[Time of day: Noon]
It's midday. Bring this into scene descriptions:
- The sun is straight overhead; the ground glares white
- The air is close; distant things shimmer in the heat
- Most people rest in the shade; the streets are quieter than in the morning
- Taverns and eating houses are at their busiest, and the smell of food drifts out
- The mood is slow and sweltering
```

#### 傍晚

```
[Time of day: Evening]
It's dusk. Bring this into scene descriptions:
- The setting sun turns the sky orange and purple, clouds edged with gold
- Long shadows stretch from buildings and trees
- Birds fly home; cooking smoke rises from the rooftops
- People finish work and head home; children chase each other in the street
- The mood is warm, a little wistful
```

#### 夜晚

```
[Time of day: Night]
It's deep night. Bring this into scene descriptions:
- Moon and stars are the only natural light, silvering everything
- Most windows are dark; here and there a candle still burns
- A cool breeze carries the sound of crickets and frogs
- The streets are nearly empty; a night watchman passes with a torch
- Something may be waiting in the dark: animals, thieves, or worse
- The mood is quiet and watchful
```

四条要是同时发给 AI，它就不知道该照哪条写。条件保证每次只发当前那一条。

---

### 第 3 步：建推进时间的行为

循环的每一步一条行为。点 **还可以加** 里的 **＋ 行为**。

#### 早晨 → 中午

| 部分 | 设置 |
|------|---------|
| 什么时候触发 | **每 N 回合**：`3` |
| 如果…… | Time of day 等于 `Morning` |
| 效果 | **修改变量**：Time of day 设为 `Noon` |
| | **告诉 AI**：`Time has moved on from morning to noon. Let the change show naturally in your next description.` |

#### 中午 → 傍晚、傍晚 → 夜晚、夜晚 → 早晨

做法一样，换成下一对值：

| 行为 | 如果…… | 修改变量 | 告诉 AI |
|-----------|---------|-----------------|-------------|
| 中午 → 傍晚 | Time of day 等于 `Noon` | Time of day 设为 `Evening` | `Time has moved on from noon to evening.` |
| 傍晚 → 夜晚 | Time of day 等于 `Evening` | Time of day 设为 `Night` | `Time has moved on from evening to night.` |
| 夜晚 → 早晨 | Time of day 等于 `Night` | Time of day 设为 `Morning` | `Night is over and a new day begins.` |

四条都在同样的回合触发（3、6、9……），但每条的 **如果……** 写的是不同的时段。引擎拿四条行为都还没跑之前的时间去检查，所以每次正好只有一条会触发。

**告诉 AI** 是给 AI 塞一句话，只管它的下一次回复。长期的氛围靠设定，这一句只是让过渡读起来自然。

---

### 第 4 步（可选）：每个时段配音乐

在 **音频** 页加四条 BGM 音轨（卡里还没有音频的话，点 **还可以加** 里的 **＋ 音频**）：早晨、中午、傍晚、夜晚，每条都打开 **循环播放**，**淡入 (秒)** 和 **淡出 (秒)** 都填 2。

然后打开 **BGM 播放配置** → **条件 BGM** → **添加条件规则**，每个时段加一条：

| 字段 | 值 |
|-------|-------|
| 条件 | **变量条件**：Time of day 等于 `Morning` |
| 播放 | 早晨 |
| 淡入 (秒) / 淡出 (秒) | `3` / `3` |
| 停止上一首 BGM | 打开 |

时间一变，旧规则对不上了，新规则开始放它的曲子：一首淡出，另一首淡入。规则的更多用法见[音频设计指南](./audio-design.md)。

---

### 第 5 步（可选）：画面上的时钟

**放到玩家界面上**（第 1 步）已经把时间放到聊天页上了。想要更有味道一点，就用玩家界面：

- **冒险状态** 模板（在 **整个界面** 里）显示当前位置、天数、时段和两条状态。它会自己加天数和时段的变量；可以把那两个删掉，把它的文字指向你的 Time of day，或者干脆用它的变量代替你的
- 或者在对话页上加一个 **文字** 部件，用 **插入变量的值…** 显示 `{Time of day}`。再加两个 **图片** 部件放太阳和月亮，各自在 **什么时候显示** 里选 **变量满足条件时**（月亮那个是 Time of day **等于** `Night`）

---

### 第 6 步：试玩

点顶栏的 **试玩**。

1. 前两回合是早晨
2. 第 3 回合之后是中午，AI 的下一次回复会提到炎热或头顶的太阳
3. 再过三回合：傍晚。再过三回合：夜晚。加了音乐的话，每次都会换
4. 再过三回合：又回到早晨

试玩右边的 **这回合** 能看到哪条行为触发了，AI 拿到了哪些设定。

**如果出了问题：**

| 现象 | 可能的原因 | 怎么改 |
|---------|-------------|-----|
| 时间一直不变 | 行为 **如果……** 里的值和 **初始值** 对不上 | 变量里的 `Morning` 和条件里的要一字不差 |
| 时间一下跳了两个时段 | 两条行为的 **如果……** 写了同一个值 | 每条行为检查不同的时段 |
| 氛围没变 | 某条设定的条件写错了，或者它还在每回合都发的那一块 | 检查每条设定的条件和 **什么时候发给 AI** |
| AI 自己改了时间 | **AI 权限** 还是 **AI 可读可写** | 改成 **AI 只读** |
| 两首曲子同时在放 | **停止上一首 BGM** 没打开 | 每条时段规则都打开它 |

---

## 变体：时段长短不一样

**每 N 回合** 的间隔永远一样。想要白天长、夜晚短（比如白天每个时段 4 回合，夜晚 2 回合），就自己数回合：

1. 加一个数值变量 **Turns this period**，初始值 `0`，**AI 权限** 设成 **仅引擎（对 AI 隐藏）**
2. 加一条行为：**每回合**，效果 **修改变量** Turns this period 加上 `1`
3. 把四条时间行为的 **什么时候触发** 改成 Turns this period 的 **变量越过阈值**，**上升到以上**：早晨 → 中午、中午 → 傍晚、傍晚 → 夜晚填 `3`，夜晚 → 早晨填 `1`。每条的 **如果……** 还是看 Time of day
4. 给这四条各加一个 **修改变量**：Turns this period 设为 `0`，这样到了新时段就重新数

### 按真实时间，不按回合

**每隔几秒** 在玩家开着游戏的时候按时钟触发。四条行为都填 `300` 的话，玩 20 分钟就过一天，不管玩家有没有在打字。

---

## 写代码的做法：最后一条消息上的时间徽章

如果你自己写界面，就在界面代码里读这个变量。上面的一切都不用改。

**面板 → 界面代码** → 打开 `index.tsx`，把默认的 `return <Chat />` 换掉：

```tsx
export default function MyWorld() {
  const api = useYumina();
  const timeOfDay = String(api.variables["Time of day"] || "Morning");

  const timeConfig = {
    "Morning": { icon: "☀️", color: "#fbbf24", bg: "rgba(251,191,36,0.15)" },
    "Noon":    { icon: "🌤️", color: "#f59e0b", bg: "rgba(245,158,11,0.15)" },
    "Evening": { icon: "🌅", color: "#f97316", bg: "rgba(249,115,22,0.15)" },
    "Night":   { icon: "🌙", color: "#818cf8", bg: "rgba(129,140,248,0.15)" },
  };
  const current = timeConfig[timeOfDay] || timeConfig["Morning"];
  const msgs = api.messages || [];

  return (
    <Chat renderBubble={(msg) => {
      const isLastMsg = msg.messageIndex === msgs.length - 1;
      return (
        <div>
          <div
            style={{ color: "#e2e8f0", lineHeight: 1.7 }}
            dangerouslySetInnerHTML={{ __html: msg.contentHtml }}
          />

          {isLastMsg && (
            <div style={{
              display: "inline-flex",
              alignItems: "center",
              gap: "6px",
              marginTop: "12px",
              padding: "4px 12px",
              background: current.bg,
              border: `1px solid ${current.color}33`,
              borderRadius: "999px",
              fontSize: "13px",
              color: current.color,
              fontWeight: "600",
            }}>
              <span style={{ fontSize: "16px" }}>{current.icon}</span>
              <span>{timeOfDay}</span>
            </div>
          )}
        </div>
      );
    }} />
  );
}
```

**代码做了什么：**

- `api.variables["Time of day"]` 按名字读这个变量
- `timeConfig` 给每个时段配一个图标和颜色
- `isLastMsg` 让徽章只出现在最后一条消息上。把这个判断去掉，每条消息都会盖上时间

**面板 → 界面代码** 里文件旁边的状态应该是绿色的 **正常**。

---

## 速查

| 你想要 | 怎么做 |
|---------------|-------------|
| 每 N 回合做一件事 | **什么时候触发**：**每 N 回合** |
| 按真实时间做一件事 | **什么时候触发**：**每隔几秒** |
| 按当前的值走不同分支 | 几条触发一样、**如果……** 条件不一样的行为 |
| 给 AI 每个时段不同的氛围 | 每个时段一条设定，条件看 Time of day |
| 音乐跟着时段换 | **条件 BGM**，**变量条件**，打开 **停止上一首 BGM** |
| 告诉 AI 时间往前走了 | 效果：**告诉 AI**（只管下一次回复） |
| 不让 AI 改时间 | **AI 权限**：**AI 只读** |
| 显示时间 | **放到玩家界面上**、一个文字部件，或者界面代码 |

---

::: tip 这是食谱 #9
同样的搭法（一个由行为按节奏推着走的变量，设定和音乐跟着它变）也能用在天气、季节或者 NPC 的心情上。玩家界面里的 **日程行动** 模板会加一个天数、一个时段和每天几次行动，适合养成和经营类的卡。
:::

</div>
