博客 Blog
设计理念 · 取舍 · 为什么不那样做Design ideas · trade-offs · why not the other way
这些文章写给你和你的 agent 一起看:为什么这么设计、放弃了哪些做法、哪些取舍取决于你的玩法而不是框架。目的始终是同一个——读完知道这套东西为什么长这样、哪些规矩不能破,然后自己动手往上写。上手前该先读什么、怎么把这些合成为一份自己的 skill,放在让 Coding Agent 改源码的开头。
These posts are written for you and your agent to read together: why it is designed this way, what we gave up, and which trade-offs belong to your game rather than to the framework. The aim is always the same — come away knowing why this is shaped the way it is and which rules cannot be broken, then write on top of it yourself. What to read before you start, and how to fold it into a skill of your own, opens Letting a Coding Agent Edit the Source.
文章 Posts
三个世界,两座桥 Three Worlds, Two Bridges
引擎底下是三套互不相通的东西:Actor 的 UObject 世界、Niagara 的粒子世界、MassEntity 的实体世界。MassBattle 在中间搭了两座桥:一座让实体变成粒子属性被看见(每批 5000 槽,一组 SetNiagaraArray 推过去),一座让 Actor 和实体对上号——摆放、观察、生成都走它。最后是让桥一直走得通的几条死规矩。Under the engine sit three worlds that cannot touch each other: the UObject world of Actors, the Niagara world of particles, the MassEntity world of entities. MassBattle builds two bridges between them: one turns entities into particle attributes so they can be seen (5000 slots a batch, pushed over as SetNiagaraArray calls), one pairs Actors with entities so you can place, observe and spawn. Plus the few hard rules that keep the bridges standing.
MassEntity 与 Niagara:
同一个想法的两种写法 MassEntity and Niagara: One Idea, Two Spellings
MassEntity 和 Niagara 拆开看是同一个设计:处理器对 module,实体句柄对粒子 ID,连两个 int 的布局都一样。差别在 Mass 多出原型和查询,而 Niagara 把一份图编到 CPU 和 GPU 两个后端。Strip the names away and MassEntity and Niagara are the same design: processors against modules, entity handles against particle ids, down to the same two-int layout. Mass adds archetypes and queries; Niagara compiles one graph to two backends.
逻辑在 CPU,画在 GPU Logic on the CPU, Pictures on the GPU
逻辑跑在 CPU,画面交给 GPU,这条线把两边分开:一边是确定性和随手可读的模拟数据,一边是可以随便调、GPU 还能代算的渲染。合批让一百颗子弹共用一组数组,代价是每个实例不再自带逻辑。Logic runs on the CPU, pictures on the GPU, and that line splits the work: determinism and readable simulation data on one side, tunable rendering that can lean on GPU compute on the other. Batching puts a hundred bullets into one set of arrays; what it costs is per-instance logic.
合批渲染:一千个东西,一个组件 Batched Rendering: A Thousand Things, One Component
框架里没有"一个东西一个组件"的画法:一个 Niagara 组件管一批槽位,每帧一次数组推送。单位(5000 槽,网格、血条、动画都在里面)、伤害数字(走 NDC)、子弹与爆炸(SubType 选场景渲染器、StyleType 选 NS 内渲染器)都走这台机器。Nothing here draws one component per object: one Niagara component covers a batch of slots and takes one array push a frame. Units (5000 slots, mesh and health bar included), damage numbers (via NDC), bullets and explosions (SubType picks the level renderer, StyleType the one inside the NS) all ride the same machine.
一个单位二三十个材质球 Thirty Material Slots per Unit
一套男女角色资产烘成一个单位,二三十个材质槽,每个槽还挂着自己的母材质,五千单位时 5070 Ti 就顶不住了。合网格只降网格数,槽数得靠图集合;统一母材质省的是 cook 时间,帧数一分不省。A men-and-women pack baked into one unit lands at twenty or thirty material slots, each with its own parent, and a 5070 Ti is already struggling at five thousand units. Merging meshes cuts only the mesh count; the slots need an atlas, and one shared parent saves cook time, not frame time.
一个 Agent = 车体 + 炮塔 An Agent Is a Chassis and a Turret
一个 Agent 就是车体加炮塔,坦克、弓箭手、骑手甚至一栋建筑都能套进去,差别只在当前开了哪些开关。片段不切成 trait 是权衡过的选择:省下的内存抵不上多出来的空指针判断,配置差异改用面板开关吸收,运行时保持同构。An agent is a chassis plus a turret: tanks, archers, riders, even a building all fit, and the only difference is which switches are on. Keeping every fragment on every agent is a deliberate trade: the saved memory would not cover the null checks it would add, so panel switches absorb the configuration difference and run time stays homogeneous.
Projectile 是"一个东西" A Projectile Is a Thing, Not a Pellet
Agent 是会想的身体,Projectile 是会撞的东西,Loot 会被捡的战利品。判断标准只有一条:它需不需要“想”;需要就是 Agent,不需要就交给东西。An agent is a body that thinks, a projectile is a thing that collides, loot is what you pick up. One question decides the type: does it need to think? If yes, it is an agent; if not, it is a thing.
一帧之内:因果链与同步点 Within One Frame: Causal Chains and Sync Points
一个处理器就是一趟并行波,波尾是同步点,处理器之间的顺序靠波的边界保证。生成物需要更强的那个同步点,所以阶段切在哪,看的是下一段要不要读上一段刚生出来的实体。One processor is one parallel wave whose end is a sync point, and those boundaries are what carry order between processors. Spawned entities need the stronger sync point, so a stage gets cut wherever the next section has to read entities the previous one just produced.
能力、行为、反应:三层与一条顺序 Capability, Behaviour, Reaction
单位 AI 拆成三层:能力层只回答它能做什么,行为层同一时间只跑一个,反应层由事件推进、能和别的反应并存。一帧里三层按固定顺序串起来,新功能放哪层,先问它属于哪一类,再问它读的数据是谁写的。Unit AI is three layers: capability answers what the unit can do, behaviour runs one at a time, reaction is driven by events and can coexist with the rest. The three run in a fixed order inside a tick, and a new feature is placed by asking which layer it belongs to and who writes the data it reads.
四层,各自解决别人解决不了的事 Four Layers, Each Solving What the Others Cannot
移动按意图、交通规则、避障、物理四层走,每一层做完把结论交给下一层。避障解出来的速度不保证真的避开,所以物理必须最后说话;顺序换一下,问题马上看得见。Movement runs intent, traffic rules, avoidance, physics, each layer handing its answer to the next. An avoidance velocity does not guarantee avoidance, so physics has to speak last; change the order and the failure is immediate.
物理是引擎算出来的错觉 Physics Is an Illusion the Engine Computes
游戏里的碰撞、移动、物理都是引擎用数据算出来的,物体本身并不带这些性质。要上十万级,就得自己把空间检测、移动、避障、物理解算从头写一套,Agent 怎么想另有三条路。Collision, motion and physics in a game are computed from data; the object does not carry them. At a hundred thousand units you write that set yourself — spatial detection, movement, avoidance, physics solving — and the agents' thinking takes one of three roads.
事件不是免费的 Events Are Not Free
“事件快、tick 慢”只有在一个前提下成立:事件得能替你省掉真正贵的那部分。overlap 这类事件最贵的就是每步判定,做成事件只是把结果分了类,判定照付,所以在 ECS 里每 tick 调一次形状查询往往更划算。空间检测同理:MassBattle 给的是可随时调的查询函数,没有范围事件,块级剪枝把整块不匹配的实体一次跳掉。“Events are faster than tick” holds only when the event lets you skip the work that actually costs. For overlap the expensive part is deciding every step, and an event just sorts the result, so in an ECS a shape query called per tick is often the better deal. The same goes for spatial detection: MassBattle gives you callable query functions, no range events, and block-level pruning drops whole blocks that cannot match.
粒度的边界:为什么没有阵型 Actor Where We Stop: Why There Is No Formation Actor
MassBattle 给阵型算法和检测函数,不给替你管队伍的 FormationActor。队是什么、什么时候重编,各家游戏答案不同,那一层留给你的游戏。MassBattle ships formation algorithms and trace functions, not a FormationActor that runs your squads. What a squad is and when it regroups differ per game, so that layer belongs to your game.
让 Coding Agent 改源码 Letting a Coding Agent Edit the Source
面板给作者填数字,写逻辑应该回到源码。一个能读源码、能跑构建的 agent 让“扩展插件”第一次可以交给别人做,前提是把写法写清楚:分层、成对片段、bEnable 默认关、模板行配启动断言、跨帧字段进快照。不想动插件也行:注册管线能停用默认处理器、换上自己模块里的实现,升级不丢改动。Panels are for filling in numbers; logic belongs in source. An agent that can read the source and run the build makes extending the plugin outsourceable for the first time, provided the shape is spelled out: layers, fragment pairs, bEnable off by default, a template line with a startup assert, and cross-frame fields in the snapshot. And the plugin need not be touched: the registration pipeline can disable a stock processor and swap in your own module's implementation, which survives an update.
标记该用 Tag 还是 Flag Tag or Flag: Filter or Test
tag 是原型级过滤,flag 是实体级测试:查询能整块跳过的才值得做成 tag,状态大多只该有 flag。三个问题决定要不要双注册;另外两件事也要知道,按名字用 flag 只在蓝图里划算,块级剪枝默认关着是为了省性能。A tag filters at the archetype level, a flag tests per entity: only something queries can skip wholesale earns a tag, and most states should be flags only. Three questions decide dual registration, and two details matter: by-name flags pay off in Blueprint only, and block-level pruning ships off to save performance.
Fragment 存值,Tag 给查 A Fragment Holds the Value, a Tag Makes It Queryable
队伍索引存了两份:fragment 里存值,一组 256 个空 tag 让查询能按类型筛。我们每帧比较当前索引和上一帧的值来搬 tag,框架内部一个 tag 都不查,它们叫什么、代表什么由你定。A team index is stored twice: the fragment holds the value, and 256 empty tags make it queryable by type. We move the tag whenever the index differs from the previous frame, the framework itself queries none of them, and their meaning is yours to define.
处理器该切多细 How Fine Should a Processor Be?
处理器切粗,是因为每趟遍历都有固定代价,而细切要靠频繁改 tag 才能省下工作。tag 一换就要搬实体、帧时间跟着抖,所以我们少用 tag、一趟里多做几件事。Processors are cut coarsely because every pass has a fixed cost, and fine cuts only save work when tags change often. A tag change migrates the entity and shakes frame time, so we use fewer tags and do more inside one pass.
Mass Entity 上的线程安全 Thread Safety on Mass Entity
并行波里只写数据,改结构的事全收集起来走串行,跨实体只读本波之前写好的副本。判据一句话:这一行是在写数据,还是在动引擎维护的那套结构。Inside a wave, write data only, gather every structural change for the serial part, and cross-read only copies finished before the wave. The test is one question: is this line writing data, or moving structure the engine maintains.
GC 看不见你的 chunk The GC Cannot See Your Chunks
片段的 chunk 内存不进 GC 图,放进去的 UObject 指针没人担保,对象随时可能被回收。软路径当权威,片段外用 TStrongObjectPtr 钉住并在世界结束时清表,片段里那份指针只当缓存。The GC never sees chunk memory, so a UObject pointer stored in a fragment has nobody vouching for its target. Keep a soft path as the authority, pin the object with TStrongObjectPtr in a holder outside the fragment, clear the tables when the world ends, and treat the pointer inside the fragment as a cache.
指针还在,数据不是它的了 The Pointer Survives; the Data Does Not
缓存一个 fragment 指针,加/减 tag 之后再写它,值会安静地不生效。加/减标记就是换原型,实体已经搬家,指针还指着旧位置。Cache a fragment pointer, add or remove a tag, then write through it: the value quietly does not take. Changing a marker swaps the archetype, so the entity has moved and the pointer still holds the old address.
确定性锁步:只传走法,不传棋盘 Deterministic Lockstep: Send the Moves, Not the Board
网络上传的只有走法和一份棋谱指纹:同样的规则、同样的顺序、同样的输入,每台机器各自算出同一个世界,每帧用哈希互相盯着。附可交互的时间线演示:调延迟、把客户端推到滞后,亲眼看迟到之后暂停、快照、追帧的完整恢复链。Only the moves and a fingerprint of the position cross the wire: same rules, same order, same inputs, each machine derives the same world and hashes keep watch every frame. Includes a hands-on timeline: raise the delay, push a client out of step, and watch the full late-command recovery chain — pause, snapshot, fast-forward.
多线程确定性是怎么做到的 How a Parallel Simulation Stays Deterministic
锁步里两端各算各的,所以确定性收窄成一句话:同样的输入、种子、二进制和硬件,两个进程跑出逐位一致的状态。并行本身是安全的;真正要防的是顺序、共享写入和未定义的值,把它们变成不变量就是整篇的做法。In lockstep every peer computes on its own, so determinism narrows to one claim: same input, seed, binary and hardware, bit-identical state in both processes. Parallel passes are safe on their own; the damage comes from order, shared writes and undefined values, and the post turns each of those into an invariant.
浮点确定性与定点确定性 Float vs Fixed-Point Determinism
浮点确定性快,跟引擎天然一致,但只在同一平台、同一份构建上成立;跨平台要定点,代价是运算全部拿整数模拟。Q31.32 数学库和移动处理器的定点版都已经验过,完整重构排在功能定型之后,跟多平台支持同一趟做。Float determinism is fast and agrees with the engine by construction, but it holds only on one platform and one build; crossing platforms needs fixed point, paid for with integer emulation. The Q31.32 math library and a fixed-point movement processor are already proven, and the full refactor waits until features settle, travelling with multi-platform support.
失步怎么查 Finding a Desync
失步报警的那一帧,几乎不是开始出错的那一帧,所以排查是从哈希往回倒推。先分清是重同步之后还是自己跑偏,再用栅栏、逐实体哈希和全量转储收到具体字段;每查完一条,它就变成一条写进代码的纪律。The tick that reports a desync is almost never the tick that broke, so the work is to walk a hash back to its first divergence. Classify the desync first, then use the barrier, per-entity hashes and a full dump to close in on the field; each hunt ends as a rule that goes into the code.