Skip to content
Flecs v4.1
entity_ranges.h
Go to the documentation of this file.
1/**
2 * @file addons/entity_ranges.h
3 * @brief Entity id range management.
4 */
5
6#ifdef FLECS_ENTITY_RANGES
7
8#ifndef FLECS_ENTITY_RANGES_H
9#define FLECS_ENTITY_RANGES_H
10
11#ifdef __cplusplus
12extern "C" {
13#endif
14
15/**
16 * @defgroup c_addons_entity_ranges Entity Ranges
17 * @ingroup c_addons
18 * Entity id range management.
19 *
20 * @{
21 */
22
23/** Type that stores an entity id range.
24 * Returned by ecs_entity_range_new(), used with ecs_entity_range_set().
25 */
26typedef struct ecs_entity_range_t {
27 uint32_t min; /**< First id in range (inclusive). */
28 uint32_t max; /**< Last id in range (inclusive, 0 = unlimited). */
29 uint32_t cur; /**< Last issued id in range. */
30 ecs_vec_t recycled; /**< Recycled entity ids (vec<entity_t>). */
32
33/** Create a new entity range.
34 * This function creates a range that constrains new entity identifiers returned
35 * by the specified [min, max] interval. Each range maintains its own list of
36 * recycled entity ids, which ensures that recycled ids always respect the
37 * configured range. If `max` is set to 0, the range is unbounded.
38 *
39 * Entity ranges cannot be deleted once created. Use ecs_entity_range_set() to
40 * activate a range.
41 *
42 * @param world The world.
43 * @param min The first entity id in the range (inclusive).
44 * @param max The last entity id in the range (inclusive, 0 = unlimited).
45 * @return A pointer to the new range. Does not need to be freed.
46 */
47FLECS_API
49 ecs_world_t *world,
50 uint32_t min,
51 uint32_t max);
52
53/** Set the active entity range.
54 * This function activates a range created with ecs_entity_range_new().
55 * When a range is activated, new entity identifiers will fall within the
56 * specified [min, max] interval, including recycled identifiers.
57 *
58 * When the active range is out of available ids, operations that create new
59 * entity ids will assert.
60 *
61 * The operation only accepts ranges that have been created by
62 * ecs_entity_range_new().
63 *
64 * @param world The world.
65 * @param range The range to activate.
66 */
67FLECS_API
69 ecs_world_t *world,
70 const ecs_entity_range_t *range);
71
72/** Create a new entity from a specific range.
73 * This function creates a new entity with an id from the specified range,
74 * without changing the active range. If the range has recycled ids, a recycled
75 * id is returned, otherwise a new id is issued from the range.
76 *
77 * When the range is out of available ids, this operation will assert.
78 *
79 * The operation only accepts ranges that have been created by
80 * ecs_entity_range_new().
81 *
82 * @param world The world.
83 * @param range The range to create the entity from.
84 * @return The new entity.
85 */
86FLECS_API
88 ecs_world_t *world,
89 const ecs_entity_range_t *range);
90
91/** Get the currently active entity id range.
92 * Returns the range set by ecs_entity_range_set(), or NULL if no range is
93 * active.
94 *
95 * @param world The world.
96 * @return The active range, or NULL.
97 */
98FLECS_API
100 const ecs_world_t *world);
101
102/** @} */
103
104#ifdef __cplusplus
105}
106#endif
107
108#endif
109
110#endif // FLECS_ENTITY_RANGES
FLECS_API void ecs_entity_range_set(ecs_world_t *world, const ecs_entity_range_t *range)
Set the active entity range.
FLECS_API const ecs_entity_range_t * ecs_entity_range_new(ecs_world_t *world, uint32_t min, uint32_t max)
Create a new entity range.
FLECS_API const ecs_entity_range_t * ecs_entity_range_get(const ecs_world_t *world)
Get the currently active entity id range.
FLECS_API ecs_entity_t ecs_new_from_range(ecs_world_t *world, const ecs_entity_range_t *range)
Create a new entity from a specific range.
ecs_id_t ecs_entity_t
An entity identifier.
Definition flecs.h:396
struct ecs_world_t ecs_world_t
A world is the container for all ECS data and supporting features.
Definition flecs.h:440
Type that stores an entity id range.
uint32_t max
Last id in range (inclusive, 0 = unlimited).
uint32_t cur
Last issued id in range.
ecs_vec_t recycled
Recycled entity ids (vec<entity_t>).
uint32_t min
First id in range (inclusive).