diff --git a/flecs.c b/flecs.c index 68d148e14e..f4605bfd09 100644 --- a/flecs.c +++ b/flecs.c @@ -3287,6 +3287,16 @@ void flecs_table_init( flecs_table_init_storage_table(world, table); flecs_table_init_data(world, table); + + if (table->flags & EcsTableHasOnTableCreate) { + flecs_emit(world, world, &(ecs_event_desc_t) { + .ids = &table->type, + .event = EcsOnTableCreate, + .table = table, + .flags = EcsEventTableOnly, + .observable = world + }); + } } static @@ -3698,13 +3708,15 @@ void flecs_table_free( ecs_assert(table->refcount == 0, ECS_INTERNAL_ERROR, NULL); if (!is_root && !(world->flags & EcsWorldQuit)) { - flecs_emit(world, world, &(ecs_event_desc_t) { - .ids = &table->type, - .event = EcsOnTableDelete, - .table = table, - .flags = EcsEventTableOnly, - .observable = world - }); + if (table->flags & EcsTableHasOnTableDelete) { + flecs_emit(world, world, &(ecs_event_desc_t) { + .ids = &table->type, + .event = EcsOnTableDelete, + .table = table, + .flags = EcsEventTableOnly, + .observable = world + }); + } } if (ecs_should_log_2()) { @@ -51554,6 +51566,12 @@ ecs_flags32_t flecs_id_flag_for_event( if (e == EcsOnTableEmpty) { return EcsIdHasOnTableEmpty; } + if (e == EcsOnTableCreate) { + return EcsIdHasOnTableCreate; + } + if (e == EcsOnTableDelete) { + return EcsIdHasOnTableDelete; + } return 0; } @@ -56573,14 +56591,6 @@ ecs_table_t *flecs_create_table( flecs_init_table(world, result, prev); - flecs_emit(world, world, &(ecs_event_desc_t) { - .ids = &result->type, - .event = EcsOnTableCreate, - .table = result, - .flags = EcsEventTableOnly, - .observable = world - }); - /* Update counters */ world->info.table_count ++; world->info.table_record_count += result->record_count; @@ -60757,6 +60767,12 @@ ecs_id_record_t* flecs_id_record_new( if (flecs_check_observers_for_event(world, id, EcsOnTableEmpty)) { idr->flags |= EcsIdHasOnTableEmpty; } + if (flecs_check_observers_for_event(world, id, EcsOnTableCreate)) { + idr->flags |= EcsIdHasOnTableCreate; + } + if (flecs_check_observers_for_event(world, id, EcsOnTableDelete)) { + idr->flags |= EcsIdHasOnTableDelete; + } if (ecs_should_log_1()) { char *id_str = ecs_id_str(world, id); diff --git a/flecs.h b/flecs.h index 3770414939..916663a767 100644 --- a/flecs.h +++ b/flecs.h @@ -335,9 +335,12 @@ extern "C" { #define EcsIdHasUnSet (1u << 19) #define EcsIdHasOnTableFill (1u << 20) #define EcsIdHasOnTableEmpty (1u << 21) +#define EcsIdHasOnTableCreate (1u << 22) +#define EcsIdHasOnTableDelete (1u << 23) #define EcsIdEventMask\ (EcsIdHasOnAdd|EcsIdHasOnRemove|EcsIdHasOnSet|EcsIdHasUnSet|\ - EcsIdHasOnTableFill|EcsIdHasOnTableEmpty) + EcsIdHasOnTableFill|EcsIdHasOnTableEmpty|EcsIdHasOnTableCreate|\ + EcsIdHasOnTableDelete) #define EcsIdMarkedForDelete (1u << 30) @@ -416,6 +419,8 @@ extern "C" { #define EcsTableHasUnSet (1u << 19u) #define EcsTableHasOnTableFill (1u << 20u) #define EcsTableHasOnTableEmpty (1u << 21u) +#define EcsTableHasOnTableCreate (1u << 22u) +#define EcsTableHasOnTableDelete (1u << 23u) #define EcsTableHasObserved (1u << 25u) #define EcsTableHasTarget (1u << 26u) diff --git a/include/flecs/private/api_flags.h b/include/flecs/private/api_flags.h index 55da55025e..aa8724f534 100644 --- a/include/flecs/private/api_flags.h +++ b/include/flecs/private/api_flags.h @@ -75,9 +75,12 @@ extern "C" { #define EcsIdHasUnSet (1u << 19) #define EcsIdHasOnTableFill (1u << 20) #define EcsIdHasOnTableEmpty (1u << 21) +#define EcsIdHasOnTableCreate (1u << 22) +#define EcsIdHasOnTableDelete (1u << 23) #define EcsIdEventMask\ (EcsIdHasOnAdd|EcsIdHasOnRemove|EcsIdHasOnSet|EcsIdHasUnSet|\ - EcsIdHasOnTableFill|EcsIdHasOnTableEmpty) + EcsIdHasOnTableFill|EcsIdHasOnTableEmpty|EcsIdHasOnTableCreate|\ + EcsIdHasOnTableDelete) #define EcsIdMarkedForDelete (1u << 30) @@ -156,6 +159,8 @@ extern "C" { #define EcsTableHasUnSet (1u << 19u) #define EcsTableHasOnTableFill (1u << 20u) #define EcsTableHasOnTableEmpty (1u << 21u) +#define EcsTableHasOnTableCreate (1u << 22u) +#define EcsTableHasOnTableDelete (1u << 23u) #define EcsTableHasObserved (1u << 25u) #define EcsTableHasTarget (1u << 26u) diff --git a/src/id_record.c b/src/id_record.c index c28c756bf6..3de453fc59 100644 --- a/src/id_record.c +++ b/src/id_record.c @@ -256,6 +256,12 @@ ecs_id_record_t* flecs_id_record_new( if (flecs_check_observers_for_event(world, id, EcsOnTableEmpty)) { idr->flags |= EcsIdHasOnTableEmpty; } + if (flecs_check_observers_for_event(world, id, EcsOnTableCreate)) { + idr->flags |= EcsIdHasOnTableCreate; + } + if (flecs_check_observers_for_event(world, id, EcsOnTableDelete)) { + idr->flags |= EcsIdHasOnTableDelete; + } if (ecs_should_log_1()) { char *id_str = ecs_id_str(world, id); diff --git a/src/observer.c b/src/observer.c index 567673787f..ee7d7e259c 100644 --- a/src/observer.c +++ b/src/observer.c @@ -50,6 +50,12 @@ ecs_flags32_t flecs_id_flag_for_event( if (e == EcsOnTableEmpty) { return EcsIdHasOnTableEmpty; } + if (e == EcsOnTableCreate) { + return EcsIdHasOnTableCreate; + } + if (e == EcsOnTableDelete) { + return EcsIdHasOnTableDelete; + } return 0; } diff --git a/src/table.c b/src/table.c index 4bb1e6c0ba..2ba4080bd4 100644 --- a/src/table.c +++ b/src/table.c @@ -673,6 +673,16 @@ void flecs_table_init( flecs_table_init_storage_table(world, table); flecs_table_init_data(world, table); + + if (table->flags & EcsTableHasOnTableCreate) { + flecs_emit(world, world, &(ecs_event_desc_t) { + .ids = &table->type, + .event = EcsOnTableCreate, + .table = table, + .flags = EcsEventTableOnly, + .observable = world + }); + } } static @@ -1084,13 +1094,15 @@ void flecs_table_free( ecs_assert(table->refcount == 0, ECS_INTERNAL_ERROR, NULL); if (!is_root && !(world->flags & EcsWorldQuit)) { - flecs_emit(world, world, &(ecs_event_desc_t) { - .ids = &table->type, - .event = EcsOnTableDelete, - .table = table, - .flags = EcsEventTableOnly, - .observable = world - }); + if (table->flags & EcsTableHasOnTableDelete) { + flecs_emit(world, world, &(ecs_event_desc_t) { + .ids = &table->type, + .event = EcsOnTableDelete, + .table = table, + .flags = EcsEventTableOnly, + .observable = world + }); + } } if (ecs_should_log_2()) { diff --git a/src/table_graph.c b/src/table_graph.c index dd038116a7..0101d8f999 100644 --- a/src/table_graph.c +++ b/src/table_graph.c @@ -560,14 +560,6 @@ ecs_table_t *flecs_create_table( flecs_init_table(world, result, prev); - flecs_emit(world, world, &(ecs_event_desc_t) { - .ids = &result->type, - .event = EcsOnTableCreate, - .table = result, - .flags = EcsEventTableOnly, - .observable = world - }); - /* Update counters */ world->info.table_count ++; world->info.table_record_count += result->record_count;