Skip to content

[Feature Request]: [ECS] Move ComponentMapper's OnDelete invoke #1062

Closed

Description

@DoubleBarreledHerald

Purpose

By moving ComponentMapper's OnDelete invoke to before the removal of the component reference,
the component can be easily referenced in the action.

public override void Delete(int entityId)
{
    OnDelete?.Invoke(entityId);
    Components[entityId] = null;
    _onCompositionChanged(entityId);
}

Motivation

I am using another physics library and have a component to act as an "in between" with the ECS system.
But, when I destroy the parent entity or component I don't have an easy way to reference the component to destroy the partner physics body.

Activity

  1. AristurtleDev commented on Nov 9, 2025

    @AristurtleDev
    Collaborator

    I don't see any significant downsides to implementing this change.

    Somethings to consider would be if an OnDelete handler calls Delete() again on the same component, the component would be set to null during handler execution. Subsequent handlers in the chain would see null and likely throw. This is user error and would fail fast, putting the responsibility on users to avoid duplicating delete calls. If a handler throws an exception, the component will not be be removed and the composition changed event will not fire, leaving the entity in an inconsistent state. This is standard event handler behavior, however, and would be the user's responsibility to handle.

    An alternative approach could be to split this into two events; OnBeforeDelete (invoked before removal, as you're proposing) and keep the existing OnDelete (invoked after removal). This would provide flexibility for different use cases while maintaining backward compatibility.

    However, I think moving OnDelete to before the removal is the better choice since the current timing provides no benefit and it enables important cleanup scenarios like your physics body example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions