C++ ECS Execution Order and Deferred Changes¶
A queue solves a timing problem: decisions observe stable membership and requested changes become visible at a defined boundary. It does not require slot reuse.
Frame Contract¶
DeferredWorld wraps chapter 6:
Decisions see post-simulation state, after natural expiration. Recorded commands do not change membership during the callback. Newly created bodies first move next frame.
This is our chosen policy, not a universal ECS rule.
Run a Replacement¶
Advance A to x=1, request deletion, then spawn from its snapshot. During decisions A resolves; after flush the new handle resolves at x=1.
The replacement receives a new monotonic ID. No generations are involved.
Restrict Mutation Authority¶
The callback receives a const world view and Decisions writer. The writer exposes spawn/destroy, not immediate mutation or flush.
Mutation through a captured wrapper reference during a frame is rejected. Do not retain callback views or writer references beyond the callback.
Spawn input is validated when recorded. Destroy records the full handle, including owner, not merely its number.
Failure Is Not a Transaction¶
A decision exception happens after simulation advanced. Recorded commands remain pending; another step is blocked until retry or discard.
A flush failure may leave a successful prefix applied and a failed command plus suffix pending. Receipts retain successes. Retry flushes pending work without replaying the prefix or simulation.
Discard removes pending work, not previously applied changes. Commands::flush expects a trusted target with the spawn/destroy guarantees used here, not arbitrary side-effecting code.
Tests Specify Timing¶
The deferred group checks stable observations, repeated-delete handling, next-frame movement, reentrancy rejection, callback failure, retry, and discard.
The maintenance group injects a controlled pre-mutation failure on the second spawn and checks that retry yields exactly two bodies. This does not emulate every possible allocation failure.
Exercise¶
Record delete(A), spawn(snapshot(A)), delete(A). Expect one deletion, one creation, and one ignored deletion. Explain recording order separately from visibility.
Next: diagnose a wrong result with writer traces.