Skip to content

Component hook ergonomics #21794

@cart

Description

@cart

What problem does this solve or what need does it fill?

It would be nice if this common pattern could be expressed more easily:

#[derive(Component]
#[component(on_add =  Self::on_add, on_remove = Self::on_remove)]
struct TilePosition {
    x: u32,
    y: u32,
}

impl TilePosition {
    fn on_add(mut world: DeferredWorld, context: HookContext) {
    }

    fn on_remove(mut world: DeferredWorld, context: HookContext) {
    }
}

What solution would you like?

The component hook derive attributes currently have no default value. I propose that we default #[component(on_add)] to #[component(on_add = Self::on_add)]. For example:

#[derive(Component]
#[component(on_add)]
struct TilePosition {
    x: u32,
    y: u32,
}

impl TilePosition {
    fn on_add(mut world: DeferredWorld, context: HookContext) {
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ECSEntities, components, systems, and eventsC-FeatureA new feature, making something new possibleD-MacrosCode that generates Rust codeD-TrivialNice and easy! A great choice to get started with BevyS-Ready-For-ImplementationThis issue is ready for an implementation PR. Go for it!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions