-
Notifications
You must be signed in to change notification settings - Fork 1
Open
Milestone
Description
Description
The current implementation in ComponentBase.cs needs enhanced error handling and validation to ensure custom components meet ECS requirements.
Why Value Type Validation is Critical for ECS Components
In Entity-Component-System architectures, components must be value types (structs) for several important reasons:
1. Memory Layout and Performance
- Components are stored in contiguous memory arrays (ComponentArray) for cache efficiency
- Value types ensure predictable memory layouts without indirection
- Reference types would introduce pointer chasing and memory fragmentation
2. Native Interop Requirements
- The engine uses native interop with C++ code that expects fixed-size, POD (Plain Old Data) types
- Reference types have unpredictable memory layouts and garbage collection concerns
- Native code needs to safely read/write component data using raw pointers
3. Copy Semantics
- ECS operations frequently copy components (e.g., when moving entities between archetypes)
- Value types have well-defined copy semantics
- Reference types would share state unexpectedly across component instances
4. Serialization and Networking
- Components need to be serializable for save/load and networking
- Value types can be directly serialized as binary blobs
- Reference types require complex serialization handling
5. Thread Safety
- Multiple systems may access the same component type concurrently
- Value types eliminate shared mutable state concerns
- Reference types would require synchronization mechanisms
Proposed Improvements
- Add value type validation - Reject reference types during component registration
- Improve exception handling - Catch specific reflection exceptions
- Add performance optimization - Cache discovered component types
Current Issues
- Broad exception catching hides specific reflection errors
- No validation ensures components are structs
- Repeated reflection calls impact performance
References
- PR: feat: add custom components to .NET scripts #352
- Comment: feat: add custom components to .NET scripts #352 (comment)
Implementation Priority
High - This validation prevents runtime errors and ensures ECS performance characteristics.
Metadata
Metadata
Assignees
Labels
No labels
Type
Projects
Status
Todo