Skip to content

Fix: SoA diff serializer correctly handles undefined values for entities without components#208

Open
americanjeff wants to merge 5 commits into
NateTheGreatt:mainfrom
americanjeff:main
Open

Fix: SoA diff serializer correctly handles undefined values for entities without components#208
americanjeff wants to merge 5 commits into
NateTheGreatt:mainfrom
americanjeff:main

Conversation

@americanjeff
Copy link
Copy Markdown

Summary

  • Fixed hasChanged function in SoASerializer to properly handle undefined array slots when entities don't have all components
  • Fixed shadow array initialization to not fill with zeros (which caused false change detection for undefined slots)

Root Cause

When using diff-mode serialization over all entities (e.g., getAllEntities()), entities that don't have a particular component have undefined slots in that component's arrays. The getShadow function was initializing shadow arrays with 0, and hasChanged was treating undefined vs undefined as a change (returning true instead of false).

Changes

  1. getShadow: Remove .fill(0) - shadow arrays now initialize with undefined slots
  2. hasChanged: Add proper handling for undefined values:
    • Both undefined → no change, return false
    • Shadow undefined, current defined → first time seeing value, return true
    • Current undefined, shadow defined → entity lost component, return true

Test Plan

All existing tests pass (162 tests).

American Jeff added 5 commits March 8, 2026 11:34
…ntities

- Add check for undefined shadow values in hasChanged function
- Float values (f32/f64) now serialize correctly for new entities in diff mode
- Add tests demonstrating the bug and verifying the fix
Goal of this change is to prevent recycled EID corruption in SoA diff serialization

- Add getRemovals() to ObserverSerializerFunction: tracks entity IDs removed since last call via onRemove(networkedTag) observer
- Add getRemovals option to SoASerializerOptions: before each diff pass, clears ALL shadow entries for removed EIDs (entity-level, not per-component) because the serializer processes all component arrays for every entity — stale typed-array values left by bitECS can contaminate shadows for any component at a recycled slot
- Add clearEntity() method to SoASerializerFunction for manual shadow clearing
- Add EntityRecycling.test.ts with three tests covering the recycling scenario
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant