Release vs. KENSEI_DEBUG
KenseiECS has two builds of the same code. Release keeps the hot paths free of checks; KENSEI_DEBUG compiles in validation that turns misuse into exceptions.
| Situation | Release | KENSEI_DEBUG |
|---|---|---|
Add of a component the entity already has | throws | throws |
Add/Get/Has/Remove, AddEvent, Subscribe/Unsubscribe with a dead or stale Entity | undefined (may corrupt another entity) | throws, also while another entity is being destroyed; Add/Get/Has/Remove and AddEvent accept the entity whose DestroyEntity is running, for its listeners |
Pool Add(int) on a dead slot | undefined | throws, except for the slot being destroyed |
Get (world or pool) or pool Modify of a component the entity does not have | IndexOutOfRangeException on Mono and .NET; on IL2CPP, which skips bounds checks there, a read outside the pool's arrays, and a write through the ref corrupts memory | throws |
GetComponentTypes / GetComponentCount of a dead entity | reads the slot: empty while it is free, another entity's components once it is reused | throws |
| A listener re-adds a component to the entity being destroyed on every removal pass | DestroyEntity never returns | throws after 1,000 passes |
Remove of a missing component | no-op | no-op |
A filter with Exc only, or with no constraints at all (world.Filter<Exc<Died>>()) | End() throws | End() throws |
world.Subscribe(entity, view) without the listener type (also Unsubscribe, UnsubscribeLater, CreateWithListener) | not checked: C# infers the view's class, so the call works on a Listeners<ViewClass> that no system reads and the view is never called | not checked |
world.AddEvent of a type with no OneFrame<EventBuffer<T>>() or DelHere registration | not checked: the events are never removed, and every consumer reads them again each frame | not checked |
DestroyEntity of a dead entity | no-op | no-op |
CopyEntity of a dead entity | returns Entity.Null | throws |
GetEntity on a dead slot | dead handle | dead handle |
A CommandBuffer command on a PendingEntity of another buffer, of a recording the buffer has played back or cleared since (also right after Playback, before anything else is recorded), or a default one | not checked: the command goes to whichever entity has that id in the buffer, or to the one an earlier Playback created under it | throws; the command is not recorded |
| Removing an unvisited entity from an iterated filter (would double-visit) | double visit | throws once the removal is complete: the entity has left the filter, the other filters are updated and an entity left without components is destroyed, so the world is consistent after the exception (up to 2.4.0 it threw in the middle: the entity stayed in the iterated filter without the component). Inside DestroyEntity the destroy is interrupted like by any exception: the slot is released and the components not removed yet stay orphaned until Clear |
A Filter.Enumerator driven by hand (GetEnumerator, MoveNext) and never disposed | nothing to check | World.NextTick outside a Run, or the end of the outermost SystemsRunner.Run, releases the filter's iteration guard and throws, naming the filter; up to 2.4.0 the guard stayed held and later removals from the filter threw the double-visit error at random. A foreach over a filter that is wrapped around a root Run() or a World.NextTick(), outside any Run, looks the same from there and gets the same report: Run() throws from its NextTick before any system ran, so the systems are skipped for that frame, and the loop goes on with its guard released. Drive the frames from outside a loop over a filter |
Runner: Run before Init | not checked: the systems run without their Init | throws |
Runner: Add after Init | not checked: Init is not called for the system | throws |
Runner: unknown name in SetActive / IsActive / GetRunner | ignored: does nothing, returns false, returns null | throws |
| Runner: a name registered twice in one runner | not checked: lookups by the name reach only one of the two | throws |
| Runner: a runner that is already registered added again (any slot, any parent), or added to itself or to a runner nested in it | throws | throws |
EcsBootstrap: Configure registers something in the update runner under "fixed" or "late" | not checked: the name reaches two registrations | Awake throws right after Configure |
Runner: SetActive / IsActive on a system without Run (Init/Destroy only) | does nothing / returns false | throws |
| Nested runner with a different World / SharedData | silently ignored | throws |
CreateEntity / Pool<T> after World.Destroy | NullReferenceException on Mono and .NET; on IL2CPP, which skips null checks in World, a read through a null reference that crashes the player (an access violation, not an exception) | ObjectDisposedException |
World.Clear / ReserveEntities / any Reserve (world, pool, filter) / Filter after World.Destroy | ObjectDisposedException | ObjectDisposedException |
A reserved storage array grows past its reserve (ReserveEntities, pool.Reserve, filter.Reserve) | not checked | counted in GrowthsAfterReserve, the first one kept in FirstGrowthAfterReserve, logged by EcsProfiler while it records that world; growth of a pool or filter nothing reserved is not counted, and EcsProfiler logs it marked (not reserved) |
Validation code is not compiled in release; the cost is zero.
csharp
var entity = world.CreateEntity(new Health { Value = 10 });
world.DestroyEntity(entity);
// Release: undefined; the slot may already belong to another entity.
// KENSEI_DEBUG: throws InvalidOperationException.
world.Get<Health>(entity);
// Safe in both modes: check first.
if (world.IsAlive(entity)) {
world.Get<Health>(entity).Value -= 1;
}TIP
Develop and run tests with KENSEI_DEBUG; a "may corrupt another entity" bug in release shows up there as an exception at the faulty call. Safe patterns for removing entities while iterating are covered in Filters and CommandBuffer.
Enabling it
- Unity: toggle via KenseiECS -> Debug Mode. It sets
KENSEI_DEBUGin the active Build Profile (Unity 6) or, without one, in the Player Settings of the active build target only. A Build Profile is a shared asset under version control, so the toggle logs each write to it. Turning the mode off does not touch other targets; a warning lists those whose Player Settings still have the define. A non-development player build with the define fails unlessKENSEI_ALLOW_DEBUG_RELEASEis set the same way. The error names where the define comes from: the profile or the Player Settings of the target (checked in the preprocess step), orBuildPlayerOptions.extraScriptingDefines, Additional Compiler Arguments orAssets/csc.rsp(checked right after the player scripts compile). Content builds (Addressables, AssetBundles) are not checked. The checks run inside the build, after Unity has started calling preprocessors, so build scripts should check the defines beforeBuildPlayer; see Release build guard. - .NET / tests: the define has to reach the project that compiles the KenseiECS sources. With a project reference to
KenseiECS.NET/KenseiECS.csproj, build with-p:KenseiDebug=true: it is a global property, so the referenced project gets it too. A project that compiles the sources itself, asKenseiECS.Testsdoes, addsKENSEI_DEBUGto its ownDefineConstants; defining it in a test project that only references a builtKenseiECS.dllchanges nothing.
Debug-only features
Besides the checks, KENSEI_DEBUG enables the debug tooling: EcsProfiler, per-system timings in the runner, entity names, World.GrowthsAfterReserve and World.FirstGrowthAfterReserve. Code that uses them must compile in release too:
SetName,GetName,GrowthsAfterReserveandFirstGrowthAfterReserveexist in every build. Without the define they do nothing, returnnull, 0 and an emptyStorageGrowth, so they are safe in any expression (SetNamestill evaluates its arguments).EcsProfilerwithProfileEventandProfileEventType, the runner timings (SystemInfo.LastRunMsandPeakRunMs,SystemsRunner.ResetTimings) and the untypedGetRaw/SetRawof a pool exist only underKENSEI_DEBUG, so code that uses them needs the#if:
csharp
world.SetName(entity, "Player"); // a no-op in release; arguments are still evaluated
#if KENSEI_DEBUG
EcsProfiler.Enable(world); // EcsProfiler exists only under KENSEI_DEBUG
#endif