《游戏编辑器技术原理与架构》13.2 快照暂停单步

Play 模式要能暂停:暂停后模拟不再自动推进,但可手动单步。PauseStepper 维护暂停状态——pause 冻结自动推进、step 手动推进一格、resume 恢复;SnapshotStore 保存/恢复模拟快照——单步几步不满意,restore 回到暂停点。暂停与快照配合,才谈得上可控的 Play。6 条用例本机真跑。

13.2 快照暂停单步

Play 模式的调试价值在于可控:游戏一直在跑,怎么检查一个瞬间的状态?答案是暂停、单步、回退。本节把「可控的 Play」落成两个基础件:

  • PauseStepper:暂停与推进。pause 冻结自动推进,step 手动推进一格(只在暂停时有效),resume 恢复。
  • SnapshotStore:快照存取。暂停瞬间 save 一份,用户单步几步不满意,restore 回到暂停点。

暂停与快照是 Play 调试的左右手:暂停管「时间停住」,快照管「时间倒回」。

本章与站内既有内容的分工

主题站内既有内容本节做什么
拷贝边界13.1 共享模型与拷贝边界进入 Play 的入口策略;本节管 Play 内的时间
命令栈10.1 命令栈与事务撤销管编辑操作;快照管模拟状态
多进程 PIE13.3 多进程 PIE单进程内暂停单步;多进程靠心跳与 IPC

暂停的两种推进:自动 vs 手动

PauseStepper 的推进分两条路:advance()(自动)和 step()(手动)。pause 期间 advance 是空操作——模拟被冻结;step 手动推进一格。两条路的分界正是暂停的语义:

非暂停:advance() 推进,step() 返回 false(应走 advance)
暂停后:advance() 冻结,step() 推进一格

step 在非暂停时返回 false 而不是静默推进,是一个关键设计:调用方必须明确自己在单步。错误地让 step 在非暂停时也推进,会让「暂停」形同虚设——用户以为停了,单步却在自动推进的时间上再叠加。显式拒绝让这类误用立即暴露。

快照:暂停点的记忆

单步调试最常见的操作是「单步两步、看状态、不满意、退回再看」。SnapshotStore 的 restore() 取最近一张快照——暂停瞬间存的,单步前的状态。save 连续存多张(每个暂停点一张),depth() 让「存了几张」可见。

快照与暂停配合的完整循环:

pause() → save(快照) → step() → step() → 状态不对 → restore() → 重新单步

「可控的 Play」= 暂停(时间停住)+ 快照(时间可回退)+ 单步(一格一格看)。三者缺一不可。

对标暂停、单步与状态回退

Unity 的 EditorApplication.Step() 在播放模式执行单帧。这个入口可对照本节的单步操作。helio 的 SnapshotStore 只恢复示例显式捕获的数据。要重试完整的一帧,还要考虑输入、时钟、随机数、物理内部状态和外部副作用是否能够一起恢复。 Unity EditorApplication.Step

可运行产物:6 条用例

下面 src/snapshot-pause-step.ts 层级 L1,完整可编译,命令 node --test test/13-2-snapshot-pause-step.test.ts:

// file: src/snapshot-pause-step.ts
// tier: L1
// verify: node --test test/13-2-snapshot-pause-step.test.ts
//
// Edit/Play 共享与隔离深化(第十三章):快照暂停单步。
// Play 模式要能暂停:暂停后模拟不再自动推进,但用户可以手动单步。
// PauseStepper 维护暂停状态与推进逻辑——pause 冻结自动推进,step 手动
// 推进一格。SnapshotStore 保存/恢复模拟快照:暂停瞬间存一份,用户单步
// 几步不满意,restore 回到暂停点。暂停与快照配合,才谈得上「可控的 Play」。

export class PauseStepper {
  #paused = false;
  #tick = 0;

  pause(): void { this.#paused = true; }
  resume(): void { this.#paused = false; }
  isPaused(): boolean { return this.#paused; }

  /** 非暂停时自动推进一格。 */
  advance(): void { if (!this.#paused) this.#tick += 1; }

  /** 暂停时手动推进一格;非暂停返回 false(应走 advance)。 */
  step(): boolean {
    if (!this.#paused) return false;
    this.#tick += 1;
    return true;
  }

  tick(): number { return this.#tick; }
}

export class SnapshotStore<T> {
  readonly #history: T[] = [];

  save(snapshot: T): void { this.#history.push(snapshot); }

  /** 取最近一张快照(不删除)。 */
  restore(): T | undefined { return this.#history.at(-1); }

  clear(): void { this.#history.length = 0; }
  depth(): number { return this.#history.length; }
}

advance 用 if (!this.#paused) 做冻结——暂停时推进是空操作而不是报错,因为自动推进来自模拟循环,循环不能因为暂停而中断,只需跳过。step 则是显式的 return false——手动单步必须让调用方知道「现在不是暂停态,你的单步没生效」。

可运行产物:6 条用例

下面是 test/13-2-snapshot-pause-step.test.ts 层级 L1,命令 node --test test/13-2-snapshot-pause-step.test.ts:

// file: test/13-2-snapshot-pause-step.test.ts
// tier: L1
// verify: node --test test/13-2-snapshot-pause-step.test.ts
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { PauseStepper, SnapshotStore } from '../src/snapshot-pause-step.ts';

test('非暂停时 advance 自动推进', () => {
  const s = new PauseStepper();
  s.advance();
  s.advance();
  assert.equal(s.tick(), 2);
});

test('pause 后 advance 不再推进', () => {
  const s = new PauseStepper();
  s.advance(); // tick=1
  s.pause();
  assert.equal(s.isPaused(), true);
  s.advance();
  s.advance();
  assert.equal(s.tick(), 1); // 冻结
});

test('step 暂停时手动推进一格', () => {
  const s = new PauseStepper();
  s.pause();
  assert.equal(s.step(), true);
  assert.equal(s.step(), true);
  assert.equal(s.tick(), 2);
});

test('step 非暂停时返回 false', () => {
  const s = new PauseStepper();
  assert.equal(s.step(), false);
  assert.equal(s.tick(), 0);
});

test('resume 恢复自动推进', () => {
  const s = new PauseStepper();
  s.pause();
  s.step(); // tick=1
  s.resume();
  assert.equal(s.isPaused(), false);
  s.advance(); // tick=2
  assert.equal(s.tick(), 2);
});

test('SnapshotStore 保存/恢复/清空', () => {
  const store = new SnapshotStore<{ tick: number }>();
  store.save({ tick: 1 });
  store.save({ tick: 5 });
  assert.deepEqual(store.restore(), { tick: 5 }); // 最近一张
  assert.equal(store.depth(), 2);
  store.clear();
  assert.equal(store.depth(), 0);
  assert.equal(store.restore(), undefined);
});

本机实测(Node v26.5.0,node --test test/13-2-snapshot-pause-step.test.ts):tests 6、pass 6、fail 0,耗时约 38 ms。

第二条验收暂停冻结:pause 后连续 advance 两次,tick 停在 1。第三条验收单步:暂停后 step() 两次,tick 到 2。第四条钉死边界:非暂停时 step 返回 false——单步只在暂停态有意义,误用立即暴露。第六条验收快照:多张保存取最近、clear 后 depth 归零。

小结

  • 暂停 = 时间停住(advance 冻结),单步 = 手动推进一格(step),resume 恢复。
  • step 在非暂停时显式返回 false:单步只在暂停态有意义,误用立即暴露。
  • 快照 = 暂停点的记忆:restore 回暂停点,单步过程可无限重试。
  • 对标主流引擎「无原生状态回退」:helio 的暂停点可回退是逐帧调试的质变。
  • 下一节把 Play 推出进程外:多进程 PIE——独立进程运行游戏、心跳检测存活、模式切换。

阅读导航:上一节:13.1 共享模型与拷贝边界 · 下一节:13.3 多进程 PIE 。

继续阅读

探索更多技术文章

浏览归档,发现更多关于系统设计、工具链和工程实践的内容。

全部文章 返回首页

「books」更多文章

  1. 《大规模分布式计算深入解析·向量数据库与 AI 检索内核》 · 目录与学习路径(45 篇)
  2. 《大规模分布式计算深入解析》15.3 自研向量引擎:可执行骨架与工程缺口
  3. 《大规模分布式计算深入解析》14.1 Milvus:组件架构、段管理与检索链路