UMassBattleLootComponent
Source/MassBattle/Public/Components/MassBattleLootComponent.h
概述 Overview
把 Actor 绑定到 Mass loot(战利品)实体的组件,派生自 UMassBattleEntityComponentBase。与 Agent 组件镜像对称:从 LootConfigAsset 经 UMassBattleLootSubsystem::MakeLootTemplateFromDataAsset 构建模板并生成实体(含弹道/插值两种出生运动、分数乘数、事件接收者绑定);bLockstepNetworked 时走服务器驱动的 LootComponent.Spawn 锁步指令路径;每帧 OnEntityTick 轮询 FLootParamsRT.State 状态机,在 Collected/Expired 边沿广播事件;快照恢复后按 OwningLootUniqueID 重链。事件通过实体 FLootEventRT.EventReceiveActor 路由(组件查找也走该字段)。
Binds an Actor to a Mass loot entity; derived from UMassBattleEntityComponentBase. Mirror-symmetric with the Agent component: builds the template from LootConfigAsset via UMassBattleLootSubsystem::MakeLootTemplateFromDataAsset and spawns the entity (ballistic/interped birth movement, score multipliers, event receiver binding); when bLockstepNetworked routes through the server-driven LootComponent.Spawn lockstep command path; each frame OnEntityTick polls the FLootParamsRT.State state machine and broadcasts on Collected/Expired edges; after snapshot restore re-links by OwningLootUniqueID. Events route through the entity's FLootEventRT.EventReceiveActor (component lookup also goes through that field).
继承 Inheritance
公开方法 Public Methods
Loot 版本的三向(组件↔实体↔Actor)变换同步(Blueprint 名 "Sync Transform Entity To Component")。前置与 Agent 版本相同(同步模式非全 None、相对变换已捕获、片段有效)。执行顺序:位置 → 旋转 → 缩放;实体局部偏移随实体当前朝向旋转;ComponentToEntity 写实体时无 FMoving 门控(Loot 无 FMoving —— 但 Interped 移动变体可能重写位置,C2E 仅适用于非插值驱动);联机锁步模式下跳过任意轴 ComponentToEntity 写回(实体变换由锁步指令下发);FF 期间 UpdateFastForwardState 切换 ReplicateMovement,EffectiveSyncMode 把 ComponentToEntity 映射为 EntityToActor。累积后经 SetWorldTransform/SetActorTransform 原子应用。
Loot version of the three-way (component↔entity↔actor) transform sync (Blueprint name "Sync Transform Entity To Component"). Same preconditions as the Agent version (sync modes not all None, relative transforms captured, fragments valid). Order: location → rotation → scale; entity-local offsets rotate with the entity's current orientation; ComponentToEntity writes have no FMoving gate (Loot has no FMoving — but the Interped movement variants may rewrite location, so C2E applies only to non-interp-driven loot); in networked lockstep mode any-axis ComponentToEntity write-back is skipped (entity transform arrives via the lockstep command); during FF, UpdateFastForwardState toggles ReplicateMovement and EffectiveSyncMode maps ComponentToEntity to EntityToActor. Accumulated updates apply atomically via SetWorldTransform/SetActorTransform.
Input
| 参数 Param | 类型 Type | 说明 Description |
|---|---|---|
InDeltaTime | float | 时间步长(秒),当前实现未使用。Delta time in seconds; currently unused. |
Output
无返回值。副作用:按同步方向写 FLocating/FRotating/FScaling 或组件/Actor 变换;切换 Owner ReplicateMovement。
No return value. Side effects: writes FLocating/FRotating/FScaling or component/actor transforms per direction; toggles the Owner's ReplicateMovement.
从实体句柄反查绑定组件。Loot 没有 FVisualizing,改为读 FLootEventRT.EventReceiveActor(必须非空),再在该 Actor 上按类查找 UMassBattleLootComponent。任何环节失败返回 nullptr。
Looks up the bound component from an entity handle. Loot has no FVisualizing, so it reads FLootEventRT.EventReceiveActor (must be non-null) and searches that Actor for a UMassBattleLootComponent. Returns nullptr when any step fails.
Input
| 参数 Param | 类型 Type | 说明 Description |
|---|---|---|
WorldContext | UObject* | 世界上下文(DefaultToSelf)。World context (DefaultToSelf). |
EntityHandle | FEntityHandle | 要查找的实体句柄。The entity handle to look up. |
Output
返回绑定组件,未找到或无效时 nullptr。
Returns the bound component, or nullptr when not found or invalid.
手动初始化并生成 loot 实体。已初始化时警告并返回 true;锁步组件走服务器驱动路径(有权威调用 RequestLockstepSpawn,客户端返回 false);本地路径:构建模板并把组件变换盖到模板的 FLocating/FRotating 后调用 SpawnLootFromTemplate。
Manually initializes and spawns the loot entity. Already-initialized: warns and returns true; lockstep components route through the server-driven path (authority calls RequestLockstepSpawn, clients return false); local path: builds the template, stamps the component transform into the template's FLocating/FRotating, then calls SpawnLootFromTemplate.
Input
无参数。
No parameters.
Output
返回是否成功;失败原因见 Warning 日志。
Returns success; failure reasons are in Warning logs.
读取实体当前 loot 状态(FLootParamsRT.State:Spawning / Ready / BeingCollected / Collected / Expired)。实体无效或缺少片段时返回 Expired。
Reads the entity's current loot state (FLootParamsRT.State: Spawning / Ready / BeingCollected / Collected / Expired). Returns Expired when the entity is invalid or the fragment is missing.
Input
无参数。
No parameters.
Output
返回当前状态;无效时 Expired。无副作用。
Returns the current state; Expired when invalid. No side effects.
读取实体的分数值(FLootParams.ScoreValue,生成时已应用分数乘数)。实体无效或缺少片段时返回 0。
Reads the entity's score value (FLootParams.ScoreValue, score multiplier applied at spawn). Returns 0 when the entity is invalid or the fragment is missing.
Input
无参数。
No parameters.
Output
返回分数值。无副作用。
Returns the score value. No side effects.
读取实体的唯一 ID(FDeterminism.UniqueID)。实体无效或缺少片段时返回 -1。
Reads the entity's unique ID (FDeterminism.UniqueID). Returns -1 when the entity is invalid or the fragment is missing.
Input
无参数。
No parameters.
Output
返回唯一 ID;-1 = 无效。
Returns the unique ID; -1 = invalid.
惰性访问器:缓存命中直接返回;否则经 GetWorld() 取 UMassBattleLootSubsystem 并缓存。
Lazy accessor: returns the cache on hit; otherwise fetches UMassBattleLootSubsystem from GetWorld() and caches it.
Input
无参数。
No parameters.
Output
返回子系统指针(世界缺失时可能为 nullptr)。副作用:写缓存。
Returns the subsystem pointer (may be nullptr without a world). Side effect: fills the cache.
用既有实体初始化组件(对象池/重绑路径):已绑定则直接返回。写入 FLootEventRT.EventReceiveActor = GetOwner();保存并恢复用户同步模式后捕获初始相对变换;从实体同步 PreviousLootState;清 bPendingRebind;广播 OnLootInitialized。
Initializes the component with an existing entity (pooling/rebind path): returns when already bound. Writes FLootEventRT.EventReceiveActor = GetOwner(); captures initial relative transforms around a save/restore of the user sync modes; syncs PreviousLootState from the entity; clears bPendingRebind; broadcasts OnLootInitialized.
Input
| 参数 Param | 类型 Type | 说明 Description |
|---|---|---|
InEntityHandle | FEntityHandle | 要绑定的实体。The entity to bind. |
Output
无返回值。副作用:事件接收者写入、相对变换捕获、事件广播。
No return value. Side effects: event-receiver write, relative-transform capture, event broadcast.
解绑实体:实体仍有效且 FLootEventRT.EventReceiveActor 指向本 owner 时清空(防止悬垂);清零句柄与 bIsMassInitialized;同步模式重置为 None;PreviousLootState 复位为 Spawning。
Unbinds the entity: while the entity is valid, clears FLootEventRT.EventReceiveActor when it points at this owner (prevents dangling); zeroes the handle and bIsMassInitialized; resets sync modes to None; resets PreviousLootState to Spawning.
Input
无参数。
No parameters.
Output
无返回值。副作用:上述解绑与复位。
No return value. Side effects: the unbinding and resets above.
仅服务器入口:构建并广播 LootComponent.Spawn 锁步指令(镜像 Projectile)。载荷:IntPayload = [LockstepNetID]、NamePayload = [DataAsset 路径]、FloatPayload = [位置 XYZ, 四元数 XYZW](组件位置即出生点,无需脚部偏移换算)。经 SendLockstepCommandToClients 广播并本地入队。
Server-only entry: builds and broadcasts the LootComponent.Spawn lockstep command (mirrors Projectile). Payload: IntPayload = [LockstepNetID], NamePayload = [DataAsset path], FloatPayload = [location XYZ, quaternion XYZW] (the component location is the spawn point — no foot-offset conversion). Broadcasts and enqueues locally via SendLockstepCommandToClients.
Input
无参数。
No parameters.
Output
无返回值。副作用:广播一条锁步生成指令。
No return value. Side effect: broadcasts one lockstep spawn command.
锁步指令执行路径(镜像 Projectile):两端同一 ExecuteTick 调用,实体 UID 本地分配(ExpectedUniqueID 未使用 —— 本地分配即锁步身份)。已初始化时直接返回 true(幂等 —— 快照重建/重传);从指令载荷覆写模板变换(位置精确)后走 SpawnLootFromTemplate,成功则从实体同步 OwningLootUniqueID。
Lockstep command-execution path (mirrors Projectile): called on both peers at the same ExecuteTick; the entity UID is allocated locally (ExpectedUniqueID unused — local allocation IS the lockstep identity). Already-initialized returns true immediately (idempotent — snapshot rebuild / re-delivery); overrides the template transform from the command payload (position accuracy) then runs SpawnLootFromTemplate, syncing OwningLootUniqueID from the entity on success.
Input
| 参数 Param | 类型 Type | 说明 Description |
|---|---|---|
ExpectedUniqueID | int32 | 期望 UID(当前未使用)。Expected UID (currently unused). |
SpawnTransform | const FTransform& | 生成变换(来自指令载荷)。Spawn transform (from the command payload). |
Output
返回是否生成成功。副作用:实体生成、UID 缓存写入。
Returns spawn success. Side effects: entity spawn, UID cache write.
快照恢复后的重链实现:无锁步契约(OwningLootUniqueID < 0)时直接返回。按生成时捕获的 UID 经 MB->FindEntityByUID 反查重建实体;未找到则置 bPendingRebind = true 每帧重试。找到后:保存同步模式 → DeinitializeFromEntity(无前置检查,链接中调用安全)→ InitializeWithEntity → 恢复同步模式。
Rebind implementation after snapshot restore: returns immediately without a lockstep contract (OwningLootUniqueID < 0). Looks up the rebuilt entity by the UID captured at spawn via MB->FindEntityByUID; not found sets bPendingRebind = true for per-frame retry. On success: saves sync modes → DeinitializeFromEntity (no precondition guard — safe while linked) → InitializeWithEntity → restores sync modes.
Input
无参数。
No parameters.
Output
无返回值。副作用:重绑与事件广播。
No return value. Side effects: rebinding and event broadcast.
从 LootConfigAsset 经 UMassBattleLootSubsystem::MakeLootTemplateFromDataAsset 构建 archetype 模板(含 FLootVisualizing —— 此前内联模板缺它导致实体掉出 LootMonoProcessor 查询、冻结在 Spawning,standalone 缺陷已修)。资产缺失或子系统获取失败时返回无效模板。
Builds the archetype template from LootConfigAsset via UMassBattleLootSubsystem::MakeLootTemplateFromDataAsset (includes FLootVisualizing — the previous inline template lacked it, dropping entities out of the LootMonoProcessor query and freezing them in Spawning; standalone defect fixed here). Returns an invalid template when the asset is missing or the subsystem cannot be fetched.
Input
无参数。
No parameters.
Output
返回 FEntityTemplateData;失败时 IsValid() 为 false。
Returns FEntityTemplateData; IsValid() false on failure.
从模板同步生成 loot 实体(本地路径与锁步路径共用)。流程:复制模板 → FDeterminism 以 UMassBattleSubsystem::AllocNextUID() 的 UID SeedAtSpawn(锁步下两端同一 ExecuteTick 分配相同 UID)→ 应用 Multipliers.Ballistic.ScaleMult 缩放与 ScoreMult 分数 → 重置运行时状态(FLootParamsRT:Spawner=本组件 SpawnerEntity、State=Spawning、收集延迟、bBornFired=false)→ 启用 FLootEvent 并绑定接收者 → 按 SpawnMovementMode 初始化弹道(随机方向/速度,Z 向范围,经确定性随机流)或插值(FromPoint/ToPoint/偏移乘数)移动运行时 → MA->BuildEntity 同步构建 → 注册 UID 映射、捕获相对变换、广播 OnLootInitialized。
Spawns the loot entity synchronously from the template (shared by the local and lockstep paths). Flow: copies the template → seeds FDeterminism via SeedAtSpawn with a UID from UMassBattleSubsystem::AllocNextUID() (lockstep: both peers allocate the same UID at the same ExecuteTick) → applies Multipliers.Ballistic.ScaleMult scale and ScoreMult score → resets runtime state (FLootParamsRT: Spawner = this component's SpawnerEntity, State=Spawning, collect delay, bBornFired=false) → enables FLootEvent and binds the receiver → per SpawnMovementMode initializes ballistic (random direction/velocity, Z range, via the deterministic random stream) or interped (FromPoint/ToPoint/offset multipliers) movement runtime → builds via MA->BuildEntity synchronously → registers the UID mapping, captures relative transforms, broadcasts OnLootInitialized.
Input
| 参数 Param | 类型 Type | 说明 Description |
|---|---|---|
TemplateData | const FEntityTemplateData& | 实体模板(来自 CreateTemplateFromConfig)。Entity template (from CreateTemplateFromConfig). |
Output
返回生成的实体句柄;失败时无效句柄。副作用:UID 消耗、实体构建、注册表写入、事件广播。
Returns the spawned entity handle; invalid on failure. Side effects: UID consumption, entity build, registry write, event broadcast.
每 tick 状态机事件轮询(基类 TickComponent 在 SyncTransform 后调用):读 FLootParamsRT.State,与 PreviousLootState 比较;进入 Collected 时广播 OnLootCollected,进入 Expired(寿命耗尽未收集销毁)时广播 OnLootDestroyed;随后同步 PreviousLootState。
Per-tick state-machine event polling (called by base TickComponent after SyncTransform): reads FLootParamsRT.State and compares with PreviousLootState; entering Collected broadcasts OnLootCollected, entering Expired (lifespan ran out without collection) broadcasts OnLootDestroyed; then syncs PreviousLootState.
Input
| 参数 Param | 类型 Type | 说明 Description |
|---|---|---|
InDeltaTime | float | 时间步长(秒),当前实现未使用。Delta time in seconds; currently unused. |
Output
无返回值。副作用:状态边沿时广播事件。
No return value. Side effect: broadcasts on state edges.
① 调用基类(锁步 ID 预分配等);② 锁步路径:服务器 + Auto 模式向 GameMode 注册(RegisterPendingLockstepLootSpawn),tick 边界冲刷;随后返回;③ 非锁步:已初始化或未配置资产则休眠;Auto 模式直接 ManualInitialize(),Manual 模式等待显式调用。
① Calls the base (lockstep ID pre-allocation etc.); ② lockstep path: server + Auto mode registers with the GameMode (RegisterPendingLockstepLootSpawn) for the tick-boundary flush, then returns; ③ non-lockstep: dormant when already initialized or without a config asset; Auto mode calls ManualInitialize() directly, Manual mode waits for an explicit call.
Input
无参数。
No parameters.
Output
无返回值。副作用:锁步注册 / 自动生成 / 休眠分流。
No return value. Side effects: lockstep registration / auto spawn / dormant split.
① 基类先注销宿主注册表;② 未初始化则返回;③ 网络模式(UMassBattleSubsystem::bNetworkedMode):服务器广播 LootComponent.Despawn 锁步指令(双端同一 ExecuteTick 标 PendingDestroy),本地仅解绑;④ 非网络:本地标 PendingDestroy 并入销毁队列。
① Base unregisters the host registry first; ② returns when not initialized; ③ networked mode (UMassBattleSubsystem::bNetworkedMode): the server broadcasts the LootComponent.Despawn lockstep command (both peers mark PendingDestroy at the same ExecuteTick), local side only unbinds; ④ non-networked: marks PendingDestroy locally and enqueues destruction.
Input
| 参数 Param | 类型 Type | 说明 Description |
|---|---|---|
EndPlayReason | EEndPlayReason::Type | 结束播放原因。End-play reason. |
Output
无返回值。副作用:销毁实体(本地)或广播 Despawn 指令(网络)并解绑。
No return value. Side effects: entity destruction (local) or Despawn command broadcast (network) plus unbinding.
成员变量 Members
初始化模式:Auto / Manual(枚举定义于 MassBattleEnums.h;工具提示提及的 "Disabled" 值在枚举中不存在)。
Initialization mode: Auto / Manual (enum in MassBattleEnums.h; the "Disabled" value in the tooltip does not exist in the enum).
战利品配置数据资产,模板构建的数据源。未设置时组件休眠(等池化系统调用 InitializeWithEntity)。
Loot configuration data asset, the template data source. Unset keeps the component dormant (waiting for pooling InitializeWithEntity).
战利品参数乘数(见 Fragments/Loot.h 的 FLootMultipliers):Ballistic(ScaleMult / ScoreMult / VelocityMult / GravityMult)与 Interped(ScaleMult / ScoreMult / SpeedMult)两组。生成时应用 Ballistic.ScaleMult(缩放)与 Ballistic.ScoreMult(分数);弹道出生速度再乘 Ballistic.VelocityMult。
Loot parameter multipliers (see FLootMultipliers in Fragments/Loot.h): Ballistic (ScaleMult / ScoreMult / VelocityMult / GravityMult) and Interped (ScaleMult / ScoreMult / SpeedMult). At spawn, Ballistic.ScaleMult (scale) and Ballistic.ScoreMult (score) apply; ballistic birth velocity is further scaled by Ballistic.VelocityMult.
生成此战利品的实体(可选)。生成时写入 FLootParamsRT.Spawner;插值出生模式把它作为 Target。
Entity that spawned this loot (optional). Written into FLootParamsRT.Spawner at spawn; the interped birth mode uses it as the Target.
实体绑定完成事件,生成/重绑成功时广播。
Entity-bound event, broadcast on successful spawn/rebind.
战利品被收集事件,状态进入 Collected 时由 OnEntityTick 广播。
Loot-collected event, broadcast by OnEntityTick when the state enters Collected.
战利品销毁事件,状态进入 Expired(寿命耗尽)时由 OnEntityTick 广播。
Loot-destroyed event, broadcast by OnEntityTick when the state enters Expired (lifespan ran out).
上一帧缓存的 loot 状态,用于 OnEntityTick 的边沿检测;解绑时复位为 Spawning。
Last-frame cached loot state for edge detection in OnEntityTick; reset to Spawning on unbind.
实体 UID 在锁步生成时两端本地分配(不复制 —— 锁步保证两端一致);快照重绑时按此值反查实体。
Entity UID allocated locally on both peers at lockstep spawn time (NOT replicated — lockstep keeps both ends equal); snapshot rebind looks the entity up by this value.
惰性缓存的 loot 子系统指针。
Lazily-cached loot subsystem pointer.
管线角色 Pipeline Role
本地路径:BeginPlay(Auto)→ ManualInitialize → CreateTemplateFromConfig(经 UMassBattleLootSubsystem::MakeLootTemplateFromDataAsset)→ SpawnLootFromTemplate(同步 BuildEntity,UID 本地分配)→ 注册 UID 映射 → 广播 OnLootInitialized。锁步路径:服务器经 RegisterPendingLockstepLootSpawn 注册,GameMode tick 边界冲刷广播 LootComponent.Spawn;双端同一 ExecuteTick 执行 SpawnLockstepLootAtTransform。模拟侧:LootMonoProcessor 每帧推进状态机并把 OnLootSpawn/Collect/Destroy 事件推入 UMassBattleSubsystem 的事件队列;组件侧 OnEntityTick 独立轮询 FLootParamsRT.State 边沿广播收集/销毁委托(两条事件路径并存)。快照恢复后经 RebindToEntity(按 OwningLootUniqueID 经 FindEntityByUID)重链;销毁时服务器广播 LootComponent.Despawn。
Local path: BeginPlay (Auto) → ManualInitialize → CreateTemplateFromConfig (via UMassBattleLootSubsystem::MakeLootTemplateFromDataAsset) → SpawnLootFromTemplate (synchronous BuildEntity, UID allocated locally) → UID mapping registration → OnLootInitialized broadcast. Lockstep path: the server registers via RegisterPendingLockstepLootSpawn; the GameMode flushes at the tick boundary, broadcasting LootComponent.Spawn; both peers execute SpawnLockstepLootAtTransform at the same ExecuteTick. Simulation side: LootMonoProcessor advances the state machine each frame and pushes OnLootSpawn/Collect/Destroy events into the UMassBattleSubsystem event queues; the component side independently polls FLootParamsRT.State edges in OnEntityTick to broadcast the collect/destroy delegates (two event paths coexist). After snapshot restore, RebindToEntity re-links (by OwningLootUniqueID via FindEntityByUID); on destruction the server broadcasts LootComponent.Despawn.
补充说明 Notes
cpp 文件末尾有 "LootComponent.Spawn lockstep command binding (stub)" 注释段,说明锁步功能实现已随任务推进完成(注释为历史残留);LockstepNetID 生命周期实际已在基类实现。
The cpp file ends with a "LootComponent.Spawn lockstep command binding (stub)" comment section noting the feature was deferred per task; the lockstep functionality is implemented now (the comment is historical residue) — the LockstepNetID lifecycle lives in the base class.