Mass Battle Frameworkv1.20.0

三个世界,两座桥 Three Worlds, Two Bridges

RENDERING

引擎里的三个世界,和 MassBattle 在中间搭的两座桥 · 2026-12-01The engine's three worlds, and the two bridges MassBattle builds between them · 2026-12-01

系列这些文章写给人和他的 agent 一起看。这套文档怎么读 →SeriesThese posts are for you and your agent. How to read these docs →

三个平行世界 Three Worlds Side by Side

虚幻引擎底下其实是三套东西并排跑,各自管一摊。一套是 UObject:Actor、组件、蓝图、GC、复制都在这边,你平时写游戏代码基本都在这一层,身份就是一根指针,几千个就开始吃力。一套是粒子:Niagara,老一点的项目里是 Cascade,每颗粒子就是 GPU 上的一组属性——位置、颜色、年龄,没有反射也没有名字,几百万颗也不稀奇。还有一套是 MassEntity:实体是 chunk 里的一个下标,数据躺在 fragment 里,逻辑是 processor 扫查询,整层不碰 UObject,十万单位在双端之间逐位对齐地跑。

Under the hood Unreal runs three separate sets of machinery side by side. One is UObject: Actors, components, Blueprint, GC, replication. It is where you normally write game code, its idea of identity is a pointer, and a few thousand of them is where things start to hurt. One is particles: Niagara, or Cascade on older projects. A particle is a pile of attributes on the GPU — position, colour, age — with no reflection and no name, and a few million is nothing special. The third is MassEntity: an entity is an index inside a chunk, the data sits in fragments, the logic is processors sweeping a query, no UObject anywhere in the layer, and a hundred thousand units run bit-identical on both peers.

三套东西的底座对不上。UObject 的 GC 只认注册过的引用,chunk 里的内存它根本看不见;反过来,Mass 的 chunk 里也不许放裸 UObject 指针。粒子更绝,CPU 这边连"这是谁"都答不上来——瞄准一颗粒子?没有这回事。想让它们互相帮忙,只能专门搭桥,把数据按对方的格式递过去。

The three are built on foundations that do not match. The UObject GC only tracks registered references; memory inside a chunk is invisible to it. In the other direction, a Mass chunk is not allowed to hold a bare UObject pointer at all. Particles go further: on the CPU side there is no answer to "which one is this", so aiming at a particle is not a thing you can do. If they need to help each other, somebody has to carry the data across in the other side's format.

世界World代表Representative身份Identity数据住在Data lives in量级Scale
UObjectUObjectActor / 组件Actor, components指针,GC 管A pointer, GC-ownedUObject 堆The UObject heap千级Thousands
粒子ParticleNiagara / CascadeNiagara, Cascade发射器里的下标An index in an emitterGPU 粒子缓冲GPU particle buffers百万级Millions
MassEntityMassEntityEntity / Fragment / ProcessorEntity, fragment, processor(Index, 世代) 手柄An (index, generation) handleArchetype chunkArchetype chunks十万级,双端一致Tens of thousands, bit-identical

MassBattle 搭了两座。一座往粒子那边,让实体被看见;一座往 UObject 那边,让实体和 Actor 对上号。

MassBattle builds two. One runs toward the particle world so entities can be seen. One runs toward UObject so entities and Actors can find each other.

先解决"看得见" First: Being Seen

先说显示。十万个单位,一个单位一个 Actor 是行不通的——光 Actor 和组件的 tick 就把帧吃光了,所以渲染这条路完全不碰 Actor。场景里只放一个 AMassBattleAgentRenderer,它自己是个 Actor,但只是个壳。壳里管着一批一批的 Niagara 组件,默认每批 5000 个槽位,一个槽位站一个单位。槽位 i 的属性,就是第 i 个实体这一帧的样子。

Start with getting them on screen. One Actor per unit will not work — ticking the actors and their components alone eats the frame — so this path does not touch Actors at all. The level holds a single AMassBattleAgentRenderer. It is an Actor itself, but only a shell. The shell owns batches of Niagara components, 5000 slots per batch by default, one unit per slot. The attributes of slot i are how entity i looks this frame.

每帧,Agent Render Processor 把实体并行扫一遍,把这一帧要显示的东西填进槽位数组:位置、朝向、缩放、Mesh 下标、样式、动画轨道、DynamicParams、血条比例、可见性。填完把整组数组推给 Niagara 组件,走的是 SetNiagaraArrayVector、SetNiagaraArrayQuat、SetNiagaraArrayInt32 这些接口。到了 Niagara 那边,数组就是粒子属性——读第 i 颗粒子的 Location,拿到的就是第 i 个实体的位置。骨骼档多走一步:每批一对 RenderTarget 加一个计算着色器,槽位里放解算的输入,蒙皮整个在 GPU 上做。

Each frame the Agent Render Processor walks the entities in parallel and fills the slot arrays: location, orientation, scale, mesh index, style, animation tracks, dynamic params, health-bar ratio, visibility. Then it pushes the whole set onto the batch's Niagara component — SetNiagaraArrayVector, SetNiagaraArrayQuat, SetNiagaraArrayInt32 and friends. On the Niagara side those arrays are the particle attributes: read Location of particle i and you get entity i's position. The bone tier goes one step further — one render-target pair and one compute shader per batch, the slots carrying the solve inputs, and skinning happening entirely on the GPU.

看看真正送过去的是什么:没有 entity handle,没有 fragment,也没有行为,只有这个单位此刻长什么样。对面拿到的就这些,需要的也就这些。骨骼、顶点动画、图集三种画法共用这座桥,每个槽位按自己的 LOD 档位挑画法;近到一定程度整档换成真的 Actor 来画,那是另一座桥的事。反方向没有流量:粒子从不写回实体,渲染帧和逻辑帧之间用 ElapsedSinceLogicTick 插值,连批的 tick 都是 MassBattle 子系统统一推的,渲染器不挂引擎 tick。

Look at what actually crosses: no entity handle, no fragment, no behaviour — only how the unit looks right now. That is all the far side gets, and all it needs. Bones, vertex animation and atlas share this bridge, each slot picking its path by its own LOD band; when a band gets close enough it switches wholesale to a real Actor, which is the other bridge's job. Nothing flows back. A particle never writes into an entity, render frames interpolate between logic frames on ElapsedSinceLogicTick, and even the batches are ticked by the MassBattle subsystem instead of the engine, because the tempo here belongs to the simulation.

再解决"对得上话" Then: Getting the Two Sides Talking

你在关卡里摆了个 Actor,模拟里跑的是一个实体,得有人把它们对上号,这个人就是 MassBattleAgentComponent。Auto 模式下 BeginPlay 建实体,Manual 模式你自己挑时机;fragment 的初值(模板数据)直接在组件上编辑;之后每帧按同步模式把实体变换写回 Actor。联机时组件上有个会复制的 LockstepNetID,服务器下命令靠它找到实体;快照恢复把实体重建之后,组件按 UID 再绑回去。

You place an Actor in the level, the simulation runs an entity, and MassBattleAgentComponent is what pairs them. In Auto mode the entity is spawned on BeginPlay, in Manual mode you pick the moment; the fragment initial values (template data) are edited right on the component; after that the entity's transform is written back to the Actor each frame according to the sync mode. In a networked session the component carries a replicated LockstepNetID, which is how a server command finds its entity, and when a snapshot restore rebuilds the entity the component re-binds by UID.

Obstacle 组件是反着用的。你在 UObject 这边摆 Box、Capsule、Spline 碰撞体,组件把它们注册成 Mass 那边的障碍实体,流场和网格吃这份数据。动态障碍跟着 Actor 走,通过 Obstacle.Spawn、SyncTransform、Destroy 三条命令同步过去。编辑体验留在你熟悉的那边,干活的是另一边。

The obstacle components run the other way. You lay out box, capsule and spline collision shapes in the UObject world, the components register them as obstacle entities, and the flow field and the grid consume that. A dynamic obstacle follows its Actor through three commands — Obstacle.Spawn, SyncTransform, Destroy. Authoring stays where you are used to it; the work happens on the other side.

模拟跑起来之后,发生的事得传出来:谁出生了,谁开始索敌,谁打了一发,谁挨了,谁死了。这些事件在并行段收集,合并成队列,回到游戏线程依次派发到 Actor 的接口实现上,就是 IMassBattleAgentInterface 那串 OnXxx。这些函数全都没有返回值。这是故意的:Actor 这边只能看、只能反应,改不了模拟。想让实体做什么,走命令,别从事件里往回伸手。

Once the simulation is running, what happens there has to get out: who appeared, who started tracing, who fired, who got hit, who died. The events are collected in parallel, merged into queues, and dispatched back on the game thread to the Actor's interface implementation — the IMassBattleAgentInterface OnXxx set. None of those functions returns anything. That is on purpose: the Actor side can watch and respond, it cannot change the simulation. If you want an entity to do something, send a command; do not reach back through the events.

还有一种情况:模拟里真的要产生一个 UObject,比如生成一个 Actor、播一条音效。这类请求排进 Host 子系统的队列,池子按类型分开,Actor、音效、特效各管各的,闲置超时才销毁。生成出来的 Actor 会收到 IMassBattleActorInterface::OnActorSpawn,参数里带着 OwnerEntity,一出生就知道自己替哪个实体干活。

There is one more case: the simulation really does need a UObject — spawn an Actor, play a sound. Those requests queue into the Host subsystem, whose pools are split by kind, Actors and sounds and FX each on their own, destroyed only after an idle timeout. The spawned Actor gets IMassBattleActorInterface::OnActorSpawn with its OwnerEntity, so it knows from birth which entity it is working for.

能长期并存的几条死规矩 What Keeps It Working

两座桥能一直这么用下去,是因为规矩定得死。第一,方向单一:状态只从模拟往外流,画面和观察者只读。想往模拟里写,只有命令层一条路,输入、出生、变换同步全走那儿,因为命令能同步、能重放、能进哈希。第二,只搬值:两边谁也不持有对方的内存。槽位号会复用,Actor 指针会被池回收,entity handle 在快照重建后要换世代号,这些都不是稳定身份,稳定身份只有 UID。第三,别贪宽:渲染桥不送行为状态,事件桥不送 fragment。

The two bridges keep working because we nailed the rules down hard. One, a single direction: state flows out of the simulation, and pictures and observers read it. Writing back in has exactly one legal route, the command layer — input, spawning, transform sync — because commands can be synchronised, replayed and hashed. Two, pass values only: neither side holds the other's memory. Slot indices get reused, Actor pointers get recycled by pools, and entity handles get a new generation after a snapshot restore. None of those is a stable identity; the UID is. Three, do not get greedy: the render bridge carries no behaviour state, the event bridge carries no fragments.

桥一宽,两个世界就糊成一锅粥:GC 得去扫 chunk,GPU 得等 CPU,确定性被表现层拖着走。十万单位能跑起来,靠的就是每座桥只搬自己那一行数,搬完就停。

Widen a bridge and the two worlds melt into one pot: the GC would have to scan chunks, the GPU would wait on the CPU, and determinism would get dragged around by the presentation layer. The hundred thousand units only work because each bridge carries its own line of data and stops there.

新东西先想清楚放哪边 Where a New Piece Belongs

这个三世界的格局是虚幻引擎自己长出来的。引擎本身也有几座官方的桥:动画转贴图、Niagara 数据通道、Mass 的 Actor 表示。我们能做的只是认下这个格局,把每座桥修到能过十万单位:渲染按批搬,事件按队列搬,生成按池搬。

The three-world arrangement is Unreal's own shape. The engine already ships official bridges of its own: animation-to-texture, Niagara data channels, Mass's Actor representation. All we could do was accept the shape and widen each bridge enough for a hundred thousand units. Rendering moves by batch. Events move by queue. Spawning moves by pool.

所以以后加新能力,先问两个问题:它属于哪一边?非过桥不可吗?能不过就别过,把数据留在模拟里,只把结果递出来。真要修新桥,上面几条规矩照抄,别把两边焊死。

So when a new capability shows up, ask two questions first: which side does it belong to, and does it have to cross at all? If it can stay, it stays — keep the data in the simulation and hand out only the result. If a new bridge is genuinely needed, follow the same rules, and do not weld the two sides together.

Leroy Works · Mass Battle Framework 作者
联系:Discord · QQ 群 916358710 · Fab · 全部联系方式

Leroy Works — author of Mass Battle Framework
Contact: Discord · QQ group 916358710 · Fab · all channels