Skip to content
Flecs v4.1
flecs.h
Go to the documentation of this file.
1/**
2 * @file flecs.h
3 * @brief Flecs public API.
4 *
5 * This file contains the public API for Flecs.
6 */
7
8#ifndef FLECS_H
9#define FLECS_H
10
11/**
12 * @defgroup c C API
13 *
14 * @{
15 * @}
16 */
17
18/**
19 * @defgroup core Core
20 * @ingroup c
21 * Core ECS functionality (entities, storage, queries).
22 *
23 * @{
24 */
25
26/**
27 * @defgroup options API defines
28 * Defines for customizing compile-time features.
29 *
30 * @{
31 */
32
33/* Flecs version macros */
34#define FLECS_VERSION_MAJOR 4 /**< Flecs major version. */
35#define FLECS_VERSION_MINOR 1 /**< Flecs minor version. */
36#define FLECS_VERSION_PATCH 6 /**< Flecs patch version. */
37
38/** Flecs version. */
39#define FLECS_VERSION FLECS_VERSION_IMPL(\
40 FLECS_VERSION_MAJOR, FLECS_VERSION_MINOR, FLECS_VERSION_PATCH)
41
42/** @def FLECS_CONFIG_HEADER
43 * Allows for including a user-customizable header that specifies compile-time
44 * features. */
45#ifdef FLECS_CONFIG_HEADER
46#include "flecs_config.h"
47#endif
48
49/** @def ecs_float_t
50 * Customizable precision for floating-point operations. */
51#ifndef ecs_float_t
52#define ecs_float_t float
53#endif
54
55/** @def ecs_ftime_t
56 * Customizable precision for scalar time values. Change to double precision for
57 * processes that can run for a long time (e.g., longer than a day). */
58#ifndef ecs_ftime_t
59#define ecs_ftime_t ecs_float_t
60#endif
61
62
63/** @def FLECS_ACCURATE_COUNTERS
64 * Define to ensure that global counters used for statistics (such as the
65 * allocation counters in the OS API) are accurate in multithreaded
66 * applications, at the cost of increased overhead.
67 */
68// #define FLECS_ACCURATE_COUNTERS
69
70/** @def FLECS_DISABLE_COUNTERS
71 * Disables counters used for statistics. Improves performance, but
72 * will prevent some features that rely on statistics from working,
73 * like the statistics pages in the explorer.
74 */
75// #define FLECS_DISABLE_COUNTERS
76
77/* Make sure provided configuration is valid */
78#if defined(FLECS_DEBUG) && defined(FLECS_NDEBUG)
79#warning "invalid configuration: cannot both define FLECS_DEBUG and FLECS_NDEBUG"
80#endif
81#if defined(FLECS_DEBUG) && defined(NDEBUG)
82#warning "invalid configuration: cannot both define FLECS_DEBUG and NDEBUG"
83#endif
84
85/** @def FLECS_DEBUG
86 * Used for input parameter checking and cheap sanity checks. There are lots of
87 * asserts in every part of the code, so this will slow down applications.
88 */
89#if !defined(FLECS_DEBUG) && !defined(FLECS_NDEBUG)
90#if defined(NDEBUG)
91#define FLECS_NDEBUG
92#else
93#define FLECS_DEBUG
94#endif
95#endif
96
97/** @def FLECS_SANITIZE
98 * Enables expensive checks that can detect issues early. Recommended for
99 * running tests or when debugging issues. This will severely slow down code.
100 */
101#ifdef FLECS_SANITIZE
102#ifndef FLECS_DEBUG
103#define FLECS_DEBUG /* If sanitized mode is enabled, so is debug mode */
104#endif
105#endif
106
107/** @def FLECS_DEBUG_INFO
108 * Adds additional debug information to internal data structures. Necessary when
109 * using natvis.
110 */
111#ifdef FLECS_DEBUG
112#ifndef FLECS_DEBUG_INFO
113#define FLECS_DEBUG_INFO
114#endif
115#ifndef FLECS_EXCLUSIVE_ACCESS
116#define FLECS_EXCLUSIVE_ACCESS /* Enable exclusive access checks in debug mode */
117#endif
118#endif
119
120/* Tip: if you see weird behavior that you think might be a bug, make sure to
121 * test with the FLECS_DEBUG or FLECS_SANITIZE flags enabled. There's a good
122 * chance that this gives you more information about the issue! */
123
124/** @def FLECS_SOFT_ASSERT
125 * Define to not abort for recoverable errors, like invalid parameters. An error
126 * is still thrown to the console. This is recommended for when running inside a
127 * third-party runtime, such as the Unreal editor.
128 *
129 * Note that internal sanity checks (ECS_INTERNAL_ERROR) will still abort a
130 * process, as this gives more information than a (likely) subsequent crash.
131 *
132 * When a soft assert occurs, the code will attempt to minimize the number of
133 * side effects of the failed operation, but this may not always be possible.
134 * Even though an application may still be able to continue running after a soft
135 * assert, it should be treated as if in an undefined state.
136 */
137// #define FLECS_SOFT_ASSERT
138
139/** @def FLECS_KEEP_ASSERT
140 * By default, asserts are disabled in release mode, when either FLECS_NDEBUG or
141 * NDEBUG is defined. Defining FLECS_KEEP_ASSERT ensures that asserts are not
142 * disabled. This define can be combined with FLECS_SOFT_ASSERT.
143 */
144// #define FLECS_KEEP_ASSERT
145
146/** @def FLECS_DEFAULT_TO_UNCACHED_QUERIES
147 * When set, this will cause queries with the EcsQueryCacheDefault policy
148 * to default to EcsQueryCacheNone. This can reduce the memory footprint of
149 * applications at the cost of performance. Queries that use features which
150 * require caching, such as group_by and order_by, will still use caching.
151 */
152// #define FLECS_DEFAULT_TO_UNCACHED_QUERIES
153
154/** @def FLECS_CREATE_MEMBER_ENTITIES
155 * By default, Flecs does not create entities for component members when they
156 * are registered with reflection. Define this addon if an application relies
157 * on members being defined as entities. Features that require members to be
158 * defined as entities are:
159 * - Member queries
160 * - Metrics
161 * - Alerts
162 *
163 * Member entities can also be created on a per-type basis by setting
164 * ecs_struct_desc_t::create_member_entities to true.
165 */
166// #define FLECS_CREATE_MEMBER_ENTITIES
167
168/** @def FLECS_CPP_NO_AUTO_REGISTRATION
169 * When set, the C++ API will require that components are registered before they
170 * are used. This is useful in multithreaded applications, where components need
171 * to be registered beforehand, and to catch issues in projects where component
172 * registration is mandatory. Disabling automatic component registration also
173 * slightly improves performance.
174 * The C API is not affected by this feature.
175 */
176// #define FLECS_CPP_NO_AUTO_REGISTRATION
177
178/** @def FLECS_CPP_NO_ENUM_REFLECTION
179 * When set, the C++ API will not attempt to discover and register enum
180 * constants for registered enum components. This will cause C++ APIs that
181 * accept enum constants to not work.
182 * Disabling this feature can significantly improve compile times and reduce
183 * the RAM footprint of an application.
184 */
185// #define FLECS_CPP_NO_ENUM_REFLECTION
186
187/** @def FLECS_NO_ALWAYS_INLINE
188 * When set, this will prevent functions from being annotated with always_inline,
189 * which can improve performance at the cost of increased binary footprint.
190 */
191// #define FLECS_NO_ALWAYS_INLINE
192
193/** @def FLECS_CUSTOM_BUILD
194 * This macro lets you customize which addons to build Flecs with.
195 * Without any addons, Flecs is just a minimal ECS storage, but addons add
196 * features such as systems, scheduling, and reflection. If an addon is disabled,
197 * it is excluded from the build, so that it consumes no resources. By default,
198 * all addons are enabled.
199 *
200 * You can customize a build by either whitelisting or blacklisting addons. To
201 * whitelist addons, first define the FLECS_CUSTOM_BUILD macro, which disables
202 * all addons. You can then manually select the addons you need by defining
203 * their macro, like "FLECS_SYSTEM".
204 *
205 * To blacklist an addon, make sure to *not* define FLECS_CUSTOM_BUILD, and
206 * instead define the addons you don't need by defining FLECS_NO_<addon>, for
207 * example "FLECS_NO_SYSTEM". If there are any addons that depend on the
208 * blacklisted addon, an error will be thrown during the build.
209 *
210 * Note that addons can have dependencies on each other. Addons will
211 * automatically enable their dependencies. To see the list of addons that were
212 * compiled in a build, enable tracing before creating the world by doing:
213 *
214 * @code
215 * ecs_log_set_level(0);
216 * @endcode
217 *
218 * which outputs the full list of addons Flecs was compiled with.
219 */
220// #define FLECS_CUSTOM_BUILD
221
222#ifndef FLECS_CUSTOM_BUILD
223#define FLECS_ALERTS /**< Monitor conditions for errors. */
224#define FLECS_APP /**< Application addon. */
225#define FLECS_CACHED_QUERIES /**< Cached query support. */
226// #define FLECS_C /**< C API convenience macros, always enabled. */
227#define FLECS_CPP /**< C++ API. */
228#define FLECS_CONSTRAINT_TRAITS /**< Component traits that enforce constraints. */
229#define FLECS_DOC /**< Document entities and components. */
230// #define FLECS_EXCLUSIVE_ACCESS /**< Enable exclusive world access checks. */
231#define FLECS_ENTITY_RANGES /**< Create entities in custom id ranges. */
232#define FLECS_FRAME /**< Frame management utilities. */
233// #define FLECS_JOURNAL /**< Journaling addon. */
234#define FLECS_JSON /**< Parsing JSON to/from component values. */
235#define FLECS_HTTP /**< Tiny HTTP server for connecting to remote UI. */
236#define FLECS_LOG /**< When enabled, ECS provides more detailed logs. */
237#define FLECS_META /**< Reflection support. */
238#define FLECS_METRICS /**< Expose component data as statistics. */
239#define FLECS_MODULE /**< Module support. */
240#define FLECS_MULTI_WORLD /**< Support C++ component ids across multiple worlds. */
241#define FLECS_OS_API_IMPL /**< Default implementation for OS API. */
242// #define FLECS_PERF_TRACE /**< Enable performance tracing. */
243#define FLECS_PIPELINE /**< Pipeline support. */
244#define FLECS_PREFAB /**< Prefabs */
245#define FLECS_QUERY_PLANS /**< Query plan support. */
246#define FLECS_REST /**< REST API for querying application data. */
247#define FLECS_PARSER /**< Utilities for script and query DSL parsers. */
248#define FLECS_QUERY_DSL /**< Flecs query DSL parser. */
249#define FLECS_SCRIPT /**< Flecs entity notation language. */
250#define FLECS_SCRIPT_ASYNC /**< Async/await support for Flecs script. */
251// #define FLECS_SCRIPT_MATH /**< Math functions for Flecs script (may require linking with libm). */
252// #define FLECS_SCRIPT_PLATFORM /**< Platform constants for Flecs script. */
253#define FLECS_SCRIPT_EVENT /**< Mouse and keyboard events for Flecs script. */
254#define FLECS_SYSTEM /**< System support. */
255#define FLECS_STATS /**< Track runtime statistics. */
256#define FLECS_TIMER /**< Timer support. */
257#define FLECS_UNITS /**< Built-in standard units. */
258#endif // ifndef FLECS_CUSTOM_BUILD
259
260#include "flecs/private/addon_defines.h"
261
262/** @def FLECS_LOW_FOOTPRINT
263 * Set a number of constants to values that decrease memory footprint, at the
264 * cost of decreased performance. */
265// #define FLECS_LOW_FOOTPRINT
266#ifdef FLECS_LOW_FOOTPRINT
267#define FLECS_HI_COMPONENT_ID 16
268#define FLECS_HI_ID_RECORD_ID 16
269#define FLECS_ENTITY_PAGE_BITS 6
270#define FLECS_USE_OS_ALLOC
271#define FLECS_DEFAULT_TO_UNCACHED_QUERIES
272#endif
273
274/** @def FLECS_HI_COMPONENT_ID
275 * This constant can be used to balance between performance and memory
276 * utilization. The constant is used in two ways:
277 * - Entity IDs 0..FLECS_HI_COMPONENT_ID are reserved for component IDs.
278 * - Used as the lookup array size in table edges.
279 *
280 * Increasing this value increases the size of the lookup array, which allows
281 * fast table traversal, which improves performance of ECS add/remove
282 * operations. Component IDs that fall outside of this range use a regular map
283 * lookup, which is slower but more memory efficient.
284 *
285 * This value must be set to a power of 2. Setting it to a value that is not a
286 * power of 2 will degrade performance.
287 */
288#ifndef FLECS_HI_COMPONENT_ID
289#define FLECS_HI_COMPONENT_ID 256
290#endif
291
292/** @def FLECS_HI_ID_RECORD_ID
293 * This constant can be used to balance between performance and memory
294 * utilization. The constant is used to determine the size of the component record
295 * lookup array. ID values that fall outside of this range use a regular map
296 * lookup, which is slower but more memory efficient.
297 */
298#ifndef FLECS_HI_ID_RECORD_ID
299#define FLECS_HI_ID_RECORD_ID 1024
300#endif
301
302/** @def FLECS_SPARSE_PAGE_BITS
303 * This constant is used to determine the number of bits of an ID that is used
304 * to determine the page index when used with a sparse set. The number of bits
305 * determines the page size, which is (1 << bits).
306 * Lower values decrease memory utilization, at the cost of more allocations. */
307#ifndef FLECS_SPARSE_PAGE_BITS
308#define FLECS_SPARSE_PAGE_BITS 6
309#endif
310
311/** @def FLECS_ENTITY_PAGE_BITS
312 * Same as FLECS_SPARSE_PAGE_BITS, but for the entity index. */
313#ifndef FLECS_ENTITY_PAGE_BITS
314#define FLECS_ENTITY_PAGE_BITS 10
315#endif
316
317/** @def FLECS_USE_OS_ALLOC
318 * When enabled, Flecs will use the OS allocator provided in the OS API directly
319 * instead of the built-in block allocator. This can decrease memory utilization
320 * as memory will be freed more often, at the cost of decreased performance. */
321// #define FLECS_USE_OS_ALLOC
322
323/** @def FLECS_ID_DESC_MAX
324 * Maximum number of IDs to add in ecs_entity_desc_t / ecs_bulk_desc_t. */
325#ifndef FLECS_ID_DESC_MAX
326#define FLECS_ID_DESC_MAX 32
327#endif
328
329/** @def FLECS_EVENT_DESC_MAX
330 * Maximum number of events in ecs_observer_desc_t. */
331#ifndef FLECS_EVENT_DESC_MAX
332#define FLECS_EVENT_DESC_MAX 8
333#endif
334
335/** @def FLECS_TERM_COUNT_MAX
336 * Maximum number of terms in queries. Should not exceed 64. */
337#ifndef FLECS_TERM_COUNT_MAX
338#define FLECS_TERM_COUNT_MAX 32
339#endif
340
341/** @def FLECS_TERM_ARG_COUNT_MAX
342 * Maximum number of arguments for a term. */
343#ifndef FLECS_TERM_ARG_COUNT_MAX
344#define FLECS_TERM_ARG_COUNT_MAX 16
345#endif
346
347#ifdef FLECS_QUERY_PLANS
348/** @def FLECS_QUERY_VARIABLE_COUNT_MAX
349 * Maximum number of query variables per query. Should not exceed 128. */
350#ifndef FLECS_QUERY_VARIABLE_COUNT_MAX
351#define FLECS_QUERY_VARIABLE_COUNT_MAX 64
352#endif
353#endif
354
355/** @def FLECS_QUERY_SCOPE_NESTING_MAX
356 * Maximum nesting depth of query scopes. */
357#ifndef FLECS_QUERY_SCOPE_NESTING_MAX
358#define FLECS_QUERY_SCOPE_NESTING_MAX 8
359#endif
360
361/** @def FLECS_DAG_DEPTH_MAX
362 * Maximum number of levels in a DAG (acyclic relationship graph). If a graph with a
363 * depth larger than this is encountered, a CYCLE_DETECTED panic is thrown.
364 */
365#ifndef FLECS_DAG_DEPTH_MAX
366#define FLECS_DAG_DEPTH_MAX 128
367#endif
368
369/** @def FLECS_TREE_SPAWNER_DEPTH_CACHE_SIZE
370 * Size of the depth cache in the tree spawner component. Higher values speed up prefab
371 * instantiation for deeper hierarchies, at the cost of slightly more memory.
372 */
373#define FLECS_TREE_SPAWNER_DEPTH_CACHE_SIZE (6)
374
375/** @} */
376
377#include "flecs/private/api_defines.h"
378
379/**
380 * @defgroup core_types Core API Types
381 * Types for core API objects.
382 *
383 * @{
384 */
385
386/** IDs are the things that can be added to an entity.
387 * An ID can be an entity or pair, and can have optional ID flags. */
388typedef uint64_t ecs_id_t;
389
390/** An entity identifier.
391 * Entity IDs consist of a number unique to the entity in the lower 32 bits,
392 * and a counter used to track entity liveliness in the upper 32 bits. When an
393 * ID is recycled, its generation count is increased. This causes recycled IDs
394 * to be very large (>4 billion), which is normal. */
396
397/** A type is a list of (component) IDs.
398 * Types are used to communicate the "type" of an entity. In most type systems, a
399 * typeof operation returns a single type. In ECS, however, an entity can have
400 * multiple components, which is why an ECS type consists of a vector of IDs.
401 *
402 * The component IDs of a type are sorted, which ensures that it doesn't matter
403 * in which order components are added to an entity. For example, if adding
404 * Position then Velocity would result in type [Position, Velocity], first
405 * adding Velocity then Position would also result in type [Position, Velocity].
406 *
407 * Entities are grouped together by type in the ECS storage in tables. The
408 * storage has exactly one table per unique type that is created by the
409 * application that stores all entities and components for that type. This is
410 * also referred to as an archetype.
411 */
412typedef struct {
413 ecs_id_t *array; /**< Array with IDs. */
414 int32_t count; /**< Number of elements in array. */
415} ecs_type_t;
416
417/** A world is the container for all ECS data and supporting features.
418 * Applications can have multiple worlds, though in most cases will only need
419 * one. Worlds are isolated from each other, and can have separate sets of
420 * systems, components, modules, etc.
421 *
422 * If an application has multiple worlds with overlapping components, it is
423 * common (though not strictly required) to use the same component IDs across
424 * worlds, which can be achieved by declaring a global component ID variable.
425 * To do this in the C API, see the entities/fwd_component_decl example. The
426 * C++ API automatically synchronizes component IDs between worlds.
427 *
428 * Component ID conflicts between worlds can occur when a world has already used
429 * an ID for something else. There are a few ways to avoid this:
430 *
431 * - Ensure to register the same components in each world, in the same order.
432 * - Create a dummy world in which all components are preregistered, which
433 * initializes the global ID variables.
434 *
435 * In some use cases, typically when writing tests, multiple worlds are created
436 * and deleted with different components, registered in different order. To
437 * ensure isolation between tests, the C++ API has a `flecs::reset` function
438 * that forces the API to ignore the old component IDs. */
440
441/** A stage enables modification while iterating and from multiple threads. */
443
444/** A table stores entities and components for a specific type. */
446
447/** A term is a single element in a query. */
448typedef struct ecs_term_t ecs_term_t;
449
450/** A query returns entities matching a list of constraints. */
452
453/** An observer is a system that is invoked when an event matches its query.
454 * Observers allow applications to respond to specific events, such as adding or
455 * removing a component. Observers are created by specifying both a query and
456 * a list of event kinds that should be listened for. An example of an observer
457 * that triggers when a Position component is added to an entity (in C++):
458 *
459 * @code
460 * world.observer<Position>()
461 * .event(flecs::OnAdd)
462 * .each([](Position& p) {
463 * // called when Position is added to an entity
464 * });
465 * @endcode
466 *
467 * Observers only trigger when the source of the event matches the full observer
468 * query. For example, an OnAdd observer for Position, Velocity will only
469 * trigger after both components have been added to the entity. */
471
472/** An observable produces events that can be listened for by an observer.
473 * Currently only the world is observable. In the future, queries will become
474 * observable objects as well. */
476
477/** Type used for iterating iterable objects.
478 * Iterators are objects that provide applications with information
479 * about the currently iterated result, and store any state required for the
480 * iteration. */
481typedef struct ecs_iter_t ecs_iter_t;
482
483/** A ref is a fast way to fetch a component for a specific entity.
484 * Refs are a faster alternative to repeatedly calling ecs_get() for the same
485 * entity/component combination. When comparing the performance of getting a ref
486 * to calling ecs_get(), a ref is typically 3-5x faster.
487 *
488 * Refs achieve this performance by caching internal data structures associated
489 * with the entity and component on the ecs_ref_t object that otherwise would
490 * have to be looked up. */
491typedef struct ecs_ref_t ecs_ref_t;
492
493/** Type hooks are callbacks associated with component lifecycle events.
494 * Typical examples of lifecycle events are construction, destruction, copying,
495 * and moving of components. */
497
498/** Type information.
499 * Contains information about a (component) type, such as its size,
500 * alignment, and type hooks. */
502
503/** Information about an entity, like its table and row. */
505
506/** Information about a (component) ID, such as type info and tables with the ID. */
508
509/** A poly object.
510 * A poly (short for polymorph) object is an object that has a variable list of
511 * capabilities, determined by a mixin table. This is the current list of types
512 * in the Flecs API that can be used as an ecs_poly_t:
513 *
514 * - ecs_world_t
515 * - ecs_stage_t
516 * - ecs_query_t
517 *
518 * Functions that accept an ecs_poly_t argument can accept objects of these
519 * types. If the object does not have the requested mixin, the API will throw an
520 * assert.
521 *
522 * The poly/mixin framework enables partially overlapping features to be
523 * implemented once, and enables objects of different types to interact with
524 * each other depending on what mixins they have, rather than their type
525 * (in some ways it's like a mini-ECS). Additionally, each poly object has a
526 * header that enables the API to do sanity checking on the input arguments.
527 */
528typedef void ecs_poly_t;
529
530/** Type that stores poly mixins. */
532
533/** Header for ecs_poly_t objects. */
534typedef struct ecs_header_t {
535 int32_t type; /**< Magic number indicating which type of Flecs object. */
536 int32_t refcount; /**< Refcount, to enable RAII handles. */
537 ecs_mixins_t *mixins; /**< Table with offsets to (optional) mixins. */
539
540/** Opaque type for table record. */
542
543/** @} */
544
545#include "flecs/datastructures/vec.h" /* Vector datatype */
546#include "flecs/datastructures/sparse.h" /* Sparse set */
547#include "flecs/datastructures/block_allocator.h" /* Block allocator */
548#include "flecs/datastructures/stack_allocator.h" /* Stack allocator */
549#include "flecs/datastructures/map.h" /* Map */
550#include "flecs/datastructures/allocator.h" /* Allocator */
551#include "flecs/datastructures/strbuf.h" /* String builder */
552#include "flecs/os_api.h" /* Abstraction for operating system functions */
553
554#ifdef __cplusplus
555extern "C" {
556#endif
557
558/**
559 * @defgroup api_types API types
560 * Public API types.
561 *
562 * @{
563 */
564
565
566/**
567 * @defgroup function_types Function types.
568 * Function callback types.
569 *
570 * @{
571 */
572
573/** Function prototype for runnables (systems, observers).
574 * The run callback overrides the default behavior for iterating through the
575 * results of a runnable object.
576 *
577 * The default runnable iterates the iterator, and calls an iter_action (see
578 * below) for each returned result.
579 *
580 * @param it The iterator to be iterated by the runnable.
581 */
582typedef void (*ecs_run_action_t)(
583 ecs_iter_t *it);
584
585/** Function prototype for iterables.
586 * A system may invoke a callback multiple times, typically once for each
587 * matched table.
588 *
589 * @param it The iterator containing the data for the current match.
590 */
591typedef void (*ecs_iter_action_t)(
592 ecs_iter_t *it);
593
594/** Function prototype for iterating an iterator.
595 * Stored inside initialized iterators. This allows an application to iterate
596 * an iterator without needing to know what created it.
597 *
598 * @param it The iterator to iterate.
599 * @return True if iterator has more results, false if not.
600 */
602 ecs_iter_t *it);
603
604/** Function prototype for freeing an iterator.
605 * Free iterator resources.
606 *
607 * @param it The iterator to free.
608 */
610 ecs_iter_t *it);
611
612/** Callback used for comparing components. */
614 ecs_entity_t e1,
615 const void *ptr1,
616 ecs_entity_t e2,
617 const void *ptr2);
618
619/** Callback used for sorting the entire table of components. */
621 ecs_world_t* world,
622 ecs_table_t* table,
623 ecs_entity_t* entities,
624 void* ptr,
625 int32_t size,
626 int32_t lo,
627 int32_t hi,
628 ecs_order_by_action_t order_by);
629
630/** Callback used for grouping tables in a query. */
631typedef uint64_t (*ecs_group_by_action_t)(
632 ecs_world_t *world,
633 ecs_table_t *table,
634 ecs_id_t group_id,
635 void *ctx);
636
637/** Callback invoked when a query creates a new group. */
638typedef void* (*ecs_group_create_action_t)(
639 ecs_world_t *world,
640 uint64_t group_id,
641 void *group_by_ctx); /* from ecs_query_desc_t */
642
643/** Callback invoked when a query deletes an existing group. */
645 ecs_world_t *world,
646 uint64_t group_id,
647 void *group_ctx, /* return value from ecs_group_create_action_t */
648 void *group_by_ctx); /* from ecs_query_desc_t */
649
650/** Initialization action for modules. */
651typedef void (*ecs_module_action_t)(
652 ecs_world_t *world);
653
654/** Action callback on world exit. */
655typedef void (*ecs_fini_action_t)(
656 ecs_world_t *world,
657 void *ctx);
658
659/** Function to clean up context data. */
660typedef void (*ecs_ctx_free_t)(
661 void *ctx);
662
663/** Callback used for sorting values. */
664typedef int (*ecs_compare_action_t)(
665 const void *ptr1,
666 const void *ptr2);
667
668/** Callback used for hashing values. */
669typedef uint64_t (*ecs_hash_value_action_t)(
670 const void *ptr);
671
672/** Constructor/destructor callback. */
673typedef void (*ecs_xtor_t)(
674 void *ptr,
675 int32_t count,
676 const ecs_type_info_t *type_info);
677
678/** Copy is invoked when a component is copied into another component. */
679typedef void (*ecs_copy_t)(
680 void *dst_ptr,
681 const void *src_ptr,
682 int32_t count,
683 const ecs_type_info_t *type_info);
684
685/** Move is invoked when a component is moved to another component. */
686typedef void (*ecs_move_t)(
687 void *dst_ptr,
688 void *src_ptr,
689 int32_t count,
690 const ecs_type_info_t *type_info);
691
692/** Compare hook to compare component instances. */
693typedef int (*ecs_cmp_t)(
694 const void *a_ptr,
695 const void *b_ptr,
696 const ecs_type_info_t *type_info);
697
698/** Equals operator hook. */
699typedef bool (*ecs_equals_t)(
700 const void *a_ptr,
701 const void *b_ptr,
702 const ecs_type_info_t *type_info);
703
704/** On validate hook. Invoked before on_set/OnSet hooks and observers. When the
705 * hook returns false, the on_set/OnSet hooks and observers are not invoked for
706 * the entity. */
707typedef bool (*ecs_on_validate_t)(
708 ecs_world_t *world,
709 ecs_entity_t entity,
710 void *ptr);
711
712/** Destructor function for poly objects. */
713typedef void (*flecs_poly_dtor_t)(
714 ecs_poly_t *poly);
715
716/** @} */
717
718/**
719 * @defgroup query_types Query descriptor types.
720 * Types used to describe queries.
721 *
722 * @{
723 */
724
725/** Specify read/write access for term. */
726typedef enum ecs_inout_kind_t {
727 EcsInOutDefault, /**< InOut for regular terms, In for shared terms. */
728 EcsInOutNone, /**< Term is neither read nor written. */
729 EcsInOutFilter, /**< Same as InOutNone + prevents term from triggering observers. */
730 EcsInOut, /**< Term is both read and written. */
731 EcsIn, /**< Term is only read. */
732 EcsOut, /**< Term is only written. */
734
735/** Specify operator for term. */
736typedef enum ecs_oper_kind_t {
737 EcsAnd, /**< The term must match. */
738 EcsOr, /**< One of the terms in an or chain must match. */
739 EcsNot, /**< The term must not match. */
740 EcsOptional, /**< The term may match. */
741 EcsAndFrom, /**< Term must match all components from term ID. */
742 EcsOrFrom, /**< Term must match at least one component from term ID. */
743 EcsNotFrom, /**< Term must match none of the components from term ID. */
745
746/** Specify cache policy for query. */
748 EcsQueryCacheDefault, /**< Behavior determined by query creation context. */
749 EcsQueryCacheAuto, /**< Cache query terms that are cacheable. */
750#ifdef FLECS_CACHED_QUERIES
751 EcsQueryCacheAll, /**< Require that all query terms can be cached. */
752#endif
753 EcsQueryCacheNone, /**< No caching. */
755
756/** Term ID flags. */
757
758/** Match on self.
759 * Can be combined with other term flags on the ecs_term_ref_t::id field.
760 * \ingroup queries
761 */
762#define EcsSelf (1llu << 63)
763
764/** Match by traversing upwards.
765 * Can be combined with other term flags on the ecs_term_ref_t::id field.
766 * \ingroup queries
767 */
768#define EcsUp (1llu << 62)
769
770/** Traverse relationship transitively.
771 * Can be combined with other term flags on the ecs_term_ref_t::id field.
772 * \ingroup queries
773 */
774#define EcsTrav (1llu << 61)
775
776/** Sort results breadth-first.
777 * Can be combined with other term flags on the ecs_term_ref_t::id field.
778 * \ingroup queries
779 */
780#define EcsCascade (1llu << 60)
781
782/** Iterate groups in descending order.
783 * Can be combined with other term flags on the ecs_term_ref_t::id field.
784 * \ingroup queries
785 */
786#define EcsDesc (1llu << 59)
787
788/** Term ID is a variable.
789 * Can be combined with other term flags on the ecs_term_ref_t::id field.
790 * \ingroup queries
791 */
792#define EcsIsVariable (1llu << 58)
793
794/** Term ID is an entity.
795 * Can be combined with other term flags on the ecs_term_ref_t::id field.
796 * \ingroup queries
797 */
798#define EcsIsEntity (1llu << 57)
799
800/** Term ID is a name (don't attempt to look up as an entity).
801 * Can be combined with other term flags on the ecs_term_ref_t::id field.
802 * \ingroup queries
803 */
804#define EcsIsName (1llu << 56)
805
806/** All term traversal flags.
807 * Can be combined with other term flags on the ecs_term_ref_t::id field.
808 * \ingroup queries
809 */
810#define EcsTraverseFlags (EcsSelf|EcsUp|EcsTrav|EcsCascade|EcsDesc)
811
812/** All term reference kind flags.
813 * Can be combined with other term flags on the ecs_term_ref_t::id field.
814 * \ingroup queries
815 */
816#define EcsTermRefFlags (EcsTraverseFlags|EcsIsVariable|EcsIsEntity|EcsIsName)
817
818/** Type that describes a reference to an entity or variable in a term. */
819typedef struct ecs_term_ref_t {
820 ecs_entity_t id; /**< Entity ID. If left to 0 and flags do not
821 * specify whether the ID is an entity or a variable,
822 * the ID will be initialized to #EcsThis.
823 * To explicitly set the ID to 0, leave the ID
824 * member to 0 and set #EcsIsEntity in flags. */
825
826 const char *name; /**< Name. This can be either the variable name
827 * (when the #EcsIsVariable flag is set) or an
828 * entity name. When ecs_term_t::move is true,
829 * the API assumes ownership over the string and
830 * will free it when the term is destroyed. */
832
833/** Type that describes a term (single element in a query). */
835 ecs_id_t id; /**< Component ID to be matched by term. Can be
836 * set directly, or will be populated from the
837 * first/second members, which provide more
838 * flexibility. */
839
840 ecs_term_ref_t src; /**< Source of term. */
841 ecs_term_ref_t first; /**< Component or first element of pair. */
842 ecs_term_ref_t second; /**< Second element of pair. */
843
844 ecs_entity_t trav; /**< Relationship to traverse when looking for the
845 * component. The relationship must have
846 * the `Traversable` property. Default is `IsA`. */
847
848 int16_t inout; /**< Access to contents matched by term. */
849 int16_t oper; /**< Operator of term. */
850
851 int8_t field_index; /**< Index of the field for the term in the iterator. */
852 ecs_flags16_t flags_; /**< Flags that help evaluation, set by ecs_query_init(). */
853};
854
855/** Queries are lists of constraints (terms) that match entities.
856 * Created with ecs_query_init().
857 */
859 ecs_header_t hdr; /**< Object header. */
860
861 ecs_term_t *terms; /**< Query terms. */
862 int32_t *sizes; /**< Component sizes. Indexed by field. */
863 ecs_id_t *ids; /**< Component ids. Indexed by field. */
864
865 uint64_t bloom_filter; /**< Bitmask used to quickly discard tables. */
866 ecs_flags32_t flags; /**< Query flags. */
867#ifdef FLECS_QUERY_PLANS
868 int8_t var_count; /**< Number of query variables. */
869#endif
870 int8_t term_count; /**< Number of query terms. */
871 int8_t field_count; /**< Number of fields returned by the query. */
872
873 /** Bitmasks for quick field information lookups. */
874 ecs_termset_t fixed_fields; /**< Fields with a fixed source. */
875 ecs_termset_t var_fields; /**< Fields with non-$this variable source. */
876 ecs_termset_t static_id_fields; /**< Fields with a static (component) id. */
877 ecs_termset_t data_fields; /**< Fields that have data. */
878 ecs_termset_t write_fields; /**< Fields that write data. */
879 ecs_termset_t read_fields; /**< Fields that read data. */
880 ecs_termset_t row_fields; /**< Fields that must be acquired with field_at. */
881 ecs_termset_t shared_readonly_fields; /**< Fields that don't write shared data. */
882 ecs_termset_t set_fields; /**< Fields that will be set. */
883
884 ecs_query_cache_kind_t cache_kind; /**< Caching policy of the query. */
885
886#ifdef FLECS_QUERY_PLANS
887 char **vars; /**< Array with variable names for the iterator. */
888#endif
889
890 void *ctx; /**< User context to pass to callback. */
891 void *binding_ctx; /**< Context to be used for language bindings. */
892
893 ecs_entity_t entity; /**< Entity associated with query (optional). */
894 ecs_world_t *real_world; /**< Actual world. */
895 ecs_world_t *world; /**< World or stage the query was created with. */
896
897 int32_t eval_count; /**< Number of times the query is evaluated. */
898};
899
900/** An observer reacts to events matching a query.
901 * Created with ecs_observer_init().
902 */
904 ecs_header_t hdr; /**< Object header. */
905
906 ecs_query_t *query; /**< Observer query. */
907
908 /** Observer events. */
910 int32_t event_count; /**< Number of events. */
911
912 ecs_iter_action_t callback; /**< See ecs_observer_desc_t::callback. */
913 ecs_run_action_t run; /**< See ecs_observer_desc_t::run. */
914
915 void *ctx; /**< Observer context. */
916 void *callback_ctx; /**< Callback language binding context. */
917 void *run_ctx; /**< Run language binding context. */
918
919 ecs_ctx_free_t ctx_free; /**< Callback to free ctx. */
920 ecs_ctx_free_t callback_ctx_free; /**< Callback to free callback_ctx. */
921 ecs_ctx_free_t run_ctx_free; /**< Callback to free run_ctx. */
922
923 ecs_observable_t *observable; /**< Observable for the observer. */
924
925 ecs_world_t *world; /**< The world. */
926 ecs_entity_t entity; /**< Entity associated with the observer. */
927};
928
929/** @} */
930
931/** Type that contains component lifecycle callbacks.
932 *
933 * @ingroup components
934 */
935
936/* Flags that can be used to check which hooks a type has set */
937#define ECS_TYPE_HOOK_CTOR ECS_CAST(ecs_flags32_t, 1 << 0)
938#define ECS_TYPE_HOOK_DTOR ECS_CAST(ecs_flags32_t, 1 << 1)
939#define ECS_TYPE_HOOK_COPY ECS_CAST(ecs_flags32_t, 1 << 2)
940#define ECS_TYPE_HOOK_MOVE ECS_CAST(ecs_flags32_t, 1 << 3)
941#define ECS_TYPE_HOOK_COPY_CTOR ECS_CAST(ecs_flags32_t, 1 << 4)
942#define ECS_TYPE_HOOK_MOVE_CTOR ECS_CAST(ecs_flags32_t, 1 << 5)
943#define ECS_TYPE_HOOK_CTOR_MOVE_DTOR ECS_CAST(ecs_flags32_t, 1 << 6)
944#define ECS_TYPE_HOOK_MOVE_DTOR ECS_CAST(ecs_flags32_t, 1 << 7)
945#define ECS_TYPE_HOOK_CMP ECS_CAST(ecs_flags32_t, 1 << 8)
946#define ECS_TYPE_HOOK_EQUALS ECS_CAST(ecs_flags32_t, 1 << 9)
947
948
949/* Flags that can be used to set/check which hooks of a type are invalid */
950#define ECS_TYPE_HOOK_CTOR_ILLEGAL ECS_CAST(ecs_flags32_t, 1 << 10)
951#define ECS_TYPE_HOOK_DTOR_ILLEGAL ECS_CAST(ecs_flags32_t, 1 << 12)
952#define ECS_TYPE_HOOK_COPY_ILLEGAL ECS_CAST(ecs_flags32_t, 1 << 13)
953#define ECS_TYPE_HOOK_MOVE_ILLEGAL ECS_CAST(ecs_flags32_t, 1 << 14)
954#define ECS_TYPE_HOOK_COPY_CTOR_ILLEGAL ECS_CAST(ecs_flags32_t, 1 << 15)
955#define ECS_TYPE_HOOK_MOVE_CTOR_ILLEGAL ECS_CAST(ecs_flags32_t, 1 << 16)
956#define ECS_TYPE_HOOK_CTOR_MOVE_DTOR_ILLEGAL ECS_CAST(ecs_flags32_t, 1 << 17)
957#define ECS_TYPE_HOOK_MOVE_DTOR_ILLEGAL ECS_CAST(ecs_flags32_t, 1 << 18)
958#define ECS_TYPE_HOOK_CMP_ILLEGAL ECS_CAST(ecs_flags32_t, 1 << 19)
959#define ECS_TYPE_HOOK_EQUALS_ILLEGAL ECS_CAST(ecs_flags32_t, 1 << 20)
960
961/* Internal debug flag that indicates type hooks have been invoked */
962#define ECS_TYPE_HOOK_IN_USE ECS_CAST(ecs_flags32_t, 1 << 21)
963
964
965/* All valid hook flags */
966#define ECS_TYPE_HOOKS (ECS_TYPE_HOOK_CTOR|ECS_TYPE_HOOK_DTOR|\
967 ECS_TYPE_HOOK_COPY|ECS_TYPE_HOOK_MOVE|ECS_TYPE_HOOK_COPY_CTOR|\
968 ECS_TYPE_HOOK_MOVE_CTOR|ECS_TYPE_HOOK_CTOR_MOVE_DTOR|\
969 ECS_TYPE_HOOK_MOVE_DTOR|ECS_TYPE_HOOK_CMP|ECS_TYPE_HOOK_EQUALS)
970
971/* All invalid hook flags */
972#define ECS_TYPE_HOOKS_ILLEGAL (ECS_TYPE_HOOK_CTOR_ILLEGAL|\
973 ECS_TYPE_HOOK_DTOR_ILLEGAL|ECS_TYPE_HOOK_COPY_ILLEGAL|\
974 ECS_TYPE_HOOK_MOVE_ILLEGAL|ECS_TYPE_HOOK_COPY_CTOR_ILLEGAL|\
975 ECS_TYPE_HOOK_MOVE_CTOR_ILLEGAL|ECS_TYPE_HOOK_CTOR_MOVE_DTOR_ILLEGAL|\
976 ECS_TYPE_HOOK_MOVE_DTOR_ILLEGAL|ECS_TYPE_HOOK_CMP_ILLEGAL|\
977 ECS_TYPE_HOOK_EQUALS_ILLEGAL)
979 ecs_xtor_t ctor; /**< ctor. */
980 ecs_xtor_t dtor; /**< dtor. */
981 ecs_copy_t copy; /**< copy assignment. */
982 ecs_move_t move; /**< move assignment. */
983
984 /** Ctor + copy. */
986
987 /** Ctor + move. */
989
990 /** Ctor + move + dtor (or move_ctor + dtor).
991 * This combination is typically used when a component is moved from one
992 * location to a new location, like when it is moved to a new table. If
993 * not set explicitly, it will be derived from other callbacks. */
995
996 /** Move + dtor.
997 * This combination is typically used when a component is moved from one
998 * location to an existing location, like what happens during a remove. If
999 * not set explicitly, it will be derived from other callbacks. */
1001
1002 /** Compare hook. */
1004
1005 /** Equals hook. */
1007
1008 /** Hook flags.
1009 * Indicates which hooks are set for the type, and which hooks are illegal.
1010 * When an ILLEGAL flag is set when calling ecs_set_hooks(), a hook callback
1011 * will be set that panics when called. */
1012 ecs_flags32_t flags;
1013
1014 /** Callback that is invoked when an instance of a component is added. This
1015 * callback is invoked before observers are invoked. */
1017
1018 /** Callback that is invoked when an instance of the component is set. This
1019 * callback is invoked before observers are invoked, and enables the component
1020 * to respond to changes on itself before others can. */
1022
1023 /** Callback that is invoked when an instance of the component is removed.
1024 * This callback is invoked after the observers are invoked, and before the
1025 * destructor is invoked. */
1027
1028 /** Callback that is invoked with the existing and new value before the
1029 * value is assigned. Invoked after on_add and before on_set. Registering
1030 * an on_replace hook prevents using operations that return a mutable
1031 * pointer to the component, like get_mut(), ensure(), and emplace(). */
1033
1034 /** Callback that is invoked before the on_set/OnSet hooks and observers are
1035 * invoked. When the callback returns false, the on_set/OnSet hooks and
1036 * observers are not invoked for the entity. */
1038
1039 void *ctx; /**< User-defined context. */
1040 void *binding_ctx; /**< Language binding context. */
1041 void *lifecycle_ctx; /**< Component lifecycle context (see meta addon). */
1042
1043 ecs_ctx_free_t ctx_free; /**< Callback to free ctx. */
1044 ecs_ctx_free_t binding_ctx_free; /**< Callback to free binding_ctx. */
1045 ecs_ctx_free_t lifecycle_ctx_free; /**< Callback to free lifecycle_ctx. */
1046};
1047
1048/** Type that contains component information (passed to ctors/dtors/...).
1049 *
1050 * @ingroup components
1051 */
1053 ecs_size_t size; /**< Size of the type. */
1054 ecs_size_t alignment; /**< Alignment of the type. */
1055 ecs_type_hooks_t hooks; /**< Type hooks. */
1056 ecs_entity_t component; /**< Handle to component (do not set). */
1057 const char *name; /**< Type name. */
1058 int32_t refcount; /**< Refcount (do not set). */
1059};
1060
1061#include "flecs/private/api_types.h" /* Supporting API types */
1062#include "flecs/private/api_support.h" /* Supporting API functions */
1063#include "flecs/datastructures/hashmap.h" /* Hashmap */
1064#include "flecs/private/api_internals.h" /* Supporting API functions */
1065
1066/** Value of a dynamic type.
1067 * See the meta addon for functions to create, assign and destruct values. */
1068typedef struct ecs_value_t {
1069 ecs_entity_t type; /**< Type of value. */
1070 void *ptr; /**< Pointer to value. */
1072
1073/** Used with ecs_entity_init().
1074 *
1075 * @ingroup entities
1076 */
1077typedef struct ecs_entity_desc_t {
1078 int32_t _canary; /**< Used for validity testing. Must be 0. */
1079
1080 ecs_entity_t id; /**< Set to modify existing entity (optional). */
1081
1082 ecs_entity_t parent; /**< Parent entity. */
1083
1084 const char *name; /**< Name of the entity. If no entity is provided, an
1085 * entity with this name will be looked up first. When
1086 * an entity is provided, the name will be verified
1087 * with the existing entity. */
1088
1089 const char *sep; /**< Optional custom separator for hierarchical names.
1090 * Leave to NULL for the default ('.') separator. Set to
1091 * an empty string to prevent tokenization of the name. */
1092
1093 const char *root_sep; /**< Optional, used for identifiers relative to the root. */
1094
1095 const char *symbol; /**< Optional entity symbol. A symbol is an unscoped
1096 * identifier that can be used to look up an entity. The
1097 * primary use case for this is to associate the entity
1098 * with a language identifier, such as a type or
1099 * function name, where these identifiers differ from
1100 * the name they are registered with in Flecs. For
1101 * example, C type "EcsPosition" might be registered
1102 * as "flecs.components.transform.Position", with the
1103 * symbol set to "EcsPosition". */
1104
1105 bool use_low_id; /**< When set to true, a low id (typically reserved for
1106 * components) will be used to create the entity, if
1107 * no ID is specified. */
1109
1110/** Used with ecs_bulk_init().
1111 *
1112 * @ingroup entities
1113 */
1114typedef struct ecs_bulk_desc_t {
1115 int32_t _canary; /**< Used for validity testing. Must be 0. */
1116
1117 ecs_entity_t *entities; /**< Entities to bulk insert. Entity IDs provided by
1118 * the application must be empty (cannot
1119 * have components). If no entity IDs are provided, the
1120 * operation will create 'count' new entities. */
1121
1122 int32_t count; /**< Number of entities to create/populate. */
1123
1124 ecs_id_t ids[FLECS_ID_DESC_MAX]; /**< IDs to create the entities with. */
1125
1126 void **data; /**< Array with component data to insert. Each element in
1127 * the array must correspond with an element in the ids
1128 * array. If an element in the ids array is a tag, the
1129 * data array must contain a NULL. An element may be
1130 * set to NULL for a component, in which case the
1131 * component will not be set by the operation. */
1132
1133 ecs_table_t *table; /**< Table to insert the entities into. Should not be set
1134 * at the same time as ids. When 'table' is set at the
1135 * same time as 'data', the elements in the data array
1136 * must correspond with the ids in the table's type. */
1137
1139
1140/** Used with ecs_component_init().
1141 *
1142 * @ingroup components
1143 */
1144typedef struct ecs_component_desc_t {
1145 int32_t _canary; /**< Used for validity testing. Must be 0. */
1146
1147 /** Existing entity to associate with a component (optional). */
1149
1150 /** Parameters for type (size, hooks, ...). */
1153
1154/** Iterator.
1155 * Used for iterating queries. The ecs_iter_t type contains all the information
1156 * that is provided by a query, and contains all the state required for the
1157 * iterator code.
1158 *
1159 * Functions that create iterators accept as first argument the world, and as
1160 * second argument the object they iterate. For example:
1161 *
1162 * @code
1163 * ecs_iter_t it = ecs_query_iter(world, q);
1164 * @endcode
1165 *
1166 * When this code is called from a system, it is important to use the world
1167 * provided by its iterator object to ensure thread safety. For example:
1168 *
1169 * @code
1170 * void Collide(ecs_iter_t *it) {
1171 * ecs_iter_t qit = ecs_query_iter(it->world, Colliders);
1172 * }
1173 * @endcode
1174 *
1175 * An iterator contains resources that need to be released. By default, this
1176 * is handled by the last call to next() that returns false. When iteration is
1177 * ended before iteration has completed, an application has to manually call
1178 * ecs_iter_fini() to release the iterator resources:
1179 *
1180 * @code
1181 * ecs_iter_t it = ecs_query_iter(world, q);
1182 * while (ecs_query_next(&it)) {
1183 * if (cond) {
1184 * ecs_iter_fini(&it);
1185 * break;
1186 * }
1187 * }
1188 * @endcode
1189 *
1190 * @ingroup queries
1191 */
1193 /* World */
1194 ecs_world_t *world; /**< The world. Can point to a stage when in deferred or readonly mode. */
1195 ecs_world_t *real_world; /**< Actual world. Never points to a stage. */
1196
1197 /* Matched data */
1198 int32_t offset; /**< Offset relative to the current table. */
1199 int32_t count; /**< Number of entities to iterate. */
1200 const ecs_entity_t *entities; /**< Entity identifiers. */
1201 void **ptrs; /**< Component pointers. If not set or if it is NULL for a field, use it->trs. */
1202 const ecs_table_record_t **trs; /**< Info on where to find the field in the table. */
1203 const int16_t *columns;
1204 const ecs_size_t *sizes; /**< Component sizes. */
1205 ecs_table_t *table; /**< Current table. */
1206 ecs_table_t *other_table; /**< Previous or next table when adding or removing. */
1207 ecs_id_t *ids; /**< (Component) IDs. */
1208 ecs_entity_t *sources; /**< Entity on which the ID was matched (0 if same as entities). */
1209#ifdef FLECS_QUERY_PLANS
1210 ecs_flags64_t constrained_vars; /**< Bitset that marks constrained variables. */
1211#endif
1212 ecs_termset_t set_fields; /**< Fields that are set. */
1213 ecs_termset_t ref_fields; /**< Bitset with fields that aren't component arrays. */
1214 ecs_termset_t row_fields; /**< Fields that must be obtained with field_at. */
1215 ecs_termset_t up_fields; /**< Bitset with fields matched through up traversal. */
1216
1217 /* Input information */
1218 ecs_entity_t system; /**< The system (if applicable). */
1219 ecs_entity_t event; /**< The event (if applicable). */
1220 ecs_id_t event_id; /**< The (component) ID for the event. */
1221 int32_t event_cur; /**< Unique event ID. Used to dedup observer calls. */
1222
1223 /* Query information */
1224 int8_t field_count; /**< Number of fields in the iterator. */
1225 int8_t term_index; /**< Index of the term that emitted an event.
1226 * This field will be set to the 'index' field
1227 * of an observer term. */
1228 const ecs_query_t *query; /**< Query being evaluated. */
1229
1230 /* Context */
1231 void *param; /**< Param passed to ecs_run(). */
1232 void *ctx; /**< System context. */
1233 void *binding_ctx; /**< System binding context. */
1234 void *callback_ctx; /**< Callback language binding context. */
1235 void *run_ctx; /**< Run language binding context. */
1236
1237 /* Time */
1238 ecs_ftime_t delta_time; /**< Time elapsed since last frame. */
1239 ecs_ftime_t delta_system_time;/**< Time elapsed since last system invocation. */
1240
1241 /* Iterator counters */
1242 int32_t frame_offset; /**< Offset relative to the start of iteration. */
1243
1244 /* Misc */
1245 ecs_flags32_t flags; /**< Iterator flags. */
1246 ecs_entity_t interrupted_by; /**< When set, system execution is interrupted. */
1247 ecs_iter_private_t priv_; /**< Private data. */
1248
1249 /* Chained iterators */
1250 ecs_iter_next_action_t next; /**< Function to progress iterator. */
1251 ecs_iter_action_t callback; /**< Callback of system or observer. */
1252 ecs_iter_fini_action_t fini; /**< Function to clean up iterator resources. */
1253 ecs_iter_t *chain_it; /**< Optional, allows for creating iterator chains. */
1254};
1255
1256
1257/** Query must match prefabs.
1258 * Can be combined with other query flags on the ecs_query_desc_t::flags field.
1259 * \ingroup queries
1260 */
1261#define EcsQueryMatchPrefab (1u << 1u)
1262
1263/** Query must match disabled entities.
1264 * Can be combined with other query flags on the ecs_query_desc_t::flags field.
1265 * \ingroup queries
1266 */
1267#define EcsQueryMatchDisabled (1u << 2u)
1268
1269/** Query must match empty tables.
1270 * Can be combined with other query flags on the ecs_query_desc_t::flags field.
1271 * \ingroup queries
1272 */
1273#define EcsQueryMatchEmptyTables (1u << 3u)
1274
1275/** Query may have unresolved entity identifiers.
1276 * Can be combined with other query flags on the ecs_query_desc_t::flags field.
1277 * \ingroup queries
1278 */
1279#define EcsQueryAllowUnresolvedByName (1u << 6u)
1280
1281/** Query only returns whole tables (ignores toggle or member fields).
1282 * Can be combined with other query flags on the ecs_query_desc_t::flags field.
1283 * \ingroup queries
1284 */
1285#define EcsQueryTableOnly (1u << 7u)
1286
1287/** Enable change detection for a query.
1288 * Can be combined with other query flags on the ecs_query_desc_t::flags field.
1289 *
1290 * Adding this flag makes it possible to use ecs_query_changed() and
1291 * ecs_iter_changed() with the query. Change detection requires the query to be
1292 * cached. If cache_kind is left to the default value, this flag will cause it
1293 * to default to EcsQueryCacheAuto.
1294 *
1295 * \ingroup queries
1296 */
1297#define EcsQueryDetectChanges (1u << 8u)
1298
1299/** Enable ordering for query groups.
1300 * When this flag is set, groups will be iterated in ascending order, with lower
1301 * group ids first and higher group ids afterwards.
1302 *
1303 * This flag is enabled automatically when a query contains cascade terms.
1304 *
1305 * \ingroup queries
1306 */
1307#define EcsQueryGroupByOrdered (1u << 9u)
1308
1309/** Enable descending ordering for query groups.
1310 * When this flag is set in combination with EcsQueryGroupByOrdered, groups will
1311 * be iterated in descending order, with higher group ids first and lower group
1312 * ids afterwards.
1313 *
1314 * This flag is enabled automatically when a query contains cascade|desc terms.
1315 *
1316 * \ingroup queries
1317 */
1318#define EcsQueryGroupByDesc (1u << 10u)
1319
1320
1321/** Used with ecs_query_init().
1322 *
1323 * \ingroup queries
1324 */
1325typedef struct ecs_query_desc_t {
1326 /** Used for validity testing. Must be 0. */
1327 int32_t _canary;
1328
1329 /** Query terms. */
1331
1332 /** Query DSL expression (optional). */
1333 const char *expr;
1334
1335 /** Caching policy of the query. */
1337
1338 /** Flags for enabling query features. */
1339 ecs_flags32_t flags;
1340
1341 /** Callback used for ordering query results. If order_by is 0, the
1342 * pointer provided to the callback will be NULL. If the callback is not
1343 * set, results will not be ordered. */
1345
1346 /** Callback used for ordering query results. Same as order_by_callback,
1347 * but more efficient. */
1349
1350 /** Component to sort on, used together with order_by_callback or
1351 * order_by_table_callback. */
1353
1354 /** Component ID to be used for grouping. Used together with the
1355 * group_by_callback. */
1357
1358 /** Callback used for grouping results. If the callback is not set, results
1359 * will not be grouped. When set, this callback will be used to calculate a
1360 * "rank" for each entity (table) based on its components. This rank is then
1361 * used to sort entities (tables), so that entities (tables) of the same
1362 * rank are "grouped" together when iterated. */
1364
1365 /** Callback that is invoked when a new group is created. The return value of
1366 * the callback is stored as context for a group. */
1368
1369 /** Callback that is invoked when an existing group is deleted. The return
1370 * value of the on_group_create callback is passed as context parameter. */
1372
1373 /** Context to pass to group_by. */
1375
1376 /** Function to free group_by_ctx. */
1378
1379 /** User context to pass to callback. */
1380 void *ctx;
1381
1382 /** Context to be used for language bindings. */
1384
1385 /** Callback to free ctx. */
1387
1388 /** Callback to free binding_ctx. */
1390
1391 /** Entity associated with query (optional). */
1394
1395/** Used with ecs_observer_init().
1396 *
1397 * @ingroup observers
1398 */
1399typedef struct ecs_observer_desc_t {
1400 /** Used for validity testing. Must be 0. */
1401 int32_t _canary;
1402
1403 /** Existing entity to associate with an observer (optional). */
1405
1406 /** Query for observer. */
1408
1409 /** Events to observe (OnAdd, OnRemove, OnSet). */
1411
1412 /** When an observer is created, generate events from existing data. For example,
1413 * #EcsOnAdd `Position` would match all existing instances of `Position`. */
1415
1416 /** Global observers are tied to the lifespan of the world. Creating a
1417 * global observer does not create an entity, and therefore
1418 * ecs_observer_init() will not return an entity handle. */
1420
1421 /** Callback to invoke on an event, invoked when the observer matches. */
1423
1424 /** Callback invoked on an event. When left to NULL, the default runner
1425 * is used, which matches the event with the observer's query, and calls
1426 * 'callback' when it matches.
1427 * A reason to override the run function is to improve performance, if there
1428 * are more efficient ways to test whether an event matches the observer than
1429 * the general-purpose query matcher. */
1431
1432 /** User context to pass to callback. */
1433 void *ctx;
1434
1435 /** Callback to free ctx. */
1437
1438 /** Context associated with callback (for language bindings). */
1440
1441 /** Callback to free callback ctx. */
1443
1444 /** Context associated with run (for language bindings). */
1445 void *run_ctx;
1446
1447 /** Callback to free run ctx. */
1449
1450 /** Used for internal purposes. Do not set. */
1452 int8_t term_index_; /**< Used for internal purposes. Do not set. */
1453 ecs_flags32_t flags_; /**< Used for internal purposes. Do not set. */
1455
1456/** Used with ecs_emit().
1457 *
1458 * @ingroup observers
1459 */
1460typedef struct ecs_event_desc_t {
1461 /** The event ID. Only observers for the specified event will be notified. */
1463
1464 /** Component IDs. Only observers with a matching component ID will be
1465 * notified. Observers are guaranteed to get notified once, even if they
1466 * match more than one ID. */
1468
1469 /** The table for which to notify. */
1471
1472 /** Optional second table to notify. This can be used to communicate the
1473 * previous or next table, in case an entity is moved between tables. */
1475
1476 /** Limit notified entities to ones starting from offset (row) in table. */
1477 int32_t offset;
1478
1479 /** Limit number of notified entities to count. offset+count must be less
1480 * than the total number of entities in the table. If left to 0, it will be
1481 * automatically determined by doing `ecs_table_count(table) - offset`. */
1482 int32_t count;
1483
1484 /** Single-entity alternative to setting table / offset / count. */
1486
1487 /** Optional context.
1488 * The type of the param must be the event, where the event is a component.
1489 * When an event is enqueued, the value of param is copied to a temporary
1490 * storage of the event type. */
1491 void *param;
1492
1493 /** Same as param, but with the guarantee that the value won't be modified.
1494 * When an event with a const parameter is enqueued, the value of the param
1495 * is copied to a temporary storage of the event type. */
1496 const void *const_param;
1497
1498 /** Optional pointer to the value of the component for which the event is
1499 * emitted. May only be set for events that are emitted for a single
1500 * component id and a single entity. If provided, observers will use this
1501 * pointer instead of fetching the component from the table/storage. */
1502 void *set_ptr;
1503
1504 /** Observable (usually the world). */
1506
1507 /** Event flags. */
1508 ecs_flags32_t flags;
1510
1511
1512/**
1513 * @defgroup misc_types Miscellaneous types
1514 * Types used to create entities, observers, queries, and more.
1515 *
1516 * @{
1517 */
1518
1519/** Type with information about the current Flecs build. */
1520typedef struct ecs_build_info_t {
1521 const char *compiler; /**< Compiler used to compile Flecs. */
1522 const char **addons; /**< Addons included in the build. */
1523 const char **flags; /**< Compile-time settings. */
1524 const char *version; /**< Stringified version. */
1525 int16_t version_major; /**< Major Flecs version. */
1526 int16_t version_minor; /**< Minor Flecs version. */
1527 int16_t version_patch; /**< Patch Flecs version. */
1528 bool debug; /**< Is this a debug build? */
1529 bool sanitize; /**< Is this a sanitize build? */
1530 bool perf_trace; /**< Is this a perf tracing build? */
1532
1533/** Type that contains information about the world. */
1534typedef struct ecs_world_info_t {
1535 ecs_entity_t last_component_id; /**< Last issued component entity ID. */
1536
1537 ecs_ftime_t delta_time_raw; /**< Raw delta time (no time scaling). */
1538 ecs_ftime_t delta_time; /**< Time passed to or computed by ecs_progress(). */
1539 ecs_ftime_t time_scale; /**< Time scale applied to delta_time. */
1540 ecs_ftime_t target_fps; /**< Target FPS. */
1541 ecs_ftime_t frame_time_total; /**< Total time spent processing a frame. */
1542 ecs_ftime_t system_time_total; /**< Total time spent in systems. */
1543 ecs_ftime_t emit_time_total; /**< Total time spent notifying observers. */
1544 ecs_ftime_t merge_time_total; /**< Total time spent in merges. */
1545 ecs_ftime_t rematch_time_total; /**< Time spent on query rematching. */
1546 double world_time_total; /**< Time elapsed in simulation. */
1547 double world_time_total_raw; /**< Time elapsed in simulation (no scaling). */
1548
1549 int64_t frame_count_total; /**< Total number of frames. */
1550 int64_t merge_count_total; /**< Total number of merges. */
1551 int64_t eval_comp_monitors_total; /**< Total number of monitor evaluations. */
1552 int64_t rematch_count_total; /**< Total number of rematches. */
1553
1554 int64_t id_create_total; /**< Total number of times a new ID was created. */
1555 int64_t id_delete_total; /**< Total number of times an ID was deleted. */
1556 int64_t table_create_total; /**< Total number of times a table was created. */
1557 int64_t table_delete_total; /**< Total number of times a table was deleted. */
1558 int64_t pipeline_build_count_total; /**< Total number of pipeline builds. */
1559 int64_t systems_ran_total; /**< Total number of systems run. */
1560 int64_t observers_ran_total; /**< Total number of times an observer was invoked. */
1561 int64_t queries_ran_total; /**< Total number of times a query was evaluated. */
1562
1563 int32_t tag_id_count; /**< Number of tag (no data) IDs in the world. */
1564 int32_t component_id_count; /**< Number of component (data) IDs in the world. */
1565 int32_t pair_id_count; /**< Number of pair IDs in the world. */
1566
1567 int32_t table_count; /**< Number of tables. */
1568
1569 uint32_t creation_time; /**< Time when world was created. */
1570
1571 /* -- Command counts -- */
1572 struct {
1573 int64_t add_count; /**< Add commands processed. */
1574 int64_t remove_count; /**< Remove commands processed. */
1575 int64_t delete_count; /**< Delete commands processed. */
1576 int64_t clear_count; /**< Clear commands processed. */
1577 int64_t set_count; /**< Set commands processed. */
1578 int64_t ensure_count; /**< Ensure or emplace commands processed. */
1579 int64_t modified_count; /**< Modified commands processed. */
1580 int64_t discard_count; /**< Commands discarded, happens when the entity is no longer alive when running the command. */
1581 int64_t event_count; /**< Enqueued custom events. */
1582 int64_t other_count; /**< Other commands processed. */
1583 int64_t batched_entity_count; /**< Entities for which commands were batched. */
1584 int64_t batched_command_count; /**< Commands batched. */
1585 } cmd; /**< Command statistics. */
1586
1587 const char *name_prefix; /**< Value set by ecs_set_name_prefix(). Used
1588 * to remove library prefixes of symbol
1589 * names (such as `Ecs`, `ecs_`) when
1590 * registering them as names. */
1592
1593/** Type that contains information about a query group. */
1595 uint64_t id; /**< Group ID. */
1596 int32_t match_count; /**< How often tables have been matched or unmatched. */
1597 int32_t table_count; /**< Number of tables in group. */
1598 void *ctx; /**< Group context, returned by on_group_create. */
1600
1601/** @} */
1602
1603/**
1604 * @defgroup builtin_components Built-in component types.
1605 * Types that represent built-in components.
1606 *
1607 * @{
1608 */
1609
1610/** A (string) identifier. Used as a pair with #EcsName and #EcsSymbol tags. */
1611typedef struct EcsIdentifier {
1612 char *value; /**< Identifier string. */
1613 ecs_size_t length; /**< Length of identifier. */
1614 uint64_t hash; /**< Hash of current value. */
1615 uint64_t index_hash; /**< Hash of existing record in current index. */
1616 ecs_hashmap_t *index; /**< Current index. */
1618
1619/** Component information. */
1620typedef struct EcsComponent {
1621 ecs_size_t size; /**< Component size. */
1622 ecs_size_t alignment; /**< Component alignment. */
1624
1625/** Component for storing a poly object. */
1626typedef struct EcsPoly {
1627 ecs_poly_t *poly; /**< Pointer to poly object. */
1629
1630/** Non-fragmenting ChildOf relationship. */
1631typedef struct EcsParent {
1632 ecs_entity_t value; /**< Parent entity. */
1634
1635/** Component with data to instantiate a non-fragmenting tree. */
1636typedef struct {
1637 const char *child_name; /**< Name of the prefab child. */
1638 ecs_table_t *table; /**< Table in which the child will be stored. */
1639 uint32_t child; /**< Prefab child entity (without generation). */
1640 int32_t parent_index; /**< Index into the children vector. */
1642
1643/** Tree spawner data for a single hierarchy depth. */
1644typedef struct {
1645 ecs_vec_t children; /**< vector<ecs_tree_spawner_child_t>. */
1647
1648/** Tree instantiation cache component.
1649 * Tree instantiation cache, indexed by depth. Tables will have a
1650 * (ParentDepth, depth) pair indicating the hierarchy depth. This means that
1651 * for different depths, the tables the children are created in will also be
1652 * different. Caching tables for different depths therefore speeds up
1653 * instantiating trees even when the top-level entity is not at the root.
1654 */
1655typedef struct EcsTreeSpawner {
1656 ecs_tree_spawner_t data[FLECS_TREE_SPAWNER_DEPTH_CACHE_SIZE]; /**< Cache data indexed by depth. */
1658
1659/** @} */
1660/** @} */
1661
1662/* Only include deprecated definitions if deprecated addon is required */
1663#ifdef FLECS_DEPRECATED
1665#endif
1666
1667/**
1668 * @defgroup api_constants API Constants
1669 * Public API constants.
1670 *
1671 * @{
1672 */
1673
1674/**
1675 * @defgroup id_flags Component ID flags.
1676 * ID flags are bits that can be set on an ID (ecs_id_t).
1677 *
1678 * @{
1679 */
1680
1681/** Indicate that the ID is a pair. */
1682FLECS_API extern const ecs_id_t ECS_PAIR;
1683
1684/** Automatically override component when it is inherited. */
1685FLECS_API extern const ecs_id_t ECS_AUTO_OVERRIDE;
1686
1687/** Add a bitset to storage, which allows a component to be enabled or disabled. */
1688FLECS_API extern const ecs_id_t ECS_TOGGLE;
1689
1690/** Indicate that the target of a pair is an integer value. */
1691FLECS_API extern const ecs_id_t ECS_VALUE_PAIR;
1692
1693/** @} */
1694
1695/**
1696 * @defgroup builtin_tags Built-in component IDs.
1697 * @{
1698 */
1699
1700/* Built-in component IDs */
1701
1702/** Component component ID. */
1703FLECS_API extern const ecs_entity_t ecs_id(EcsComponent);
1704
1705/** Identifier component ID. */
1706FLECS_API extern const ecs_entity_t ecs_id(EcsIdentifier);
1707
1708/** Poly component ID. */
1709FLECS_API extern const ecs_entity_t ecs_id(EcsPoly);
1710
1711/** Parent component ID. */
1712FLECS_API extern const ecs_entity_t ecs_id(EcsParent);
1713
1714/** Component with data to instantiate a tree. */
1715FLECS_API extern const ecs_entity_t ecs_id(EcsTreeSpawner);
1716
1717/** Relationship storing the entity's depth in a non-fragmenting hierarchy. */
1718FLECS_API extern const ecs_entity_t EcsParentDepth;
1719
1720/** Tag added to queries. */
1721FLECS_API extern const ecs_entity_t EcsQuery;
1722
1723/** Tag added to observers. */
1724FLECS_API extern const ecs_entity_t EcsObserver;
1725
1726/** Tag added to systems. */
1727FLECS_API extern const ecs_entity_t EcsSystem;
1728
1729/** TickSource component ID. */
1730FLECS_API extern const ecs_entity_t ecs_id(EcsTickSource);
1731
1732/** Pipeline module component IDs. */
1733FLECS_API extern const ecs_entity_t ecs_id(EcsPipelineQuery);
1734
1735/** Timer component ID. */
1736FLECS_API extern const ecs_entity_t ecs_id(EcsTimer);
1737
1738/** RateFilter component ID. */
1739FLECS_API extern const ecs_entity_t ecs_id(EcsRateFilter);
1740
1741/** Root scope for built-in Flecs entities. */
1742FLECS_API extern const ecs_entity_t EcsFlecs;
1743
1744/** Core module scope. */
1745FLECS_API extern const ecs_entity_t EcsFlecsCore;
1746
1747/** Entity associated with world (used for "attaching" components to world). */
1748FLECS_API extern const ecs_entity_t EcsWorld;
1749
1750/** Wildcard entity ("*"). Matches any ID, returns all matches. */
1751FLECS_API extern const ecs_entity_t EcsWildcard;
1752
1753/** Any entity ("_"). Matches any ID, returns only the first. */
1754FLECS_API extern const ecs_entity_t EcsAny;
1755
1756/** This entity. Default source for queries. */
1757FLECS_API extern const ecs_entity_t EcsThis;
1758
1759/** Variable entity ("$"). Used in expressions to prefix variable names. */
1760FLECS_API extern const ecs_entity_t EcsVariable;
1761
1762/** Mark a relationship as transitive.
1763 * Behavior:
1764 *
1765 * @code
1766 * if R(X, Y) and R(Y, Z) then R(X, Z)
1767 * @endcode
1768 */
1769FLECS_API extern const ecs_entity_t EcsTransitive;
1770
1771/** Mark a relationship as reflexive.
1772 * Behavior:
1773 *
1774 * @code
1775 * R(X, X) == true
1776 * @endcode
1777 */
1778FLECS_API extern const ecs_entity_t EcsReflexive;
1779
1780/** Mark component as inheritable.
1781 * This is the opposite of Final. This trait can be used to enforce that queries
1782 * take into account component inheritance before inheritance (IsA)
1783 * relationships are added with the component as the target.
1784 */
1785FLECS_API extern const ecs_entity_t EcsInheritable;
1786
1787/** Relationship that specifies component inheritance behavior. */
1788FLECS_API extern const ecs_entity_t EcsOnInstantiate;
1789
1790#ifdef FLECS_PREFAB
1791/** Override component on instantiate.
1792 * This will copy the component from the base entity `(IsA target)` to the
1793 * instance. The base component will never be inherited from the prefab. */
1794FLECS_API extern const ecs_entity_t EcsOverride;
1795
1796/** Inherit component on instantiate.
1797 * This will inherit (share) the component from the base entity `(IsA target)`.
1798 * The component can be manually overridden by adding it to the instance. */
1799FLECS_API extern const ecs_entity_t EcsInherit;
1800#endif
1801
1802/** Never inherit component on instantiate.
1803 * This will not copy or share the component from the base entity `(IsA target)`.
1804 * When the component is added to an instance, its value will never be copied
1805 * from the base entity. */
1806FLECS_API extern const ecs_entity_t EcsDontInherit;
1807
1808/** Can be added to a relationship to indicate that the relationship can only occur
1809 * once on an entity. Adding a second instance will replace the first.
1810 *
1811 * Behavior:
1812 *
1813 * @code
1814 * R(X, Y) + R(X, Z) = R(X, Z)
1815 * @endcode
1816 */
1817FLECS_API extern const ecs_entity_t EcsExclusive;
1818
1819/** Mark a relationship as traversable. Traversable relationships may be
1820 * traversed with "up" queries. Traversable relationships are acyclic. */
1821FLECS_API extern const ecs_entity_t EcsTraversable;
1822
1823/** Ensure that a component is always added together with another component.
1824 *
1825 * Behavior:
1826 *
1827 * @code
1828 * If With(R, O) and R(X) then O(X)
1829 * If With(R, O) and R(X, Y) then O(X, Y)
1830 * @endcode
1831 */
1832FLECS_API extern const ecs_entity_t EcsWith;
1833
1834/** Mark a component as toggleable with ecs_enable_id(). */
1835FLECS_API extern const ecs_entity_t EcsCanToggle;
1836
1837/** Can be added to a relationship to indicate that it should never hold data,
1838 * even when it or the relationship target is a component. */
1839FLECS_API extern const ecs_entity_t EcsPairIsTag;
1840
1841/** Tag to indicate name identifier. */
1842FLECS_API extern const ecs_entity_t EcsName;
1843
1844/** Tag to indicate symbol identifier. */
1845FLECS_API extern const ecs_entity_t EcsSymbol;
1846
1847/** Tag to indicate alias identifier. */
1848FLECS_API extern const ecs_entity_t EcsAlias;
1849
1850/** Used to express parent-child relationships. */
1851FLECS_API extern const ecs_entity_t EcsChildOf;
1852
1853/** Used to express inheritance relationships. */
1854FLECS_API extern const ecs_entity_t EcsIsA;
1855
1856/** Used to express dependency relationships. */
1857FLECS_API extern const ecs_entity_t EcsDependsOn;
1858
1859/** Tag that, when added to a parent, ensures stable order of ecs_children() results. */
1860FLECS_API extern const ecs_entity_t EcsOrderedChildren;
1861
1862/** Tag added to module entities. */
1863FLECS_API extern const ecs_entity_t EcsModule;
1864
1865/** Tag added to prefab entities. Any entity with this tag is automatically
1866 * ignored by queries, unless #EcsPrefab is explicitly queried for. */
1867#ifdef FLECS_PREFAB
1868FLECS_API extern const ecs_entity_t EcsPrefab;
1869#endif
1870
1871/** When this tag is added to an entity, it is skipped by queries, unless
1872 * #EcsDisabled is explicitly queried for. */
1873FLECS_API extern const ecs_entity_t EcsDisabled;
1874
1875/** Trait added to entities that should never be returned by queries. Reserved
1876 * for internal entities that have special meaning to the query engine, such as
1877 * #EcsThis, #EcsWildcard, #EcsAny. */
1878FLECS_API extern const ecs_entity_t EcsNotQueryable;
1879
1880/** Event that triggers when an ID is added to an entity. */
1881FLECS_API extern const ecs_entity_t EcsOnAdd;
1882
1883/** Event that triggers when an ID is removed from an entity. */
1884FLECS_API extern const ecs_entity_t EcsOnRemove;
1885
1886/** Event that triggers when a component is set for an entity. */
1887FLECS_API extern const ecs_entity_t EcsOnSet;
1888
1889/** Event that triggers an observer when an entity starts or stops matching a query. */
1890FLECS_API extern const ecs_entity_t EcsMonitor;
1891
1892/** Event that triggers when a table is created. */
1893FLECS_API extern const ecs_entity_t EcsOnTableCreate;
1894
1895/** Event that triggers when a table is deleted. */
1896FLECS_API extern const ecs_entity_t EcsOnTableDelete;
1897
1898/** Relationship used for specifying cleanup behavior. */
1899FLECS_API extern const ecs_entity_t EcsOnDelete;
1900
1901/** Relationship used to define what should happen when a target entity (second
1902 * element of a pair) is deleted. */
1903FLECS_API extern const ecs_entity_t EcsOnDeleteTarget;
1904
1905/** Remove cleanup policy. Must be used as a target in a pair with #EcsOnDelete or
1906 * #EcsOnDeleteTarget. */
1907FLECS_API extern const ecs_entity_t EcsRemove;
1908
1909/** Delete cleanup policy. Must be used as a target in a pair with #EcsOnDelete or
1910 * #EcsOnDeleteTarget. */
1911FLECS_API extern const ecs_entity_t EcsDelete;
1912
1913/** Panic cleanup policy. Must be used as a target in a pair with #EcsOnDelete or
1914 * #EcsOnDeleteTarget. */
1915FLECS_API extern const ecs_entity_t EcsPanic;
1916
1917/** Mark component as sparse. */
1918FLECS_API extern const ecs_entity_t EcsSparse;
1919
1920/** Mark component as non-fragmenting. */
1921FLECS_API extern const ecs_entity_t EcsDontFragment;
1922
1923/** Marker used to indicate `$var == ...` matching in queries. */
1924FLECS_API extern const ecs_entity_t EcsPredEq;
1925
1926/** Marker used to indicate `$var == "name"` matching in queries. */
1927FLECS_API extern const ecs_entity_t EcsPredMatch;
1928
1929/** Marker used to indicate `$var ~= "pattern"` matching in queries. */
1930FLECS_API extern const ecs_entity_t EcsPredLookup;
1931
1932/** Marker used to indicate the start of a scope (`{`) in queries. */
1933FLECS_API extern const ecs_entity_t EcsScopeOpen;
1934
1935/** Marker used to indicate the end of a scope (`}`) in queries. */
1936FLECS_API extern const ecs_entity_t EcsScopeClose;
1937
1938/** Tag used to indicate a query is empty.
1939 * This tag is removed automatically when a query becomes non-empty, and is not
1940 * automatically re-added when it becomes empty.
1941 */
1942FLECS_API extern const ecs_entity_t EcsEmpty;
1943
1944FLECS_API extern const ecs_entity_t ecs_id(EcsPipeline); /**< Pipeline component ID. */
1945FLECS_API extern const ecs_entity_t EcsOnStart; /**< OnStart pipeline phase. */
1946FLECS_API extern const ecs_entity_t EcsPreFrame; /**< PreFrame pipeline phase. */
1947FLECS_API extern const ecs_entity_t EcsOnLoad; /**< OnLoad pipeline phase. */
1948FLECS_API extern const ecs_entity_t EcsPostLoad; /**< PostLoad pipeline phase. */
1949FLECS_API extern const ecs_entity_t EcsPreUpdate; /**< PreUpdate pipeline phase. */
1950FLECS_API extern const ecs_entity_t EcsOnUpdate; /**< OnUpdate pipeline phase. */
1951FLECS_API extern const ecs_entity_t EcsOnValidate; /**< OnValidate pipeline phase. */
1952FLECS_API extern const ecs_entity_t EcsPostUpdate; /**< PostUpdate pipeline phase. */
1953FLECS_API extern const ecs_entity_t EcsPreStore; /**< PreStore pipeline phase. */
1954FLECS_API extern const ecs_entity_t EcsOnStore; /**< OnStore pipeline phase. */
1955FLECS_API extern const ecs_entity_t EcsPostFrame; /**< PostFrame pipeline phase. */
1956FLECS_API extern const ecs_entity_t EcsPhase; /**< Phase pipeline phase. */
1957
1958FLECS_API extern const ecs_entity_t EcsConstant; /**< Tag added to enum or bitmask constants. */
1959
1960/** Value used to quickly check if a component is built-in. This is used to
1961 * filter out tables with built-in components (for example, for ecs_delete()). */
1962#define EcsLastInternalComponentId (ecs_id(EcsTreeSpawner))
1963
1964/** The first user-defined component starts from this ID. IDs up to this number
1965 * are reserved for built-in components. */
1966#define EcsFirstUserComponentId (8)
1967
1968/** The first user-defined entity starts from this ID. IDs up to this number
1969 * are reserved for built-in entities. */
1970#define EcsFirstUserEntityId (FLECS_HI_COMPONENT_ID + 128)
1971
1972/* When visualized, the reserved ID ranges look like this:
1973 * - [1..8]: Built-in components
1974 * - [9..FLECS_HI_COMPONENT_ID]: Low IDs reserved for application components
1975 * - [FLECS_HI_COMPONENT_ID + 1..EcsFirstUserEntityId]: Built-in entities
1976 */
1977
1978/** @} */
1979/** @} */
1980
1981/**
1982 * @defgroup world_api World
1983 * Functions for working with `ecs_world_t`.
1984 *
1985 * @{
1986 */
1987
1988/**
1989 * @defgroup world_creation_deletion Creation & Deletion
1990 * @{
1991 */
1992
1993/** Create a new world.
1994 * This operation automatically imports modules from addons Flecs has been built
1995 * with, except when the module specifies otherwise.
1996 *
1997 * @return A new world.
1998 */
1999FLECS_API
2001
2002/** Create a new world with just the core module.
2003 * Same as ecs_init(), but doesn't import modules from addons. This operation is
2004 * faster than ecs_init() and results in less memory utilization.
2005 *
2006 * @return A new tiny world.
2007 */
2008FLECS_API
2010
2011/** Create a new world with arguments.
2012 * Same as ecs_init(), but allows passing in command-line arguments. Command-line
2013 * arguments are used to:
2014 * - automatically derive the name of the application from argv[0]
2015 *
2016 * @param argc The number of arguments.
2017 * @param argv The argument array.
2018 * @return A new world.
2019 */
2020FLECS_API
2022 int argc,
2023 char *argv[]);
2024
2025/** Delete a world.
2026 * This operation deletes the world, and everything it contains.
2027 *
2028 * @param world The world to delete.
2029 * @return Zero if successful, non-zero if failed.
2030 */
2031FLECS_API
2033 ecs_world_t *world);
2034
2035/** Return whether the world is being deleted.
2036 * This operation can be used in callbacks like type hooks or observers to
2037 * detect if they are invoked while the world is being deleted.
2038 *
2039 * @param world The world.
2040 * @return True if being deleted, false if not.
2041 */
2042FLECS_API
2044 const ecs_world_t *world);
2045
2046/** Register an action to be executed when the world is destroyed.
2047 * Fini actions are typically used when a module needs to clean up before the
2048 * world shuts down.
2049 *
2050 * @param world The world.
2051 * @param action The function to execute.
2052 * @param ctx Userdata to pass to the function.
2053 */
2054FLECS_API
2056 ecs_world_t *world,
2057 ecs_fini_action_t action,
2058 void *ctx);
2059
2060/** Type returned by ecs_get_entities(). */
2061typedef struct ecs_entities_t {
2062 const ecs_entity_t *ids; /**< Array with all entity IDs in the world. */
2063 int32_t count; /**< Total number of entity IDs. */
2064 int32_t alive_count; /**< Number of alive entity IDs. */
2066
2067/** Return entity identifiers in the world.
2068 * This operation returns an array with all entity IDs that exist in the world.
2069 * Note that the returned array will change and may get invalidated as a result
2070 * of entity creation and deletion.
2071 *
2072 * To iterate all alive entity IDs, do:
2073 * @code
2074 * ecs_entities_t entities = ecs_get_entities(world);
2075 * for (int i = 0; i < entities.alive_count; i ++) {
2076 * ecs_entity_t id = entities.ids[i];
2077 * }
2078 * @endcode
2079 *
2080 * To iterate not-alive IDs, do:
2081 * @code
2082 * for (int i = entities.alive_count + 1; i < entities.count; i ++) {
2083 * ecs_entity_t id = entities.ids[i];
2084 * }
2085 * @endcode
2086 *
2087 * The returned array does not need to be freed. Mutating the returned array
2088 * will result in undefined behavior (and likely crashes).
2089 *
2090 * @param world The world.
2091 * @return Struct with entity ID array.
2092 */
2093FLECS_API
2095 const ecs_world_t *world);
2096
2097/** Get flags set on the world.
2098 * This operation returns the internal flags (see api_flags.h) that are
2099 * set on the world.
2100 *
2101 * @param world The world.
2102 * @return Flags set on the world.
2103 */
2104FLECS_API
2106 const ecs_world_t *world);
2107
2108/** @} */
2109
2110/**
2111 * @defgroup commands Commands
2112 * @{
2113 */
2114
2115/** Begin readonly mode.
2116 * This operation puts the world in readonly mode, which disallows mutations on
2117 * the world. Readonly mode exists so that internal mechanisms can implement
2118 * optimizations that assume certain aspects of the world do not change, while also
2119 * providing a mechanism for applications to prevent accidental mutations in,
2120 * for example, multithreaded applications.
2121 *
2122 * Readonly mode is a stronger version of deferred mode. In deferred mode,
2123 * ECS operations such as add, remove, set, delete, etc. are added to a command
2124 * queue to be executed later. In readonly mode, operations that could break
2125 * scheduler logic (such as creating systems, queries) are also disallowed.
2126 *
2127 * Readonly mode itself has a single-threaded and a multithreaded mode. In
2128 * single-threaded mode, certain mutations on the world are still allowed, for
2129 * example:
2130 * - Entity liveliness operations (such as ecs_new(), ecs_make_alive()), so that systems are
2131 * able to create new entities.
2132 * - Implicit component registration, so that it works from systems.
2133 * - Mutations to supporting data structures for the evaluation of uncached
2134 * queries, so that these can be created on the fly.
2135 *
2136 * These mutations are safe in single-threaded applications, but for
2137 * multithreaded applications the world needs to be entirely immutable. For this
2138 * purpose, multithreaded readonly mode exists, which disallows all mutations on
2139 * the world. This means that in multithreaded applications, entity liveliness
2140 * operations, implicit component registration, and on-the-fly query creation
2141 * are not guaranteed to work.
2142 *
2143 * While in readonly mode, applications can still enqueue ECS operations on a
2144 * stage. Stages are managed automatically when using the pipeline addon and
2145 * ecs_progress(), but they can also be configured manually as shown here:
2146 *
2147 * @code
2148 * // Number of stages typically corresponds with number of threads
2149 * ecs_set_stage_count(world, 2);
2150 * ecs_world_t *stage = ecs_get_stage(world, 1);
2151 *
2152 * ecs_readonly_begin(world, false);
2153 * ecs_add(world, e, Tag); // readonly assert
2154 * ecs_add(stage, e, Tag); // OK
2155 * @endcode
2156 *
2157 * When an attempt is made to perform an operation on a world in readonly mode,
2158 * the code will throw an assert saying that the world is in readonly mode.
2159 *
2160 * A call to ecs_readonly_begin() must be followed up with ecs_readonly_end().
2161 * When ecs_readonly_end() is called, all enqueued commands from configured
2162 * stages are merged back into the world. Calls to ecs_readonly_begin() and
2163 * ecs_readonly_end() should always happen from a context where the code has
2164 * exclusive access to the world. The functions themselves are not thread-safe.
2165 *
2166 * In a typical application, a (non-exhaustive) call stack that uses
2167 * ecs_readonly_begin() and ecs_readonly_end() will look like this:
2168 *
2169 * @code
2170 * ecs_progress()
2171 * ecs_readonly_begin()
2172 * ecs_defer_begin()
2173 *
2174 * // user code
2175 *
2176 * ecs_readonly_end()
2177 * ecs_defer_end()
2178 * @endcode
2179 *
2180 * @param world The world.
2181 * @param multi_threaded Whether to enable multithreaded readonly mode.
2182 * @return Whether world is in readonly mode.
2183 */
2184FLECS_API
2186 ecs_world_t *world,
2187 bool multi_threaded);
2188
2189/** End readonly mode.
2190 * This operation ends readonly mode, and must be called after
2191 * ecs_readonly_begin(). Operations that were deferred while the world was in
2192 * readonly mode will be flushed.
2193 *
2194 * @param world The world.
2195 */
2196FLECS_API
2198 ecs_world_t *world);
2199
2200/** Merge a stage.
2201 * This will merge all commands enqueued for a stage.
2202 *
2203 * @param stage The stage.
2204 */
2205FLECS_API
2207 ecs_world_t *stage);
2208
2209/** Defer operations until the end of the frame.
2210 * When this operation is invoked while iterating, operations between the
2211 * ecs_defer_begin() and ecs_defer_end() operations are executed at the end
2212 * of the frame.
2213 *
2214 * This operation is thread-safe.
2215 *
2216 * @param world The world.
2217 * @return true if world changed from non-deferred mode to deferred mode.
2218 *
2219 * @see ecs_defer_end()
2220 * @see ecs_is_deferred()
2221 * @see ecs_defer_resume()
2222 * @see ecs_defer_suspend()
2223 * @see ecs_is_defer_suspended()
2224 */
2225FLECS_API
2227 ecs_world_t *world);
2228
2229/** End a block of operations to defer.
2230 * See ecs_defer_begin().
2231 *
2232 * This operation is thread-safe.
2233 *
2234 * @param world The world.
2235 * @return true if world changed from deferred mode to non-deferred mode.
2236 *
2237 * @see ecs_defer_begin()
2238 * @see ecs_is_deferred()
2239 * @see ecs_defer_resume()
2240 * @see ecs_defer_suspend()
2241 */
2242FLECS_API
2244 ecs_world_t *world);
2245
2246/** Suspend deferring but do not flush queue.
2247 * This operation can be used to do an undeferred operation while not flushing
2248 * the operations in the queue.
2249 *
2250 * An application should invoke ecs_defer_resume() before ecs_defer_end() is called.
2251 * The operation may only be called when deferring is enabled.
2252 *
2253 * @param world The world.
2254 *
2255 * @see ecs_defer_begin()
2256 * @see ecs_defer_end()
2257 * @see ecs_is_deferred()
2258 * @see ecs_defer_resume()
2259 */
2260FLECS_API
2262 ecs_world_t *world);
2263
2264/** Resume deferring.
2265 * See ecs_defer_suspend().
2266 *
2267 * @param world The world.
2268 *
2269 * @see ecs_defer_begin()
2270 * @see ecs_defer_end()
2271 * @see ecs_is_deferred()
2272 * @see ecs_defer_suspend()
2273 */
2274FLECS_API
2276 ecs_world_t *world);
2277
2278/** Test if deferring is enabled for the current stage.
2279 *
2280 * @param world The world.
2281 * @return True if deferred, false if not.
2282 *
2283 * @see ecs_defer_begin()
2284 * @see ecs_defer_end()
2285 * @see ecs_defer_resume()
2286 * @see ecs_defer_suspend()
2287 * @see ecs_is_defer_suspended()
2288 */
2289FLECS_API
2291 const ecs_world_t *world);
2292
2293/** Test if deferring is suspended for the current stage.
2294 *
2295 * @param world The world.
2296 * @return True if suspended, false if not.
2297 *
2298 * @see ecs_defer_begin()
2299 * @see ecs_defer_end()
2300 * @see ecs_is_deferred()
2301 * @see ecs_defer_resume()
2302 * @see ecs_defer_suspend()
2303 */
2304FLECS_API
2306 const ecs_world_t *world);
2307
2308/** Configure the world to have N stages.
2309 * This initializes N stages, which allows applications to defer operations to
2310 * multiple isolated defer queues. This is typically used for applications with
2311 * multiple threads, where each thread gets its own queue, and commands are
2312 * merged when threads are synchronized.
2313 *
2314 * Note that the ecs_set_threads() function already creates the appropriate
2315 * number of stages. The ecs_set_stage_count() operation is useful for applications
2316 * that want to manage their own stages and/or threads.
2317 *
2318 * @param world The world.
2319 * @param stages The number of stages.
2320 */
2321FLECS_API
2323 ecs_world_t *world,
2324 int32_t stages);
2325
2326/** Get the number of configured stages.
2327 * Return the number of stages set by ecs_set_stage_count().
2328 *
2329 * @param world The world.
2330 * @return The number of stages used for threading.
2331 */
2332FLECS_API
2334 const ecs_world_t *world);
2335
2336/** Get stage-specific world pointer.
2337 * Flecs threads can safely invoke the API as long as they have a private
2338 * context to write to, also referred to as the stage. This function returns a
2339 * pointer to a stage, disguised as a world pointer.
2340 *
2341 * Note that this function does not create a new world. It simply wraps the
2342 * existing world in a thread-specific context, which the API knows how to
2343 * unwrap. The reason the stage is returned as an ecs_world_t is so that it
2344 * can be passed transparently to the existing API functions, instead of having to
2345 * create a dedicated API for threading.
2346 *
2347 * @param world The world.
2348 * @param stage_id The index of the stage to retrieve.
2349 * @return A thread-specific pointer to the world.
2350 */
2351FLECS_API
2353 const ecs_world_t *world,
2354 int32_t stage_id);
2355
2356/** Test whether the current world is readonly.
2357 * This function allows the code to test whether the currently used world
2358 * is readonly or whether it allows for writing.
2359 *
2360 * @param world A pointer to a stage or the world.
2361 * @return True if the world or stage is readonly.
2362 */
2363FLECS_API
2365 const ecs_world_t *world);
2366
2367/** Create an unmanaged stage.
2368 * Create a stage whose lifecycle is not managed by the world. Must be freed
2369 * with ecs_stage_free().
2370 *
2371 * @param world The world.
2372 * @return The stage.
2373 */
2374FLECS_API
2376 ecs_world_t *world);
2377
2378/** Free an unmanaged stage.
2379 *
2380 * @param stage The stage to free.
2381 */
2382FLECS_API
2384 ecs_world_t *stage);
2385
2386/** Get the stage ID.
2387 * The stage ID can be used by an application to learn about which stage it is
2388 * using, which typically corresponds with the worker thread ID.
2389 *
2390 * @param world The world.
2391 * @return The stage ID.
2392 */
2393FLECS_API
2395 const ecs_world_t *world);
2396
2397/** @} */
2398
2399/**
2400 * @defgroup world_misc Misc
2401 * @{
2402 */
2403
2404/** Set a world context.
2405 * This operation allows an application to register custom data with a world
2406 * that can be accessed anywhere where the application has the world.
2407 *
2408 * @param world The world.
2409 * @param ctx A pointer to a user-defined structure.
2410 * @param ctx_free A function that is invoked with ctx when the world is freed.
2411 */
2412FLECS_API
2414 ecs_world_t *world,
2415 void *ctx,
2416 ecs_ctx_free_t ctx_free);
2417
2418/** Set a world binding context.
2419 * Same as ecs_set_ctx(), but for binding context. A binding context is intended
2420 * specifically for language bindings to store binding-specific data.
2421 *
2422 * @param world The world.
2423 * @param ctx A pointer to a user-defined structure.
2424 * @param ctx_free A function that is invoked with ctx when the world is freed.
2425 */
2426FLECS_API
2428 ecs_world_t *world,
2429 void *ctx,
2430 ecs_ctx_free_t ctx_free);
2431
2432/** Get the world context.
2433 * This operation retrieves a previously set world context.
2434 *
2435 * @param world The world.
2436 * @return The context set with ecs_set_ctx(). If no context was set, the
2437 * function returns NULL.
2438 */
2439FLECS_API
2441 const ecs_world_t *world);
2442
2443/** Get the world binding context.
2444 * This operation retrieves a previously set world binding context.
2445 *
2446 * @param world The world.
2447 * @return The context set with ecs_set_binding_ctx(). If no context was set, the
2448 * function returns NULL.
2449 */
2450FLECS_API
2452 const ecs_world_t *world);
2453
2454/** Get build info.
2455 * Return information about the current Flecs build.
2456 *
2457 * @return A struct with information about the current Flecs build.
2458 */
2459FLECS_API
2461
2462/** Get the world info.
2463 *
2464 * @param world The world.
2465 * @return A pointer to the world info. Valid for as long as the world exists.
2466 */
2467FLECS_API
2469 const ecs_world_t *world);
2470
2471/** Dimension the world for a specified number of entities.
2472 * This operation will preallocate memory in the world for the specified number
2473 * of entities. Specifying a number lower than the current number of entities in
2474 * the world will have no effect.
2475 *
2476 * @param world The world.
2477 * @param entity_count The number of entities to preallocate.
2478 */
2479FLECS_API
2481 ecs_world_t *world,
2482 int32_t entity_count);
2483
2484/** Free unused memory.
2485 * This operation frees allocated memory that is no longer in use by the world.
2486 * Examples of allocations that get cleaned up are:
2487 * - Unused pages in the entity index
2488 * - Component columns
2489 * - Empty tables
2490 *
2491 * Flecs uses allocators internally for speeding up allocations. Allocators are
2492 * not evaluated by this function, which means that the memory reported by the
2493 * OS may not go down. For this reason, this function is most effective when
2494 * combined with FLECS_USE_OS_ALLOC, which disables internal allocators.
2495 *
2496 * @param world The world.
2497 */
2498FLECS_API
2500 ecs_world_t *world);
2501
2502/** Get the largest issued entity ID (not counting generation).
2503 *
2504 * @param world The world.
2505 * @return The largest issued entity ID.
2506 */
2507FLECS_API
2509 const ecs_world_t *world);
2510
2511/** Force aperiodic actions.
2512 * The world may delay certain operations until they are necessary for the
2513 * application to function correctly. This may cause observable side effects
2514 * such as delayed triggering of events, which can be inconvenient when, for
2515 * example, running a test suite.
2516 *
2517 * The flags parameter specifies which aperiodic actions to run. Specify 0 to
2518 * run all actions. Supported flags start with 'EcsAperiodic'. Flags identify
2519 * internal mechanisms and may change unannounced.
2520 *
2521 * @param world The world.
2522 * @param flags The flags specifying which actions to run.
2523 */
2524FLECS_API
2526 ecs_world_t *world,
2527 ecs_flags32_t flags);
2528
2529/** Used with ecs_delete_empty_tables(). */
2531 /** Free table data when generation > clear_generation. */
2533
2534 /** Delete table when generation > delete_generation. */
2536
2537 /** Amount of time operation is allowed to spend. */
2539
2540 /** Table index to start scanning at. The function loops around until it
2541 * reaches this offset again, or until the time budget is exceeded. */
2542 int32_t offset;
2544
2545/** Clean up empty tables.
2546 * This operation cleans up empty tables that meet certain conditions. Having
2547 * large amounts of empty tables does not negatively impact performance of the
2548 * ECS, but can take up considerable amounts of memory, especially in
2549 * applications with many components, and many components per entity.
2550 *
2551 * The generation specifies the minimum number of times this operation has
2552 * to be called before an empty table is cleaned up. If a table becomes
2553 * non-empty, the generation is reset.
2554 *
2555 * The operation allows for both a "clear" generation and a "delete"
2556 * generation. When the clear generation is reached, the table's
2557 * resources are freed (like component arrays) but the table itself is not
2558 * deleted. When the delete generation is reached, the empty table is deleted.
2559 *
2560 * By specifying a non-zero ID, the cleanup logic can be limited to tables with
2561 * a specific (component) ID. The operation will only increase the generation
2562 * count of matching tables.
2563 *
2564 * The min_id_count specifies a lower bound for the number of components a table
2565 * should have. Often the more components a table has, the more specific it is
2566 * and therefore less likely to be reused.
2567 *
2568 * The time budget specifies how long the operation should take at most.
2569 *
2570 * The offset parameter specifies the table index at which to start scanning.
2571 * The function loops around until it reaches this offset again, or until the
2572 * time budget is exceeded.
2573 *
2574 * @param world The world.
2575 * @param desc Configuration parameters.
2576 * @return The index + 1 of the table where the function stopped, or 0 if the
2577 * function scanned all tables. The return value can be used as the
2578 * offset for the next call.
2579 */
2580FLECS_API
2582 ecs_world_t *world,
2583 const ecs_delete_empty_tables_desc_t *desc);
2584
2585/** Get the world from a poly.
2586 *
2587 * @param poly A pointer to a poly object.
2588 * @return The world.
2589 */
2590FLECS_API
2592 const ecs_poly_t *poly);
2593
2594/** Get the entity from a poly.
2595 *
2596 * @param poly A pointer to a poly object.
2597 * @return The entity associated with the poly object.
2598 */
2599FLECS_API
2601 const ecs_poly_t *poly);
2602
2603/** Test if a pointer is of the specified type.
2604 * Usage:
2605 *
2606 * @code
2607 * flecs_poly_is(ptr, ecs_world_t)
2608 * @endcode
2609 *
2610 * This operation only works for poly types.
2611 *
2612 * @param object The object to test.
2613 * @param type The ID of the type.
2614 * @return True if the pointer is of the specified type.
2615 */
2616FLECS_API
2618 const ecs_poly_t *object,
2619 int32_t type);
2620
2621/** Test if a pointer is of the specified type.
2622 * @see flecs_poly_is_()
2623 */
2624#define flecs_poly_is(object, type)\
2625 flecs_poly_is_(object, type##_magic)
2626
2627/** Make a pair ID.
2628 * This function is equivalent to using the ecs_pair() macro, and is added for
2629 * convenience to make it easier for non-C/C++ bindings to work with pairs.
2630 *
2631 * @param first The first element of the pair.
2632 * @param second The target of the pair.
2633 * @return A pair ID.
2634 */
2635FLECS_API
2637 ecs_entity_t first,
2638 ecs_entity_t second);
2639
2640/** Begin exclusive thread access.
2641 * This operation ensures that only the thread from which this operation is
2642 * called can access the world. Attempts to access the world from other threads
2643 * will panic.
2644 *
2645 * ecs_exclusive_access_begin() must be called in pairs with
2646 * ecs_exclusive_access_end(). Calling ecs_exclusive_access_begin() from another
2647 * thread without first calling ecs_exclusive_access_end() will panic.
2648 *
2649 * A thread name can be provided to the function to improve debug messages. The
2650 * function does not copy the thread name, which means the memory for the
2651 * name must remain alive for as long as the thread has exclusive access.
2652 *
2653 * This operation should only be called once per thread. Calling it multiple
2654 * times for the same thread will cause a panic.
2655 *
2656 * Note that this feature only works in builds where asserts are enabled. The
2657 * feature requires the OS API thread_self_ callback to be set.
2658 *
2659 * @param world The world.
2660 * @param thread_name The name of the thread obtaining exclusive access.
2661 */
2662FLECS_API
2664 ecs_world_t *world,
2665 const char *thread_name);
2666
2667/** End exclusive thread access.
2668 * This operation should be called after ecs_exclusive_access_begin(). After
2669 * calling this operation, other threads are no longer prevented from mutating
2670 * the world.
2671 *
2672 * When "lock_world" is set to true, no thread will be able to mutate the world
2673 * until ecs_exclusive_access_begin() is called again. While the world is locked,
2674 * only read-only operations are allowed. For example, ecs_get_id() is allowed,
2675 * but ecs_get_mut_id() is not allowed.
2676 *
2677 * A locked world can be unlocked by calling ecs_exclusive_access_end() again with
2678 * lock_world set to false. Note that this only works for locked worlds. If
2679 * ecs_exclusive_access_end() is called on a world that has exclusive thread
2680 * access from a different thread, a panic will happen.
2681 *
2682 * This operation must be called from the same thread that called
2683 * ecs_exclusive_access_begin(). Calling it from a different thread will cause
2684 * a panic.
2685 *
2686 * @param world The world.
2687 * @param lock_world When true, any mutations on the world will be blocked.
2688 */
2689FLECS_API
2691 ecs_world_t *world,
2692 bool lock_world);
2693
2694/** @} */
2695
2696/** @} */
2697
2698/**
2699 * @defgroup entities Entities
2700 * Functions for working with `ecs_entity_t`.
2701 *
2702 * @{
2703 */
2704
2705/**
2706 * @defgroup creating_entities Creating & Deleting
2707 * Functions for creating and deleting entities.
2708 *
2709 * @{
2710 */
2711
2712/** Create new entity ID.
2713 * This operation returns an unused entity ID. This operation is guaranteed to
2714 * return an empty entity as it does not use values set by ecs_set_scope().
2715 *
2716 * @param world The world.
2717 * @return The new entity ID.
2718 */
2719FLECS_API
2721 ecs_world_t *world);
2722
2723/** Create new low ID.
2724 * This operation returns a new low ID. Entity IDs start after the
2725 * FLECS_HI_COMPONENT_ID constant. This reserves a range of low IDs for things
2726 * like components, and allows parts of the code to optimize operations.
2727 *
2728 * Note that FLECS_HI_COMPONENT_ID does not represent the maximum number of
2729 * components that can be created, only the maximum number of components that
2730 * can take advantage of these optimizations.
2731 *
2732 * This operation is guaranteed to return an empty entity as it does not use
2733 * values set by ecs_set_scope().
2734 *
2735 * This operation does not recycle IDs.
2736 *
2737 * @param world The world.
2738 * @return The new component ID.
2739 */
2740FLECS_API
2742 ecs_world_t *world);
2743
2744/** Create new entity with (component) ID.
2745 * This operation creates a new entity with an optional (component) ID.
2746 *
2747 * @param world The world.
2748 * @param component The component to create the new entity with.
2749 * @return The new entity.
2750 */
2751FLECS_API
2753 ecs_world_t *world,
2754 ecs_id_t component);
2755
2756/** Create new entity in table.
2757 * This operation creates a new entity in the specified table.
2758 *
2759 * @param world The world.
2760 * @param table The table to which to add the new entity.
2761 * @return The new entity.
2762 */
2763FLECS_API
2765 ecs_world_t *world,
2766 ecs_table_t *table);
2767
2768/** Find or create an entity.
2769 * This operation creates a new entity, or modifies an existing one. When a name
2770 * is set in the ecs_entity_desc_t::name field and ecs_entity_desc_t::entity is
2771 * not set, the operation will first attempt to find an existing entity by that
2772 * name. If no entity with that name can be found, it will be created.
2773 *
2774 * If both a name and entity handle are provided, the operation will check if
2775 * the entity name matches with the provided name. If the names do not match,
2776 * the function will fail and return 0.
2777 *
2778 * If an ID to a non-existing entity is provided, that entity ID becomes alive.
2779 *
2780 * See the documentation of ecs_entity_desc_t for more details.
2781 *
2782 * @param world The world.
2783 * @param desc Entity init parameters.
2784 * @return A handle to the new or existing entity, or 0 if failed.
2785 */
2786FLECS_API
2788 ecs_world_t *world,
2789 const ecs_entity_desc_t *desc);
2790
2791/** Create a new entity with a list of component values.
2792 * Values for zero-sized (tag) components are added without setting a value.
2793 *
2794 * This operation is equivalent to creating an entity with ecs_new() followed
2795 * by ecs_set_id() for each provided value.
2796 *
2797 * @param world The world.
2798 * @param values Null-terminated array of component values to set.
2799 * @return A handle to the new entity, or 0 if failed.
2800 */
2801FLECS_API
2803 ecs_world_t *world,
2804 const ecs_value_t *values);
2805
2806/** Bulk create or populate new entities.
2807 * This operation bulk inserts a list of new or predefined entities into a
2808 * single table.
2809 *
2810 * The operation does not take ownership of component arrays provided by the
2811 * application. Components that are non-trivially copyable will be moved into
2812 * the storage.
2813 *
2814 * The operation will emit OnAdd events for each added ID, and OnSet events for
2815 * each component that has been set.
2816 *
2817 * If no entity IDs are provided by the application, the returned array of IDs
2818 * points to an internal data structure, which changes when new entities are
2819 * created or deleted.
2820 *
2821 * If as a result of the operation, observers are invoked that delete
2822 * entities and no entity IDs were provided by the application, the returned
2823 * array of identifiers may be incorrect. To avoid this problem, an application
2824 * can first call ecs_bulk_init() to create empty entities, copy the array to one
2825 * that is owned by the application, and then use this array to populate the
2826 * entities.
2827 *
2828 * @param world The world.
2829 * @param desc Bulk creation parameters.
2830 * @return An array with the list of entity IDs created or populated.
2831 */
2832FLECS_API
2834 ecs_world_t *world,
2835 const ecs_bulk_desc_t *desc);
2836
2837/** Create N new entities.
2838 * This operation is the same as ecs_new_w_id(), but creates N entities
2839 * instead of one.
2840 *
2841 * @param world The world.
2842 * @param component The component to create the entities with.
2843 * @param count The number of entities to create.
2844 * @return An array with the entity IDs of the newly created entities.
2845 */
2846FLECS_API
2848 ecs_world_t *world,
2849 ecs_id_t component,
2850 int32_t count);
2851
2852/** Clone an entity.
2853 * This operation clones the components of one entity into another entity. If
2854 * no destination entity is provided, a new entity will be created. Component
2855 * values are not copied unless copy_value is true.
2856 *
2857 * If the source entity has a name, it will not be copied to the destination
2858 * entity. This is to prevent having two entities with the same name under the
2859 * same parent, which is not allowed.
2860 *
2861 * @param world The world.
2862 * @param dst The entity to copy the components to.
2863 * @param src The entity to copy the components from.
2864 * @param copy_value If true, the value of components will be copied to dst.
2865 * @return The destination entity.
2866 */
2867FLECS_API
2869 ecs_world_t *world,
2870 ecs_entity_t dst,
2871 ecs_entity_t src,
2872 bool copy_value);
2873
2874/** Delete an entity.
2875 * This operation will delete an entity and all of its components. The entity ID
2876 * will be made available for recycling. If the entity passed to ecs_delete() is
2877 * not alive, the operation will have no side effects.
2878 *
2879 * @param world The world.
2880 * @param entity The entity.
2881 */
2882FLECS_API
2884 ecs_world_t *world,
2885 ecs_entity_t entity);
2886
2887/** Delete all entities with the specified component.
2888 * This will delete all entities (tables) that have the specified ID. The
2889 * component may be a wildcard and/or a pair.
2890 *
2891 * @param world The world.
2892 * @param component The component.
2893 */
2894FLECS_API
2896 ecs_world_t *world,
2897 ecs_id_t component);
2898
2899/** Set child order for parent with OrderedChildren.
2900 * If the parent has the OrderedChildren trait, the order of the children
2901 * will be updated to the order in the specified children array. The operation
2902 * will fail if the parent does not have the OrderedChildren trait.
2903 *
2904 * This operation always takes place immediately, and is not deferred. When the
2905 * operation is called from a multithreaded system, it will fail.
2906 *
2907 * The reason for not deferring this operation is that by the time the deferred
2908 * command would be executed, the children of the parent could have been changed
2909 * which would cause the operation to fail.
2910 *
2911 * @param world The world.
2912 * @param parent The parent.
2913 * @param children An array with children.
2914 * @param child_count The number of children in the provided array.
2915 */
2916FLECS_API
2918 ecs_world_t *world,
2919 ecs_entity_t parent,
2920 const ecs_entity_t *children,
2921 int32_t child_count);
2922
2923/** Get ordered children.
2924 * If a parent has the OrderedChildren trait, this operation can be used to
2925 * obtain the array with child entities. If this operation is used on a parent
2926 * that does not have the OrderedChildren trait, it will fail.
2927 *
2928 * @param world The world.
2929 * @param parent The parent.
2930 * @return The array with child entities.
2931 */
2932FLECS_API
2934 const ecs_world_t *world,
2935 ecs_entity_t parent);
2936
2937/** @} */
2938
2939/**
2940 * @defgroup adding_removing Adding & Removing
2941 * Functions for adding and removing components.
2942 *
2943 * @{
2944 */
2945
2946/** Add a (component) ID to an entity.
2947 * This operation adds a single (component) ID to an entity. If the entity
2948 * already has the ID, this operation will have no side effects.
2949 *
2950 * @param world The world.
2951 * @param entity The entity.
2952 * @param component The component ID to add.
2953 */
2954FLECS_API
2956 ecs_world_t *world,
2957 ecs_entity_t entity,
2958 ecs_id_t component);
2959
2960/** Remove a component from an entity.
2961 * This operation removes a single component from an entity. If the entity
2962 * does not have the component, this operation will have no side effects.
2963 *
2964 * @param world The world.
2965 * @param entity The entity.
2966 * @param component The component to remove.
2967 */
2968FLECS_API
2970 ecs_world_t *world,
2971 ecs_entity_t entity,
2972 ecs_id_t component);
2973
2974/** Add an auto override for a component.
2975 * An auto override is a component that is automatically added to an entity when
2976 * it is instantiated from a prefab. Auto overrides are added to the entity that
2977 * is inherited from (usually a prefab). For example:
2978 *
2979 * @code
2980 * ecs_entity_t prefab = ecs_insert(world,
2981 * ecs_value(Position, {10, 20}),
2982 * ecs_value(Mass, {100}));
2983 *
2984 * ecs_auto_override(world, prefab, Position);
2985 *
2986 * ecs_entity_t inst = ecs_new_w_pair(world, EcsIsA, prefab);
2987 * assert(ecs_owns(world, inst, Position)); // true
2988 * assert(ecs_owns(world, inst, Mass)); // false
2989 * @endcode
2990 *
2991 * An auto override is equivalent to a manual override:
2992 *
2993 * @code
2994 * ecs_entity_t prefab = ecs_insert(world,
2995 * ecs_value(Position, {10, 20}),
2996 * ecs_value(Mass, {100}));
2997 *
2998 * ecs_entity_t inst = ecs_new_w_pair(world, EcsIsA, prefab);
2999 * assert(ecs_owns(world, inst, Position)); // false
3000 * ecs_add(world, inst, Position); // manual override
3001 * assert(ecs_owns(world, inst, Position)); // true
3002 * assert(ecs_owns(world, inst, Mass)); // false
3003 * @endcode
3004 *
3005 * This operation is equivalent to manually adding the ID with the AUTO_OVERRIDE
3006 * bit applied:
3007 *
3008 * @code
3009 * ecs_add_id(world, entity, ECS_AUTO_OVERRIDE | id);
3010 * @endcode
3011 *
3012 * When a component is overridden and inherited from a prefab, the value from
3013 * the prefab component is copied to the instance. When the component is not
3014 * inherited from a prefab, it is added to the instance as if using ecs_add_id().
3015 *
3016 * Overriding is the default behavior on prefab instantiation. Auto overriding
3017 * is only useful for components with the `(OnInstantiate, Inherit)` trait.
3018 * When a component has the `(OnInstantiate, DontInherit)` trait and is overridden,
3019 * the component is added, but the value from the prefab will not be copied.
3020 *
3021 * @param world The world.
3022 * @param entity The entity.
3023 * @param component The component to auto override.
3024 */
3025FLECS_API
3027 ecs_world_t *world,
3028 ecs_entity_t entity,
3029 ecs_id_t component);
3030
3031/** Clear all components.
3032 * This operation will remove all components from an entity.
3033 *
3034 * @param world The world.
3035 * @param entity The entity.
3036 */
3037FLECS_API
3039 ecs_world_t *world,
3040 ecs_entity_t entity);
3041
3042/** Remove all instances of the specified component.
3043 * This will remove the specified ID from all entities (tables). The ID may be
3044 * a wildcard and/or a pair.
3045 *
3046 * @param world The world.
3047 * @param component The component.
3048 */
3049FLECS_API
3051 ecs_world_t *world,
3052 ecs_id_t component);
3053
3054/** @} */
3055
3056/**
3057 * @defgroup enabling_disabling Enabling & Disabling
3058 * Functions for enabling/disabling entities and components.
3059 *
3060 * @{
3061 */
3062
3063/** Enable or disable an entity.
3064 * This operation enables or disables an entity by adding or removing the
3065 * #EcsDisabled tag. A disabled entity will not be matched with any systems,
3066 * unless the system explicitly specifies the #EcsDisabled tag.
3067 *
3068 * @param world The world.
3069 * @param entity The entity to enable or disable.
3070 * @param enabled true to enable the entity, false to disable.
3071 */
3072FLECS_API
3074 ecs_world_t *world,
3075 ecs_entity_t entity,
3076 bool enabled);
3077
3078/** Enable or disable a component.
3079 * Enabling or disabling a component does not add or remove a component from an
3080 * entity, but prevents it from being matched with queries. This operation can
3081 * be useful when a component must be temporarily disabled without destroying
3082 * its value. It is also a more performant operation for when an application
3083 * needs to add/remove components at high frequency, as enabling/disabling is
3084 * cheaper than a regular add or remove.
3085 *
3086 * @param world The world.
3087 * @param entity The entity.
3088 * @param component The component to enable/disable.
3089 * @param enable True to enable the component, false to disable.
3090 */
3091FLECS_API
3093 ecs_world_t *world,
3094 ecs_entity_t entity,
3095 ecs_id_t component,
3096 bool enable);
3097
3098/** Test if a component is enabled.
3099 * Test whether a component is currently enabled or disabled. This operation
3100 * will return true when the entity has the component and if it has not been
3101 * disabled by ecs_enable_id().
3102 *
3103 * @param world The world.
3104 * @param entity The entity.
3105 * @param component The component.
3106 * @return True if the component is enabled, otherwise false.
3107 */
3108FLECS_API
3110 const ecs_world_t *world,
3111 ecs_entity_t entity,
3112 ecs_id_t component);
3113
3114/** @} */
3115
3116/**
3117 * @defgroup getting Getting and Setting
3118 * Functions for getting and setting components.
3119 *
3120 * @{
3121 */
3122
3123/** Get an immutable pointer to a component.
3124 * This operation obtains a const pointer to the requested component. The
3125 * operation accepts the component entity ID.
3126 *
3127 * This operation can return inherited components reachable through an `IsA`
3128 * relationship.
3129 *
3130 * @param world The world.
3131 * @param entity The entity.
3132 * @param component The component to get.
3133 * @return The component pointer, NULL if the entity does not have the component.
3134 *
3135 * @see ecs_get_mut_id()
3136 */
3137FLECS_API
3138FLECS_ALWAYS_INLINE const void* ecs_get_id(
3139 const ecs_world_t *world,
3140 ecs_entity_t entity,
3141 ecs_id_t component);
3142
3143/** Get a mutable pointer to a component.
3144 * This operation obtains a mutable pointer to the requested component. The
3145 * operation accepts the component entity ID.
3146 *
3147 * Unlike ecs_get_id(), this operation does not return inherited components.
3148 * This is to prevent errors where an application accidentally resolves an
3149 * inherited component shared with many entities and modifies it, while thinking
3150 * it is modifying an owned component.
3151 *
3152 * @param world The world.
3153 * @param entity The entity.
3154 * @param component The component to get.
3155 * @return The component pointer, NULL if the entity does not have the component.
3156 */
3157FLECS_API
3158FLECS_ALWAYS_INLINE void* ecs_get_mut_id(
3159 const ecs_world_t *world,
3160 ecs_entity_t entity,
3161 ecs_id_t component);
3162
3163/** Get a pointer to a sparse component.
3164 * This operation obtains a pointer to a sparse component, and is a faster
3165 * alternative to using ecs_get_id() and ecs_get_mut_id(). This operation should
3166 * only be used for sparse, non-inheritable components.
3167 *
3168 * @param world The world.
3169 * @param entity The entity.
3170 * @param component The component to get.
3171 * @param size The size of the component type. Must match the size of the component.
3172 * @return The component pointer, NULL if the entity does not have the component.
3173 *
3174 * @see ecs_get_id()
3175 * @see ecs_get_mut_id()
3176 */
3177FLECS_API
3178FLECS_ALWAYS_INLINE void* ecs_get_sparse_id(
3179 const ecs_world_t *world,
3180 ecs_entity_t entity,
3181 ecs_id_t component,
3182 size_t size);
3183
3184/** Ensure an entity has a component and return a pointer.
3185 * This operation returns a mutable pointer to a component. If the entity did
3186 * not yet have the component, it will be added.
3187 *
3188 * If ensure() is called when the world is in deferred or read-only mode, the
3189 * function will:
3190 * - return a pointer to temporary storage if the component does not yet exist, or
3191 * - return a pointer to the existing component if it exists
3192 *
3193 * @param world The world.
3194 * @param entity The entity.
3195 * @param component The component to get or add.
3196 * @param size The size of the component.
3197 * @return The component pointer.
3198 *
3199 * @see ecs_emplace_id()
3200 */
3201FLECS_API
3203 ecs_world_t *world,
3204 ecs_entity_t entity,
3205 ecs_id_t component,
3206 size_t size);
3207
3208/** Create a component ref.
3209 * A ref is a handle to an entity and component pair, which caches a small amount of
3210 * data to reduce the overhead of repeatedly accessing the component. Use
3211 * ecs_ref_get() to get the component data.
3212 *
3213 * @param world The world.
3214 * @param entity The entity.
3215 * @param component The component to create a ref for.
3216 * @return The reference.
3217 */
3218FLECS_ALWAYS_INLINE FLECS_API
3220 const ecs_world_t *world,
3221 ecs_entity_t entity,
3222 ecs_id_t component);
3223
3224/** Get a component from a ref.
3225 * Get a component pointer from a ref. The ref must be created with ecs_ref_init().
3226 * The specified component must match the component with which the ref was
3227 * created.
3228 *
3229 * @param world The world.
3230 * @param ref The ref.
3231 * @param component The component to get.
3232 * @return The component pointer, NULL if the entity does not have the component.
3233 */
3234FLECS_ALWAYS_INLINE FLECS_API
3236 const ecs_world_t *world,
3237 ecs_ref_t *ref,
3238 ecs_id_t component);
3239
3240/** Update a ref.
3241 * Ensure the contents of a ref are up to date. Same as ecs_ref_get_id(), but does not
3242 * return a pointer to the component.
3243 *
3244 * @param world The world.
3245 * @param ref The ref.
3246 * @param component The component the ref was created with.
3247 */
3248FLECS_ALWAYS_INLINE FLECS_API
3250 const ecs_world_t *world,
3251 ecs_ref_t *ref,
3252 ecs_id_t component);
3253
3254/** Emplace a component.
3255 * Emplace is similar to ecs_ensure_id() except that the component constructor
3256 * is not invoked for the returned pointer, allowing the component to be
3257 * constructed directly in the storage.
3258 *
3259 * When the `is_new` parameter is not provided, the operation will assert when the
3260 * component already exists. When the `is_new` parameter is provided, it will
3261 * indicate whether the returned storage has been constructed.
3262 *
3263 * When `is_new` indicates that the storage has not yet been constructed, it must
3264 * be constructed by the code invoking this operation. Not constructing the
3265 * component will result in undefined behavior.
3266 *
3267 * @param world The world.
3268 * @param entity The entity.
3269 * @param component The component to get or add.
3270 * @param size The component size.
3271 * @param is_new Whether this is an existing or new component.
3272 * @return The (uninitialized) component pointer.
3273 */
3274FLECS_API
3276 ecs_world_t *world,
3277 ecs_entity_t entity,
3278 ecs_id_t component,
3279 size_t size,
3280 bool *is_new);
3281
3282/** Signal that a component has been modified.
3283 * This operation is usually used after modifying a component value obtained by
3284 * ecs_ensure_id(). The operation will mark the component as dirty, and invoke
3285 * OnSet observers and hooks.
3286 *
3287 * @param world The world.
3288 * @param entity The entity.
3289 * @param component The component that was modified.
3290 */
3291FLECS_API
3293 ecs_world_t *world,
3294 ecs_entity_t entity,
3295 ecs_id_t component);
3296
3297/** Set the value of a component.
3298 * This operation allows an application to set the value of a component. The
3299 * operation is equivalent to calling ecs_ensure_id() followed by
3300 * ecs_modified_id(). The operation will not modify the value of the passed-in
3301 * component. If the component has a copy hook registered, it will be used to
3302 * copy in the component.
3303 *
3304 * If the provided entity is 0, a new entity will be created.
3305 *
3306 * @param world The world.
3307 * @param entity The entity.
3308 * @param component The component to set.
3309 * @param size The size of the pointed-to value.
3310 * @param ptr The pointer to the value.
3311 */
3312FLECS_API
3314 ecs_world_t *world,
3315 ecs_entity_t entity,
3316 ecs_id_t component,
3317 size_t size,
3318 const void *ptr);
3319
3320/** @} */
3321
3322/**
3323 * @defgroup liveliness Entity Liveliness
3324 * Functions for testing and modifying entity liveliness.
3325 *
3326 * @{
3327 */
3328
3329/** Test whether an entity is valid.
3330 * This operation tests whether the entity ID:
3331 * - is not 0
3332 * - has a valid bit pattern
3333 * - is alive (see ecs_is_alive())
3334 *
3335 * If this operation returns true, it is safe to use the entity with
3336 * other operations.
3337 *
3338 * This operation should only be used if an application cannot be sure that an
3339 * entity is initialized with a valid value. In all other cases where an entity
3340 * was initialized with a valid value, but the application wants to check if the
3341 * entity is (still) alive, use ecs_is_alive().
3342 *
3343 * @param world The world.
3344 * @param e The entity.
3345 * @return True if the entity is valid, false if the entity is not valid.
3346 * @see ecs_is_alive()
3347 */
3348FLECS_API
3350 const ecs_world_t *world,
3351 ecs_entity_t e);
3352
3353/** Test whether an entity is alive.
3354 * Entities are alive after they are created, and become not alive when they are
3355 * deleted. Operations that return alive IDs are (amongst others) ecs_new(),
3356 * ecs_new_low_id() and ecs_entity_init(). IDs can be made alive with the
3357 * ecs_make_alive() function.
3358 *
3359 * After an ID is deleted it can be recycled. Recycled IDs are different from
3360 * the original ID in that they have a different generation count. This makes it
3361 * possible for the API to distinguish between the two. An example:
3362 *
3363 * @code
3364 * ecs_entity_t e1 = ecs_new(world);
3365 * ecs_is_alive(world, e1); // true
3366 * ecs_delete(world, e1);
3367 * ecs_is_alive(world, e1); // false
3368 *
3369 * ecs_entity_t e2 = ecs_new(world); // recycles e1
3370 * ecs_is_alive(world, e2); // true
3371 * ecs_is_alive(world, e1); // false
3372 * @endcode
3373 *
3374 * Unlike ecs_is_valid(), this operation will panic if the passed-in entity
3375 * ID is 0 or has an invalid bit pattern.
3376 *
3377 * @param world The world.
3378 * @param e The entity.
3379 * @return True if the entity is alive, false if the entity is not alive.
3380 * @see ecs_is_valid()
3381 */
3382FLECS_API
3384 const ecs_world_t *world,
3385 ecs_entity_t e);
3386
3387/** Remove the generation from an entity ID.
3388 *
3389 * @param e The entity ID.
3390 * @return The entity ID without the generation count.
3391 */
3392FLECS_API
3394 ecs_entity_t e);
3395
3396/** Get an alive identifier.
3397 * In some cases an application may need to work with identifiers from which
3398 * the generation has been stripped. A typical scenario in which this happens is
3399 * when iterating relationships in an entity type.
3400 *
3401 * For example, when obtaining the parent ID from a `ChildOf` relationship, the parent
3402 * (second element of the pair) will have been stored in a 32-bit value, which
3403 * cannot store the entity generation. This function can retrieve the identifier
3404 * with the current generation for that ID.
3405 *
3406 * If the provided identifier is not alive, the function will return 0.
3407 *
3408 * @param world The world.
3409 * @param e The entity for which to obtain the current alive entity ID.
3410 * @return The alive entity ID if there is one, or 0 if the ID is not alive.
3411 */
3412FLECS_API
3414 const ecs_world_t *world,
3415 ecs_entity_t e);
3416
3417/** Ensure an ID is alive.
3418 * This operation ensures that the provided ID is alive. This is useful in
3419 * scenarios where an application has an existing ID that has not been created
3420 * with ecs_new_w() (such as a global constant or an ID from a remote application).
3421 *
3422 * When this operation is successful, it guarantees that the provided ID exists,
3423 * is valid, and is alive.
3424 *
3425 * Before this operation, the ID must either not be alive or have a generation
3426 * that is equal to the passed-in entity.
3427 *
3428 * If the provided ID has a non-zero generation count and the ID does not exist
3429 * in the world, the ID will be created with the specified generation.
3430 *
3431 * If the provided ID is alive and has a generation count that does not match
3432 * the provided ID, the operation will fail.
3433 *
3434 * @param world The world.
3435 * @param entity The entity ID to make alive.
3436 *
3437 * @see ecs_make_alive_id()
3438 */
3439FLECS_API
3441 ecs_world_t *world,
3442 ecs_entity_t entity);
3443
3444/** Same as ecs_make_alive(), but for components.
3445 * An ID can be an entity or a pair, and can contain ID flags. This operation
3446 * ensures that the entity (or entities, for a pair) are alive.
3447 *
3448 * When this operation is successful, it guarantees that the provided ID can be
3449 * used in operations that accept an ID.
3450 *
3451 * Since entities in a pair do not encode their generation IDs, this operation
3452 * will not fail when an entity with non-zero generation count already exists in
3453 * the world.
3454 *
3455 * This is different from ecs_make_alive(), which will fail if attempted with an ID
3456 * that has generation 0 and an entity with a non-zero generation is currently
3457 * alive.
3458 *
3459 * @param world The world.
3460 * @param component The component to make alive.
3461 */
3462FLECS_API
3464 ecs_world_t *world,
3465 ecs_id_t component);
3466
3467/** Test whether an entity exists.
3468 * Similar to ecs_is_alive(), but ignores the entity generation count.
3469 *
3470 * @param world The world.
3471 * @param entity The entity.
3472 * @return True if the entity exists, false if the entity does not exist.
3473 */
3474FLECS_API
3476 const ecs_world_t *world,
3477 ecs_entity_t entity);
3478
3479/** Override the generation of an entity.
3480 * The generation count of an entity is increased each time an entity is deleted
3481 * and is used to test whether an entity ID is alive.
3482 *
3483 * This operation overrides the current generation of an entity with the
3484 * specified generation, which can be useful if an entity is externally managed,
3485 * like for external pools, savefiles, or netcode.
3486 *
3487 * This operation is similar to ecs_make_alive(), except that it will also
3488 * override the generation of an alive entity.
3489 *
3490 * @param world The world.
3491 * @param entity The entity for which to set the generation.
3492 */
3493FLECS_API
3495 ecs_world_t *world,
3496 ecs_entity_t entity);
3497
3498/** Get the generation of an entity.
3499 *
3500 * @param entity The entity for which to get the generation.
3501 * @return The generation of the entity.
3502 */
3503FLECS_API
3505 ecs_entity_t entity);
3506
3507/** @} */
3508
3509/**
3510 * @defgroup entity_info Entity Information.
3511 * Get information from an entity.
3512 *
3513 * @{
3514 */
3515
3516/** Get the type of an entity.
3517 *
3518 * @param world The world.
3519 * @param entity The entity.
3520 * @return The type of the entity, NULL if the entity has no components.
3521 */
3522FLECS_API
3524 const ecs_world_t *world,
3525 ecs_entity_t entity);
3526
3527/** Get the table of an entity.
3528 *
3529 * @param world The world.
3530 * @param entity The entity.
3531 * @return The table of the entity, NULL if the entity has no components or tags.
3532 */
3533FLECS_API
3535 const ecs_world_t *world,
3536 ecs_entity_t entity);
3537
3538/** Convert a type to a string.
3539 * The result of this operation must be freed with ecs_os_free().
3540 *
3541 * @param world The world.
3542 * @param type The type.
3543 * @return The stringified type.
3544 */
3545FLECS_API
3547 const ecs_world_t *world,
3548 const ecs_type_t* type);
3549
3550/** Convert a table to a string.
3551 * Same as `ecs_type_str(world, ecs_table_get_type(table))`. The result of this
3552 * operation must be freed with ecs_os_free().
3553 *
3554 * @param world The world.
3555 * @param table The table.
3556 * @return The stringified table type.
3557 *
3558 * @see ecs_table_get_type()
3559 * @see ecs_type_str()
3560 */
3561FLECS_API
3563 const ecs_world_t *world,
3564 const ecs_table_t *table);
3565
3566/** Convert an entity to a string.
3567 * Same as combining:
3568 * - ecs_get_path(world, entity)
3569 * - ecs_type_str(world, ecs_get_type(world, entity))
3570 *
3571 * The result of this operation must be freed with ecs_os_free().
3572 *
3573 * @param world The world.
3574 * @param entity The entity.
3575 * @return The entity path with stringified type.
3576 *
3577 * @see ecs_get_path()
3578 * @see ecs_type_str()
3579 */
3580FLECS_API
3582 const ecs_world_t *world,
3583 ecs_entity_t entity);
3584
3585/** Test if an entity has a component.
3586 * This operation returns true if the entity has or inherits the component.
3587 *
3588 * @param world The world.
3589 * @param entity The entity.
3590 * @param component The component to test for.
3591 * @return True if the entity has the component, false if not.
3592 *
3593 * @see ecs_owns_id()
3594 */
3595FLECS_API
3596FLECS_ALWAYS_INLINE bool ecs_has_id(
3597 const ecs_world_t *world,
3598 ecs_entity_t entity,
3599 ecs_id_t component);
3600
3601/** Test if an entity owns a component.
3602 * This operation returns true if the entity has the component. The operation
3603 * behaves the same as ecs_has_id(), except that it will return false for
3604 * components that are inherited through an `IsA` relationship.
3605 *
3606 * @param world The world.
3607 * @param entity The entity.
3608 * @param component The component to test for.
3609 * @return True if the entity has the component, false if not.
3610 */
3611FLECS_API
3612FLECS_ALWAYS_INLINE bool ecs_owns_id(
3613 const ecs_world_t *world,
3614 ecs_entity_t entity,
3615 ecs_id_t component);
3616
3617/** Get the target of a relationship.
3618 * This will return a target (second element of a pair) of the entity for the
3619 * specified relationship. The index allows for iterating through the targets,
3620 * if a single entity has multiple targets for the same relationship.
3621 *
3622 * If the index is larger than the total number of instances the entity has for
3623 * the relationship, the operation will return 0.
3624 *
3625 * @param world The world.
3626 * @param entity The entity.
3627 * @param rel The relationship between the entity and the target.
3628 * @param index The index of the relationship instance.
3629 * @return The target for the relationship at the specified index.
3630 */
3631FLECS_API
3633 const ecs_world_t *world,
3634 ecs_entity_t entity,
3635 ecs_entity_t rel,
3636 int32_t index);
3637
3638/** Get the parent (target of the `ChildOf` relationship) for an entity.
3639 * This operation is the same as calling:
3640 *
3641 * @code
3642 * ecs_get_target(world, entity, EcsChildOf, 0);
3643 * @endcode
3644 *
3645 * @param world The world.
3646 * @param entity The entity.
3647 * @return The parent of the entity, 0 if the entity has no parent.
3648 *
3649 * @see ecs_get_target()
3650 */
3651FLECS_API
3653 const ecs_world_t *world,
3654 ecs_entity_t entity);
3655
3656/** Create child with Parent component.
3657 * This creates or returns an existing child for the specified parent. If a new
3658 * child is created, the Parent component is used to create the parent
3659 * relationship.
3660 *
3661 * If a child entity already exists with the specified name, it will be
3662 * returned.
3663 *
3664 * @param world The world.
3665 * @param parent The parent for which to create the child.
3666 * @param name The name with which to create the entity (may be NULL).
3667 * @return A new or existing child entity.
3668 */
3669FLECS_API
3671 ecs_world_t *world,
3672 ecs_entity_t parent,
3673 const char *name);
3674
3675/** Get the target of a relationship for a given component.
3676 * This operation returns the first entity that has the provided component by
3677 * following the relationship. If the entity itself has the component then it
3678 * will be returned. If the component cannot be found on the entity or by
3679 * following the relationship, the operation will return 0.
3680 *
3681 * This operation can be used to look up, for example, which prefab is providing
3682 * a component by specifying the `IsA` relationship:
3683 *
3684 * @code
3685 * // Is Position provided by the entity or one of its base entities?
3686 * ecs_get_target_for_id(world, entity, EcsIsA, ecs_id(Position))
3687 * @endcode
3688 *
3689 * @param world The world.
3690 * @param entity The entity.
3691 * @param rel The relationship to follow.
3692 * @param component The component to look up.
3693 * @return The entity for which the target has been found.
3694 */
3695FLECS_API
3697 const ecs_world_t *world,
3698 ecs_entity_t entity,
3699 ecs_entity_t rel,
3700 ecs_id_t component);
3701
3702/** Return the depth for an entity in the tree for the specified relationship.
3703 * Depth is determined by counting the number of targets encountered while
3704 * traversing up the relationship tree for `rel`. Only acyclic relationships are
3705 * supported.
3706 *
3707 * @param world The world.
3708 * @param entity The entity.
3709 * @param rel The relationship.
3710 * @return The depth of the entity in the tree.
3711 */
3712FLECS_API
3714 const ecs_world_t *world,
3715 ecs_entity_t entity,
3716 ecs_entity_t rel);
3717
3718/** Count entities that have the specified ID.
3719 * Return the number of entities that have the specified ID.
3720 *
3721 * @param world The world.
3722 * @param entity The ID to search for.
3723 * @return The number of entities that have the ID.
3724 */
3725FLECS_API
3727 const ecs_world_t *world,
3728 ecs_id_t entity);
3729
3730/** @} */
3731
3732
3733/**
3734 * @defgroup paths Entity Names
3735 * Functions for working with entity names and paths.
3736 *
3737 * @{
3738 */
3739
3740/** Get the name of an entity.
3741 * This will return the name stored in `(EcsIdentifier, EcsName)`.
3742 *
3743 * @param world The world.
3744 * @param entity The entity.
3745 * @return The name of the entity, NULL if the entity has no name.
3746 *
3747 * @see ecs_set_name()
3748 */
3749FLECS_API
3750const char* ecs_get_name(
3751 const ecs_world_t *world,
3752 ecs_entity_t entity);
3753
3754/** Get the symbol of an entity.
3755 * This will return the symbol stored in `(EcsIdentifier, EcsSymbol)`.
3756 *
3757 * @param world The world.
3758 * @param entity The entity.
3759 * @return The symbol of the entity, NULL if the entity has no symbol.
3760 *
3761 * @see ecs_set_symbol()
3762 */
3763FLECS_API
3764const char* ecs_get_symbol(
3765 const ecs_world_t *world,
3766 ecs_entity_t entity);
3767
3768/** Set the name of an entity.
3769 * This will set or overwrite the name of an entity. If no entity is provided,
3770 * a new entity will be created.
3771 *
3772 * The name is stored in `(EcsIdentifier, EcsName)`.
3773 *
3774 * @param world The world.
3775 * @param entity The entity.
3776 * @param name The name.
3777 * @return The provided entity, or a new entity if 0 was provided.
3778 *
3779 * @see ecs_get_name()
3780 */
3781FLECS_API
3783 ecs_world_t *world,
3784 ecs_entity_t entity,
3785 const char *name);
3786
3787/** Set the symbol of an entity.
3788 * This will set or overwrite the symbol of an entity. If no entity is provided,
3789 * a new entity will be created.
3790 *
3791 * The symbol is stored in `(EcsIdentifier, EcsSymbol)`.
3792 *
3793 * @param world The world.
3794 * @param entity The entity.
3795 * @param symbol The symbol.
3796 * @return The provided entity, or a new entity if 0 was provided.
3797 *
3798 * @see ecs_get_symbol()
3799 */
3800FLECS_API
3802 ecs_world_t *world,
3803 ecs_entity_t entity,
3804 const char *symbol);
3805
3806/** Set an alias for an entity.
3807 * An entity can be looked up using its alias from the root scope without
3808 * providing the fully qualified name of its parent. An entity can only have
3809 * a single alias.
3810 *
3811 * The alias is stored in `(EcsIdentifier, EcsAlias)`.
3812 *
3813 * @param world The world.
3814 * @param entity The entity.
3815 * @param alias The alias.
3816 */
3817FLECS_API
3819 ecs_world_t *world,
3820 ecs_entity_t entity,
3821 const char *alias);
3822
3823/** Look up an entity by its path.
3824 * This operation is equivalent to calling:
3825 *
3826 * @code
3827 * ecs_lookup_path_w_sep(world, 0, path, ".", NULL, true);
3828 * @endcode
3829 *
3830 * @param world The world.
3831 * @param path The entity path.
3832 * @return The entity with the specified path, or 0 if no entity was found.
3833 *
3834 * @see ecs_lookup_child()
3835 * @see ecs_lookup_path_w_sep()
3836 * @see ecs_lookup_symbol()
3837 */
3838FLECS_API
3840 const ecs_world_t *world,
3841 const char *path);
3842
3843/** Look up a child entity by name.
3844 * Return an entity that matches the specified name. Only look for entities in
3845 * the provided parent. If no parent is provided, look in the current scope
3846 * (root if no scope is provided).
3847 *
3848 * @param world The world.
3849 * @param parent The parent for which to look up the child.
3850 * @param name The entity name.
3851 * @return The entity with the specified name, or 0 if no entity was found.
3852 *
3853 * @see ecs_lookup()
3854 * @see ecs_lookup_path_w_sep()
3855 * @see ecs_lookup_symbol()
3856 */
3857FLECS_API
3859 const ecs_world_t *world,
3860 ecs_entity_t parent,
3861 const char *name);
3862
3863/** Look up an entity from a path.
3864 * Look up an entity from a provided path, relative to the provided parent. The
3865 * operation will use the provided separator to tokenize the path expression. If
3866 * the provided path contains the prefix, the search will start from the root.
3867 *
3868 * If the entity is not found in the provided parent, the operation will
3869 * continue to search in the parent of the parent, until the root is reached. If
3870 * the entity is still not found, the lookup will search in the `flecs.core`
3871 * scope. If the entity is not found there either, the function returns 0.
3872 *
3873 * @param world The world.
3874 * @param parent The entity from which to resolve the path.
3875 * @param path The path to resolve.
3876 * @param sep The path separator.
3877 * @param prefix The path prefix.
3878 * @param recursive Recursively traverse up the tree until the entity is found.
3879 * @return The entity if found, else 0.
3880 *
3881 * @see ecs_lookup()
3882 * @see ecs_lookup_child()
3883 * @see ecs_lookup_symbol()
3884 */
3885FLECS_API
3887 const ecs_world_t *world,
3888 ecs_entity_t parent,
3889 const char *path,
3890 const char *sep,
3891 const char *prefix,
3892 bool recursive);
3893
3894/** Look up an entity by its symbol name.
3895 * This looks up an entity by the symbol stored in `(EcsIdentifier, EcsSymbol)`. The
3896 * operation does not take into account hierarchies.
3897 *
3898 * This operation can be useful to resolve, for example, a type by its C
3899 * identifier, which does not include the Flecs namespacing.
3900 *
3901 * @param world The world.
3902 * @param symbol The symbol.
3903 * @param lookup_as_path If not found as a symbol, look up as path.
3904 * @param recursive If looking up as path, recursively traverse up the tree.
3905 * @return The entity if found, else 0.
3906 *
3907 * @see ecs_lookup()
3908 * @see ecs_lookup_child()
3909 * @see ecs_lookup_path_w_sep()
3910 */
3911FLECS_API
3913 const ecs_world_t *world,
3914 const char *symbol,
3915 bool lookup_as_path,
3916 bool recursive);
3917
3918/** Get a path identifier for an entity.
3919 * This operation creates a path that contains the names of the entities from
3920 * the specified parent to the provided entity, separated by the provided
3921 * separator. If no parent is provided, the path will be relative to the root. If
3922 * a prefix is provided, the path will be prefixed by the prefix.
3923 *
3924 * If the parent is equal to the provided child, the operation will return an
3925 * empty string. If a non-zero component is provided, the path will be created by
3926 * looking for parents with that component.
3927 *
3928 * The returned path should be freed by the application.
3929 *
3930 * @param world The world.
3931 * @param parent The entity from which to create the path.
3932 * @param child The entity to which to create the path.
3933 * @param sep The separator to use between path elements.
3934 * @param prefix The initial character to use for root elements.
3935 * @return The relative entity path.
3936 *
3937 * @see ecs_get_path_w_sep_buf()
3938 */
3939FLECS_API
3941 const ecs_world_t *world,
3942 ecs_entity_t parent,
3943 ecs_entity_t child,
3944 const char *sep,
3945 const char *prefix);
3946
3947/** Write a path identifier to a buffer.
3948 * Same as ecs_get_path_w_sep(), but writes the result to an `ecs_strbuf_t`.
3949 *
3950 * @param world The world.
3951 * @param parent The entity from which to create the path.
3952 * @param child The entity to which to create the path.
3953 * @param sep The separator to use between path elements.
3954 * @param prefix The initial character to use for root elements.
3955 * @param buf The buffer to write to.
3956 * @param escape Whether to escape separator characters in names.
3957 *
3958 * @see ecs_get_path_w_sep()
3959 */
3960FLECS_API
3962 const ecs_world_t *world,
3963 ecs_entity_t parent,
3964 ecs_entity_t child,
3965 const char *sep,
3966 const char *prefix,
3967 ecs_strbuf_t *buf,
3968 bool escape);
3969
3970/** Find or create an entity from a path.
3971 * This operation will find or create an entity from a path, and will create any
3972 * intermediate entities if required. If the entity already exists, no entities
3973 * will be created.
3974 *
3975 * If the path starts with the prefix, then the entity will be created from the
3976 * root scope.
3977 *
3978 * @param world The world.
3979 * @param parent The entity relative to which the entity should be created.
3980 * @param path The path to create the entity for.
3981 * @param sep The separator used in the path.
3982 * @param prefix The prefix used in the path.
3983 * @return The entity.
3984 */
3985FLECS_API
3987 ecs_world_t *world,
3988 ecs_entity_t parent,
3989 const char *path,
3990 const char *sep,
3991 const char *prefix);
3992
3993/** Add a specified path to an entity.
3994 * This operation is similar to ecs_new_from_path(), but will instead add the path
3995 * to an existing entity.
3996 *
3997 * If an entity already exists for the path, it will be returned instead.
3998 *
3999 * @param world The world.
4000 * @param entity The entity to which to add the path.
4001 * @param parent The entity relative to which the entity should be created.
4002 * @param path The path to create the entity for.
4003 * @param sep The separator used in the path.
4004 * @param prefix The prefix used in the path.
4005 * @return The entity.
4006 */
4007FLECS_API
4009 ecs_world_t *world,
4010 ecs_entity_t entity,
4011 ecs_entity_t parent,
4012 const char *path,
4013 const char *sep,
4014 const char *prefix);
4015
4016/** Set the current scope.
4017 * This operation sets the scope of the current stage to the provided entity.
4018 * As a result, new entities will be created in this scope, and lookups will be
4019 * relative to the provided scope.
4020 *
4021 * It is considered good practice to restore the scope to the old value.
4022 *
4023 * @param world The world.
4024 * @param scope The entity to use as scope.
4025 * @return The previous scope.
4026 *
4027 * @see ecs_get_scope()
4028 */
4029FLECS_API
4031 ecs_world_t *world,
4032 ecs_entity_t scope);
4033
4034/** Get the current scope.
4035 * Get the scope set by ecs_set_scope(). If no scope is set, this operation will
4036 * return 0.
4037 *
4038 * @param world The world.
4039 * @return The current scope.
4040 */
4041FLECS_API
4043 const ecs_world_t *world);
4044
4045/** Set a name prefix for newly created entities.
4046 * This is a utility that lets C modules use prefixed names for C types and
4047 * C functions, while using names for the entity names that do not have the
4048 * prefix. The name prefix is currently only used by `ECS_COMPONENT`.
4049 *
4050 * @param world The world.
4051 * @param prefix The name prefix to use.
4052 * @return The previous prefix.
4053 */
4054FLECS_API
4056 ecs_world_t *world,
4057 const char *prefix);
4058
4059/** Set the search path for lookup operations.
4060 * This operation accepts an array of entity IDs that will be used as search
4061 * scopes by lookup operations. The operation returns the current search path.
4062 * It is good practice to restore the old search path.
4063 *
4064 * The search path will be evaluated starting from the last element.
4065 *
4066 * The default search path includes `flecs.core`. When a custom search path is
4067 * provided, it overwrites the existing search path. Operations that rely on
4068 * looking up names from `flecs.core` without providing the namespace may fail if
4069 * the custom search path does not include `flecs.core` (`EcsFlecsCore`).
4070 *
4071 * The search path array is not copied into managed memory. The application must
4072 * ensure that the provided array is valid for as long as it is used as the
4073 * search path.
4074 *
4075 * The provided array must be terminated with a 0 element. This enables an
4076 * application to push or pop elements to an existing array without invoking the
4077 * ecs_set_lookup_path() operation again.
4078 *
4079 * @param world The world.
4080 * @param lookup_path 0-terminated array with entity IDs for the lookup path.
4081 * @return The current lookup path array.
4082 *
4083 * @see ecs_get_lookup_path()
4084 */
4085FLECS_API
4087 ecs_world_t *world,
4088 const ecs_entity_t *lookup_path);
4089
4090/** Get the current lookup path.
4091 * Return the value set by ecs_set_lookup_path().
4092 *
4093 * @param world The world.
4094 * @return The current lookup path.
4095 */
4096FLECS_API
4098 const ecs_world_t *world);
4099
4100/** @} */
4101
4102/** @} */
4103
4104/**
4105 * @defgroup components Components
4106 * Functions for registering and working with components.
4107 *
4108 * @{
4109 */
4110
4111/** Find or create a component.
4112 * This operation creates a new component, or finds an existing one. The find or
4113 * create behavior is the same as ecs_entity_init().
4114 *
4115 * When an existing component is found, the size and alignment are verified with
4116 * the provided values. If the values do not match, the operation will fail.
4117 *
4118 * See the documentation of ecs_component_desc_t for more details.
4119 *
4120 * @param world The world.
4121 * @param desc Component init parameters.
4122 * @return A handle to the new or existing component, or 0 if failed.
4123 */
4124FLECS_API
4126 ecs_world_t *world,
4127 const ecs_component_desc_t *desc);
4128
4129/** Get the type info for a component.
4130 * This function returns the type information for a component. The component can
4131 * be a regular component or a pair. For the rules on how type information is
4132 * determined based on a component ID, see ecs_get_typeid().
4133 *
4134 * @param world The world.
4135 * @param component The component.
4136 * @return The type information of the component ID.
4137 */
4138FLECS_API
4140 const ecs_world_t *world,
4141 ecs_id_t component);
4142
4143/** Register hooks for a component.
4144 * Hooks allow for the execution of user code when components are constructed,
4145 * copied, moved, destructed, added, removed, or set. Hooks can be assigned
4146 * as long as a component has not yet been used (added to an entity).
4147 *
4148 * The hooks that are currently set can be accessed with ecs_get_type_info().
4149 *
4150 * @param world The world.
4151 * @param component The component for which to register the actions.
4152 * @param hooks The type that contains the component actions.
4153 */
4154FLECS_API
4156 ecs_world_t *world,
4157 ecs_entity_t component,
4158 const ecs_type_hooks_t *hooks);
4159
4160/** Get hooks for a component.
4161 *
4162 * @param world The world.
4163 * @param component The component for which to retrieve the hooks.
4164 * @return The hooks for the component, or NULL if not registered.
4165 */
4166FLECS_API
4168 const ecs_world_t *world,
4169 ecs_entity_t component);
4170
4171/** @} */
4172
4173/**
4174 * @defgroup ids IDs
4175 * Functions for working with `ecs_id_t`.
4176 *
4177 * @{
4178 */
4179
4180/** Return whether a specified component is a tag.
4181 * This operation returns whether the specified component is a tag (a component
4182 * without data or size).
4183 *
4184 * An ID is a tag when:
4185 * - it is an entity without the `EcsComponent` component
4186 * - it has an `EcsComponent` with size member set to 0
4187 * - it is a pair where both elements are a tag
4188 * - it is a pair where the first element has the #EcsPairIsTag tag
4189 *
4190 * @param world The world.
4191 * @param component The component.
4192 * @return Whether the provided ID is a tag.
4193 */
4194FLECS_API
4196 const ecs_world_t *world,
4197 ecs_id_t component);
4198
4199/** Return whether a specified component is in use.
4200 * This operation returns whether a component is in use in the world. A
4201 * component is in use if it has been added to one or more tables.
4202 *
4203 * @param world The world.
4204 * @param component The component.
4205 * @return Whether the component is in use.
4206 */
4207FLECS_API
4209 const ecs_world_t *world,
4210 ecs_id_t component);
4211
4212/** Get the type for a component.
4213 * This operation returns the type for a component ID, if the ID is associated
4214 * with a type. For a regular component with a non-zero size (an entity with the
4215 * EcsComponent component), the operation will return the component ID itself.
4216 *
4217 * For an entity that does not have the EcsComponent component, or with an
4218 * EcsComponent value with size 0, the operation will return 0.
4219 *
4220 * For a pair ID, the operation will return the type associated with the pair, by
4221 * applying the following queries in order:
4222 * - The first pair element is returned if it is a component.
4223 * - 0 is returned if the relationship entity has the Tag property.
4224 * - The second pair element is returned if it is a component.
4225 * - 0 is returned.
4226 *
4227 * @param world The world.
4228 * @param component The component.
4229 * @return The type of the component.
4230 */
4231FLECS_API
4233 const ecs_world_t *world,
4234 ecs_id_t component);
4235
4236/** Utility to match a component with a pattern.
4237 * This operation returns true if the provided pattern matches the provided
4238 * component. The pattern may contain a wildcard (or wildcards, when a pair).
4239 *
4240 * @param component The component.
4241 * @param pattern The pattern to compare with.
4242 * @return Whether the ID matches the pattern.
4243 */
4244FLECS_API
4246 ecs_id_t component,
4247 ecs_id_t pattern);
4248
4249/** Utility to check if a component is a pair.
4250 *
4251 * @param component The component.
4252 * @return True if the component is a pair.
4253 */
4254FLECS_API
4256 ecs_id_t component);
4257
4258/** Utility to check if a component is a wildcard.
4259 *
4260 * @param component The component.
4261 * @return True if the component is a wildcard or a pair containing a wildcard.
4262 */
4263FLECS_API
4265 ecs_id_t component);
4266
4267/** Utility to check if a component is an any wildcard.
4268 *
4269 * @param component The component.
4270 * @return True if the component is an any wildcard or a pair containing an any wildcard.
4271 */
4273 ecs_id_t component);
4274
4275/** Utility to check if an ID is valid.
4276 * A valid ID is an ID that can be added to an entity. Invalid IDs are:
4277 * - IDs that contain wildcards
4278 * - IDs that contain invalid entities
4279 * - IDs that are 0 or contain 0 entities
4280 *
4281 * Note that the same rules apply to removing from an entity, with the exception
4282 * of wildcards.
4283 *
4284 * @param world The world.
4285 * @param component The component.
4286 * @return True if the ID is valid.
4287 */
4288FLECS_API
4290 const ecs_world_t *world,
4291 ecs_id_t component);
4292
4293/** Get flags associated with an ID.
4294 * This operation returns the internal flags (see api_flags.h) that are
4295 * associated with the provided ID.
4296 *
4297 * @param world The world.
4298 * @param component The component.
4299 * @return The flags associated with the ID, or 0 if the ID is not in use.
4300 */
4301FLECS_API
4302ecs_flags32_t ecs_id_get_flags(
4303 const ecs_world_t *world,
4304 ecs_id_t component);
4305
4306/** Convert a component flag to a string.
4307 * This operation converts a component flag to a string. Possible outputs are:
4308 *
4309 * - PAIR
4310 * - TOGGLE
4311 * - AUTO_OVERRIDE
4312 *
4313 * @param component_flags The component flag.
4314 * @return The ID flag string, or NULL if no valid ID is provided.
4315 */
4316FLECS_API
4318 uint64_t component_flags);
4319
4320/** Convert a component ID to a string.
4321 * This operation converts the provided component ID to a string. It can output
4322 * strings of the following formats:
4323 *
4324 * - "ComponentName"
4325 * - "FLAG|ComponentName"
4326 * - "(Relationship, Target)"
4327 * - "FLAG|(Relationship, Target)"
4328 *
4329 * The PAIR flag is never added to the string.
4330 *
4331 * @param world The world.
4332 * @param component The component to convert to a string.
4333 * @return The component converted to a string.
4334 */
4335FLECS_API
4337 const ecs_world_t *world,
4338 ecs_id_t component);
4339
4340/** Write a component string to a buffer.
4341 * Same as ecs_id_str(), but writes the result to ecs_strbuf_t.
4342 *
4343 * @param world The world.
4344 * @param component The component to convert to a string.
4345 * @param buf The buffer to write to.
4346 */
4347FLECS_API
4349 const ecs_world_t *world,
4350 ecs_id_t component,
4351 ecs_strbuf_t *buf);
4352
4353/** Convert a string to a component.
4354 * This operation is the reverse of ecs_id_str(). The FLECS_SCRIPT addon
4355 * is required for this operation to work.
4356 *
4357 * @param world The world.
4358 * @param expr The string to convert to an ID.
4359 * @return The ID, or 0 if the string could not be converted.
4360 */
4361FLECS_API
4363 const ecs_world_t *world,
4364 const char *expr);
4365
4366/** @} */
4367
4368/**
4369 * @defgroup queries Queries
4370 * @brief Functions for working with `ecs_term_t` and `ecs_query_t`.
4371 * @{
4372 */
4373
4374/** Test whether a term ref is set.
4375 * A term ref is a reference to an entity, component, or variable for one of the
4376 * three parts of a term (src, first, second).
4377 *
4378 * @param ref The term ref.
4379 * @return True when set, false when not set.
4380 */
4381FLECS_API
4383 const ecs_term_ref_t *ref);
4384
4385/** Test whether a term is set.
4386 * This operation can be used to test whether a term has been initialized with
4387 * values or whether it is empty.
4388 *
4389 * An application generally does not need to invoke this operation. It is useful
4390 * when initializing a 0-initialized array of terms (like in ecs_query_desc_t), as
4391 * this operation can be used to find the last initialized element.
4392 *
4393 * @param term The term.
4394 * @return True when set, false when not set.
4395 */
4396FLECS_API
4398 const ecs_term_t *term);
4399
4400/** Is a term matched on the $this variable.
4401 * This operation checks whether a term is matched on the $this variable, which
4402 * is the default source for queries.
4403 *
4404 * A term has a $this source when:
4405 * - ecs_term_t::src::id is EcsThis
4406 * - ecs_term_t::src::flags is EcsIsVariable
4407 *
4408 * If ecs_term_t::src is not populated, it will be automatically initialized to
4409 * the $this source for the created query.
4410 *
4411 * @param term The term.
4412 * @return True if the term matches $this, false if not.
4413 */
4414FLECS_API
4416 const ecs_term_t *term);
4417
4418/** Is a term matched on a 0 source.
4419 * This operation checks whether a term is matched on a 0 source. A 0 source is
4420 * a term that isn't matched against anything, and can be used just to pass
4421 * (component) IDs to a query iterator.
4422 *
4423 * A term has a 0 source when:
4424 * - ecs_term_t::src::id is 0
4425 * - ecs_term_t::src::flags has EcsIsEntity set
4426 *
4427 * @param term The term.
4428 * @return True if the term has a 0 source, false if not.
4429 */
4430FLECS_API
4432 const ecs_term_t *term);
4433
4434/** Convert a term to a string expression.
4435 * Convert a term to a string expression. The resulting expression is equivalent
4436 * to the same term, with the exception of And and Or operators.
4437 *
4438 * @param world The world.
4439 * @param term The term.
4440 * @return The term converted to a string.
4441 */
4442FLECS_API
4444 const ecs_world_t *world,
4445 const ecs_term_t *term);
4446
4447/** Convert a query to a string expression.
4448 * Convert a query to a string expression. The resulting expression can be
4449 * parsed to create the same query.
4450 *
4451 * @param query The query.
4452 * @return The query converted to a string.
4453 */
4454FLECS_API
4456 const ecs_query_t *query);
4457
4458/** @} */
4459
4460/**
4461 * @defgroup each_iter Each iterator
4462 * @brief Find all entities that have a single (component) ID.
4463 * @{
4464 */
4465
4466/** Iterate all entities with a specified (component ID).
4467 * This returns an iterator that yields all entities with a single specified
4468 * component. This is a much lighter-weight operation than creating and
4469 * iterating a query.
4470 *
4471 * Usage:
4472 * @code
4473 * ecs_iter_t it = ecs_each(world, Player);
4474 * while (ecs_each_next(&it)) {
4475 * for (int i = 0; i < it.count; i ++) {
4476 * // Iterate as usual.
4477 * }
4478 * }
4479 * @endcode
4480 *
4481 * If the specified ID is a component, it is possible to access the component
4482 * pointer with ecs_field() just like with regular queries:
4483 *
4484 * @code
4485 * ecs_iter_t it = ecs_each(world, Position);
4486 * while (ecs_each_next(&it)) {
4487 * Position *p = ecs_field(&it, Position, 0);
4488 * for (int i = 0; i < it.count; i ++) {
4489 * // Iterate as usual.
4490 * }
4491 * }
4492 * @endcode
4493 *
4494 * @param world The world.
4495 * @param component The component to iterate.
4496 * @return An iterator that iterates all entities with the (component) ID.
4497 */
4498FLECS_API
4500 const ecs_world_t *world,
4501 ecs_id_t component);
4502
4503/** Progress an iterator created with ecs_each_id().
4504 *
4505 * @param it The iterator.
4506 * @return True if the iterator has more results, false if not.
4507 */
4508FLECS_API
4510 ecs_iter_t *it);
4511
4512/** Iterate children of a parent.
4513 * This operation is usually equivalent to doing:
4514 * @code
4515 * ecs_iter_t it = ecs_each_id(world, ecs_pair(EcsChildOf, parent));
4516 * @endcode
4517 *
4518 * The only exception is when the parent has the EcsOrderedChildren trait, in
4519 * which case this operation will return a single result with the ordered
4520 * child entity IDs.
4521 *
4522 * This operation is equivalent to doing:
4523 *
4524 * @code
4525 * ecs_children_w_rel(world, EcsChildOf, parent);
4526 * @endcode
4527 *
4528 * @param world The world.
4529 * @param parent The parent.
4530 * @return An iterator that iterates all children of the parent.
4531 *
4532 * @see ecs_each_id()
4533 */
4534FLECS_API
4535FLECS_ALWAYS_INLINE ecs_iter_t ecs_children(
4536 const ecs_world_t *world,
4537 ecs_entity_t parent);
4538
4539/** Same as ecs_children(), but with a custom relationship argument.
4540 *
4541 * @param world The world.
4542 * @param relationship The relationship.
4543 * @param parent The parent.
4544 * @return An iterator that iterates all children of the parent.
4545 */
4546FLECS_API
4547FLECS_ALWAYS_INLINE ecs_iter_t ecs_children_w_rel(
4548 const ecs_world_t *world,
4549 ecs_entity_t relationship,
4550 ecs_entity_t parent);
4551
4552/** Progress an iterator created with ecs_children().
4553 *
4554 * @param it The iterator.
4555 * @return True if the iterator has more results, false if not.
4556 */
4557FLECS_API
4559 ecs_iter_t *it);
4560
4561/** @} */
4562
4563/**
4564 * @defgroup queries Queries
4565 * Functions for working with `ecs_query_t`.
4566 *
4567 * @{
4568 */
4569
4570/** Create a query.
4571 * If the descriptor specifies an existing entity, the entity must not already
4572 * be associated with a query. To replace an existing query on an entity, use
4573 * ecs_query_update().
4574 *
4575 * @param world The world.
4576 * @param desc The descriptor (see ecs_query_desc_t).
4577 * @return The query.
4578 */
4579FLECS_API
4581 ecs_world_t *world,
4582 const ecs_query_desc_t *desc);
4583
4584/** Replace the query on an existing entity.
4585 * Removes the query currently attached to the entity and creates a new one
4586 * from the descriptor. Any handles to the previous query become invalid; use
4587 * the returned handle for subsequent iteration.
4588 *
4589 * @param world The world.
4590 * @param entity The entity that holds the query to replace.
4591 * @param desc The descriptor (see ecs_query_desc_t).
4592 * @return The new query, or NULL if the operation failed.
4593 */
4594FLECS_API
4596 ecs_world_t *world,
4597 ecs_entity_t entity,
4598 const ecs_query_desc_t *desc);
4599
4600/** Delete a query.
4601 *
4602 * @param query The query.
4603 */
4604FLECS_API
4606 ecs_query_t *query);
4607
4608#ifdef FLECS_QUERY_PLANS
4609
4610/** Find a variable index.
4611 * This operation looks up the index of a variable in the query. This index can
4612 * be used in operations like ecs_iter_set_var() and ecs_iter_get_var().
4613 *
4614 * @param query The query.
4615 * @param name The variable name.
4616 * @return The variable index.
4617 */
4618FLECS_API
4620 const ecs_query_t *query,
4621 const char *name);
4622
4623/** Get the variable name.
4624 * This operation returns the variable name for an index.
4625 *
4626 * @param query The query.
4627 * @param var_id The variable index.
4628 * @return The variable name.
4629 */
4630FLECS_API
4632 const ecs_query_t *query,
4633 int32_t var_id);
4634
4635/** Test if a variable is an entity.
4636 * Internally, the query engine has entity variables and table variables. When
4637 * iterating through query variables (by using ecs_query_t::var_count) only
4638 * the values for entity variables are accessible. This operation enables an
4639 * application to check if a variable is an entity variable.
4640 *
4641 * @param query The query.
4642 * @param var_id The variable ID.
4643 * @return Whether the variable is an entity variable.
4644 */
4645FLECS_API
4647 const ecs_query_t *query,
4648 int32_t var_id);
4649
4650#endif // FLECS_QUERY_PLANS
4651
4652/** Create a query iterator.
4653 * Use an iterator to iterate through the entities that match a query. Queries
4654 * can return multiple results, and have to be iterated by repeatedly calling
4655 * ecs_query_next() until the operation returns false.
4656 *
4657 * Depending on the query, a single result can contain an entire table, a range
4658 * of entities in a table, or a single entity. Iteration code has an inner and
4659 * an outer loop. The outer loop loops through the query results, and typically
4660 * corresponds with a table. The inner loop iterates entities in the result.
4661 *
4662 * Example:
4663 * @code
4664 * ecs_iter_t it = ecs_query_iter(world, q);
4665 *
4666 * while (ecs_query_next(&it)) {
4667 * Position *p = ecs_field(&it, Position, 0);
4668 * Velocity *v = ecs_field(&it, Velocity, 1);
4669 *
4670 * for (int i = 0; i < it.count; i ++) {
4671 * p[i].x += v[i].x;
4672 * p[i].y += v[i].y;
4673 * }
4674 * }
4675 * @endcode
4676 *
4677 * The world passed into the operation must be either the actual world or the
4678 * current stage, when iterating from a system. The stage is accessible through
4679 * the it.world member.
4680 *
4681 * Example:
4682 * @code
4683 * void MySystem(ecs_iter_t *it) {
4684 * ecs_query_t *q = it->ctx; // Query passed as system context
4685 *
4686 * // Create query iterator from system stage
4687 * ecs_iter_t qit = ecs_query_iter(it->world, q);
4688 * while (ecs_query_next(&qit)) {
4689 * // Iterate as usual
4690 * }
4691 * }
4692 * @endcode
4693 *
4694 * If query iteration is stopped without the last call to ecs_query_next()
4695 * returning false, iterator resources need to be cleaned up explicitly
4696 * with ecs_iter_fini().
4697 *
4698 * Example:
4699 * @code
4700 * ecs_iter_t it = ecs_query_iter(world, q);
4701 *
4702 * while (ecs_query_next(&it)) {
4703 * if (!ecs_field_is_set(&it, 0)) {
4704 * ecs_iter_fini(&it); // Free iterator resources
4705 * break;
4706 * }
4707 *
4708 * for (int i = 0; i < it.count; i ++) {
4709 * // ...
4710 * }
4711 * }
4712 * @endcode
4713 *
4714 * @param world The world.
4715 * @param query The query.
4716 * @return An iterator.
4717 *
4718 * @see ecs_query_next()
4719 */
4720FLECS_API
4722 const ecs_world_t *world,
4723 const ecs_query_t *query);
4724
4725/** Progress a query iterator.
4726 *
4727 * @param it The iterator.
4728 * @return True if the iterator has more results, false if not.
4729 *
4730 * @see ecs_query_iter()
4731 */
4732FLECS_API
4734 ecs_iter_t *it);
4735
4736/** Match an entity with a query.
4737 * This operation matches an entity with a query and returns the result of the
4738 * match in the "it" out parameter. An application should free the iterator
4739 * resources with ecs_iter_fini() if this function returns true.
4740 *
4741 * Usage:
4742 * @code
4743 * ecs_iter_t it;
4744 * if (ecs_query_has(q, e, &it)) {
4745 * ecs_iter_fini(&it);
4746 * }
4747 * @endcode
4748 *
4749 * @param query The query.
4750 * @param entity The entity to match.
4751 * @param it The iterator with matched data.
4752 * @return True if entity matches the query, false if not.
4753 */
4754FLECS_API
4756 const ecs_query_t *query,
4757 ecs_entity_t entity,
4758 ecs_iter_t *it);
4759
4760/** Match a table with a query.
4761 * This operation matches a table with a query and returns the result of the
4762 * match in the "it" out parameter. An application should free the iterator
4763 * resources with ecs_iter_fini() if this function returns true.
4764 *
4765 * Usage:
4766 * @code
4767 * ecs_iter_t it;
4768 * if (ecs_query_has_table(q, t, &it)) {
4769 * ecs_iter_fini(&it);
4770 * }
4771 * @endcode
4772 *
4773 * @param query The query.
4774 * @param table The table to match.
4775 * @param it The iterator with matched data.
4776 * @return True if table matches the query, false if not.
4777 */
4778FLECS_API
4780 const ecs_query_t *query,
4781 ecs_table_t *table,
4782 ecs_iter_t *it);
4783
4784/** Match a range with a query.
4785 * This operation matches a range with a query and returns the result of the
4786 * match in the "it" out parameter. An application should free the iterator
4787 * resources with ecs_iter_fini() if this function returns true.
4788 *
4789 * The entire range must match the query for the operation to return true.
4790 *
4791 * Usage:
4792 * @code
4793 * ecs_table_range_t range = {
4794 * .table = table,
4795 * .offset = 1,
4796 * .count = 2
4797 * };
4798 *
4799 * ecs_iter_t it;
4800 * if (ecs_query_has_range(q, &range, &it)) {
4801 * ecs_iter_fini(&it);
4802 * }
4803 * @endcode
4804 *
4805 * @param query The query.
4806 * @param range The range to match.
4807 * @param it The iterator with matched data.
4808 * @return True if range matches the query, false if not.
4809 */
4810FLECS_API
4812 const ecs_query_t *query,
4813 ecs_table_range_t *range,
4814 ecs_iter_t *it);
4815
4816#ifdef FLECS_CACHED_QUERIES
4817
4818/** Return how often a match event happened for a cached query.
4819 * This operation can be used to determine whether the query cache has been
4820 * updated with new tables.
4821 *
4822 * @param query The query.
4823 * @return The number of match events that happened.
4824 */
4825FLECS_API
4827 const ecs_query_t *query);
4828
4829/** Event emitted when a table needs to be revalidated for a query cache.
4830 * The event is enqueued when an observer detects that the components that are
4831 * matched through relationship traversal changed for a table, and is handled
4832 * when the command queue is flushed. The event payload is of type
4833 * ecs_query_cache_revalidate_t. */
4834FLECS_API extern const ecs_entity_t EcsOnQueryCacheRevalidate;
4835
4836/** Payload for EcsOnQueryCacheRevalidate event. */
4838 ecs_entity_t query; /**< Query for which to revalidate table. */
4839 uint64_t table_id; /**< Id of table to revalidate. */
4841
4842#endif // FLECS_CACHED_QUERIES
4843
4844#ifdef FLECS_QUERY_PLANS
4845
4846/** Convert a query to a string.
4847 * This will convert the query program to a string, which can aid in debugging
4848 * the behavior of a query.
4849 *
4850 * The returned string must be freed with ecs_os_free().
4851 *
4852 * @param query The query.
4853 * @return The query plan.
4854 */
4855FLECS_API
4857 const ecs_query_t *query);
4858
4859/** Convert a query to a string with a profile.
4860 * To use this, you must set the EcsIterProfile flag on an iterator before
4861 * starting iteration:
4862 *
4863 * @code
4864 * it.flags |= EcsIterProfile;
4865 * @endcode
4866 *
4867 * The returned string must be freed with ecs_os_free().
4868 *
4869 * @param query The query.
4870 * @param it The iterator with profile data.
4871 * @return The query plan with profile data.
4872 */
4873FLECS_API
4875 const ecs_query_t *query,
4876 const ecs_iter_t *it);
4877
4878/** Same as ecs_query_plan(), but includes the plan for populating the cache (if any).
4879 *
4880 * @param query The query.
4881 * @return The query plan.
4882 */
4883FLECS_API
4885 const ecs_query_t *query);
4886
4887/** Populate variables from a key-value string.
4888 * Convenience function to set query variables from a key-value string separated
4889 * by commas. The string must have the following format:
4890 *
4891 * @code
4892 * var_a: value, var_b: value
4893 * @endcode
4894 *
4895 * The key-value list may optionally be enclosed in parentheses.
4896 *
4897 * This function uses the script addon.
4898 *
4899 * @param query The query.
4900 * @param it The iterator for which to set the variables.
4901 * @param expr The key-value expression.
4902 * @return A pointer to the next character after the last parsed one.
4903 */
4904FLECS_API
4906 ecs_query_t *query,
4907 ecs_iter_t *it,
4908 const char *expr);
4909
4910#endif // FLECS_QUERY_PLANS
4911
4912#ifdef FLECS_CACHED_QUERIES
4913/** Return whether the query data changed since the last iteration.
4914 * The operation will return true after:
4915 * - new entities have been matched
4916 * - new tables have been matched or unmatched
4917 * - matched entities were deleted
4918 * - matched components were changed
4919 *
4920 * The operation will not return true after a write-only (EcsOut) or filter
4921 * (EcsInOutFilter) term has changed, when a term is not matched with the
4922 * current table ($this source) or for tag terms.
4923 *
4924 * The changed state of a table is reset after it is iterated. If an iterator was
4925 * not iterated until completion, tables may still be marked as changed.
4926 *
4927 * To check the changed state of the current iterator result, use
4928 * ecs_iter_changed().
4929 *
4930 * @param query The query.
4931 * @return True if entities changed, otherwise false.
4932 *
4933 * @see ecs_iter_changed()
4934 */
4935FLECS_API
4937 ecs_query_t *query);
4938#endif
4939
4940/** Get the query object.
4941 * Return the query object. Can be used to access various information about
4942 * the query.
4943 *
4944 * @param world The world.
4945 * @param query The query.
4946 * @return The query object.
4947 */
4948FLECS_API
4950 const ecs_world_t *world,
4951 ecs_entity_t query);
4952
4953#ifdef FLECS_CACHED_QUERIES
4954/** Skip a table while iterating.
4955 * This operation lets the query iterator know that a table was skipped while
4956 * iterating. A skipped table will not reset its changed state, and the query
4957 * will not update the dirty flags of the table for its out fields.
4958 *
4959 * Only valid iterators must be provided (next() has to be called at least once
4960 * and must return true), and the iterator must be a query iterator.
4961 *
4962 * @param it The iterator result to skip.
4963 */
4964FLECS_API
4966 ecs_iter_t *it);
4967
4968/** Set the group to iterate for a query iterator.
4969 * This operation limits the results returned by the query to only the selected
4970 * group ID. The query must have a group_by function, and the iterator must
4971 * be a query iterator.
4972 *
4973 * Groups are sets of tables that are stored together in the query cache based
4974 * on a group ID, which is calculated per table by the group_by function. To
4975 * iterate a group, an iterator only needs to know the first and last cache node
4976 * for that group, which can both be found in a fast O(1) operation.
4977 *
4978 * As a result, group iteration is one of the most efficient mechanisms to
4979 * filter out large numbers of entities, even if those entities are distributed
4980 * across many tables. This makes it a good fit for things like dividing up
4981 * a world into cells, and only iterating cells close to a player.
4982 *
4983 * The group to iterate must be set before the first call to ecs_query_next(). No
4984 * operations that can add or remove components should be invoked between calling
4985 * ecs_iter_set_group() and ecs_query_next().
4986 *
4987 * @param it The query iterator.
4988 * @param group_id The group to iterate.
4989 */
4990FLECS_API
4992 ecs_iter_t *it,
4993 uint64_t group_id);
4994
4995/** Return the map with query groups.
4996 * This map can be used to iterate the active group identifiers of a query. The
4997 * payload of the map is opaque. The map can be used as follows:
4998 *
4999 * @code
5000 * const ecs_map_t *keys = ecs_query_get_groups(q);
5001 * ecs_map_iter_t kit = ecs_map_iter(keys);
5002 * while (ecs_map_next(&kit)) {
5003 * uint64_t group_id = ecs_map_key(&kit);
5004 *
5005 * // Iterate query for group
5006 * ecs_iter_t it = ecs_query_iter(world, q);
5007 * ecs_iter_set_group(&it, group_id);
5008 * while (ecs_query_next(&it)) {
5009 * // Iterate as usual
5010 * }
5011 * }
5012 * @endcode
5013 *
5014 * This operation is not valid for queries that do not use group_by. The
5015 * returned map pointer will remain valid for as long as the query exists.
5016 *
5017 * @param query The query.
5018 * @return The map with query groups.
5019 */
5020FLECS_API
5021const ecs_map_t* ecs_query_get_groups(
5022 const ecs_query_t *query);
5023
5024/** Get the context of a query group.
5025 * This operation returns the context of a query group as returned by the
5026 * on_group_create callback.
5027 *
5028 * @param query The query.
5029 * @param group_id The group for which to obtain the context.
5030 * @return The group context, NULL if the group doesn't exist.
5031 */
5032FLECS_API
5034 const ecs_query_t *query,
5035 uint64_t group_id);
5036
5037/** Get information about a query group.
5038 * This operation returns information about a query group, including the group
5039 * context returned by the on_group_create callback.
5040 *
5041 * @param query The query.
5042 * @param group_id The group for which to obtain the group info.
5043 * @return The group info, NULL if the group doesn't exist.
5044 */
5045FLECS_API
5047 const ecs_query_t *query,
5048 uint64_t group_id);
5049
5050#endif // FLECS_CACHED_QUERIES
5051
5052/** Struct returned by ecs_query_count(). */
5053typedef struct ecs_query_count_t {
5054 int32_t results; /**< Number of results returned by the query. */
5055 int32_t entities; /**< Number of entities returned by the query. */
5056 int32_t tables; /**< Number of tables returned by the query. Only set for
5057 * queries for which the table count can be reliably
5058 * determined. */
5060
5061/** Return the number of entities and results the query matches with.
5062 * Only entities matching the $this variable as source are counted.
5063 *
5064 * @param query The query.
5065 * @return The number of matched entities.
5066 */
5067FLECS_API
5069 const ecs_query_t *query);
5070
5071/** Test whether a query returns one or more results.
5072 *
5073 * @param query The query.
5074 * @return True if query matches anything, false if not.
5075 */
5076FLECS_API
5078 const ecs_query_t *query);
5079
5080#ifdef FLECS_CACHED_QUERIES
5081
5082/** Get the query used to populate the cache.
5083 * This operation returns the query that is used to populate the query cache.
5084 * For queries that can be entirely cached, the returned query will be
5085 * equivalent to the query passed to ecs_query_init().
5086 *
5087 * @param query The query.
5088 * @return The query used to populate the cache, NULL if query is not cached.
5089 */
5090FLECS_API
5092 const ecs_query_t *query);
5093
5094#endif // FLECS_CACHED_QUERIES
5095
5096/** @} */
5097
5098/**
5099 * @defgroup observers Observers
5100 * Functions for working with events and observers.
5101 *
5102 * @{
5103 */
5104
5105/** Send an event.
5106 * This sends an event to matching observers and is the mechanism used by Flecs
5107 * itself to send `OnAdd`, `OnRemove`, etc. events.
5108 *
5109 * Applications can use this function to send custom events, where a custom
5110 * event can be any regular entity.
5111 *
5112 * Applications should not send built-in Flecs events, as this may violate
5113 * assumptions the code makes about the conditions under which those events are
5114 * sent.
5115 *
5116 * Observers are invoked synchronously. It is therefore safe to use stack-based
5117 * data as event context, which can be set in the "param" member.
5118 *
5119 * @param world The world.
5120 * @param desc The event parameters.
5121 *
5122 * @see ecs_enqueue()
5123 */
5124FLECS_API
5126 ecs_world_t *world,
5127 ecs_event_desc_t *desc);
5128
5129/** Enqueue an event.
5130 * Same as ecs_emit(), but enqueues an event in the command queue instead. The
5131 * event will be emitted when ecs_defer_end() is called.
5132 *
5133 * If this operation is called when the provided world is not in deferred mode,
5134 * it behaves just like ecs_emit().
5135 *
5136 * @param world The world.
5137 * @param desc The event parameters.
5138 */
5139FLECS_API
5141 ecs_world_t *world,
5142 ecs_event_desc_t *desc);
5143
5144/** Create an observer.
5145 * Observers can subscribe for one or more terms. An observer only triggers
5146 * when the source of the event meets all terms.
5147 *
5148 * If the descriptor specifies an existing entity, the entity must not already
5149 * be associated with an observer. To modify an existing observer, use
5150 * ecs_observer_update().
5151 *
5152 * See the documentation for ecs_observer_desc_t for more details.
5153 *
5154 * @param world The world.
5155 * @param desc The observer creation parameters.
5156 * @return The observer, or 0 if the operation failed.
5157 */
5158FLECS_API
5160 ecs_world_t *world,
5161 const ecs_observer_desc_t *desc);
5162
5163/** Update an existing observer.
5164 * Updates the configuration of an observer that was previously created with
5165 * ecs_observer_init(). Only fields in desc that are set to a non-default
5166 * value will be applied; fields left at their default value preserve the
5167 * existing configuration of the observer.
5168 *
5169 * The query and events fields of the descriptor are not used by this function;
5170 * the observer query and event subscriptions cannot be modified after
5171 * creation.
5172 *
5173 * @param world The world.
5174 * @param observer The observer to update.
5175 * @param desc The observer descriptor.
5176 * @return The observer entity, or 0 if the operation failed.
5177 */
5178FLECS_API
5180 ecs_world_t *world,
5181 ecs_entity_t observer,
5182 const ecs_observer_desc_t *desc);
5183
5184/** Get the observer object.
5185 * Return the observer object. Can be used to access various information about
5186 * the observer, like the query and context.
5187 *
5188 * @param world The world.
5189 * @param observer The observer.
5190 * @return The observer object.
5191 */
5192FLECS_API
5194 const ecs_world_t *world,
5195 ecs_entity_t observer);
5196
5197/** @} */
5198
5199/**
5200 * @defgroup iterator Iterators
5201 * Functions for working with `ecs_iter_t`.
5202 *
5203 * @{
5204 */
5205
5206/** Progress any iterator.
5207 * This operation is useful in combination with iterators for which it is not
5208 * known what created them. Example use cases are functions that should accept
5209 * any kind of iterator (such as serializers) or iterators created from poly
5210 * objects.
5211 *
5212 * This operation is slightly slower than using a type-specific iterator (e.g.,
5213 * ecs_query_next(), ecs_each_next()), as it has to call a function pointer, which
5214 * introduces a level of indirection.
5215 *
5216 * @param it The iterator.
5217 * @return True if iterator has more results, false if not.
5218 */
5219FLECS_API
5221 ecs_iter_t *it);
5222
5223/** Clean up iterator resources.
5224 * This operation cleans up any resources associated with the iterator.
5225 *
5226 * This operation should only be used when an iterator is not iterated until
5227 * completion (next() has not yet returned false). When an iterator is iterated
5228 * until completion, resources are automatically freed.
5229 *
5230 * @param it The iterator.
5231 */
5232FLECS_API
5234 ecs_iter_t *it);
5235
5236/** Count the number of matched entities in a query.
5237 * This operation returns the number of matched entities. If a query contains no
5238 * matched entities but still yields results (e.g., it has no terms with $this
5239 * sources), the operation will return 0.
5240 *
5241 * To determine the number of matched entities, the operation iterates the
5242 * iterator until it yields no more results.
5243 *
5244 * @param it The iterator.
5245 * @return The number of matched entities.
5246 */
5247FLECS_API
5249 ecs_iter_t *it);
5250
5251/** Test if an iterator is true.
5252 * This operation will return true if the iterator returns at least one result.
5253 * This is especially useful in combination with fact-checking queries (see the
5254 * queries addon).
5255 *
5256 * The operation requires a valid iterator. After the operation is invoked, the
5257 * application should no longer invoke next() on the iterator and should treat it
5258 * as if the iterator is iterated until completion.
5259 *
5260 * @param it The iterator.
5261 * @return True if the iterator returns at least one result.
5262 */
5263FLECS_API
5265 ecs_iter_t *it);
5266
5267/** Get the first matching entity from an iterator.
5268 * After this operation, the application should treat the iterator as if it has
5269 * been iterated until completion.
5270 *
5271 * @param it The iterator.
5272 * @return The first matching entity, or 0 if no entities were matched.
5273 */
5274FLECS_API
5276 ecs_iter_t *it);
5277
5278/** Set the value for an iterator variable.
5279 * This constrains the iterator to return only results for which the variable
5280 * equals the specified value. The default value for all variables is
5281 * EcsWildcard, which means the variable can assume any value.
5282 *
5283 * Example:
5284 *
5285 * @code
5286 * // Query that matches Position
5287 * ecs_query_t *q = ecs_query(world, {
5288 * .terms = {{ ecs_id(Position) }}
5289 * });
5290 *
5291 * // Constrain $this so the query only matches entity e
5292 * ecs_iter_t it = ecs_query_iter(world, q);
5293 * ecs_iter_set_var(&it, 0, e);
5294 *
5295 * while (ecs_query_next(&it)) {
5296 * for (int i = 0; i < it.count; i ++) {
5297 * // iterate as usual
5298 * }
5299 * }
5300 * @endcode
5301 *
5302 * The variable must be initialized after creating the iterator and before the
5303 * first call to next().
5304 * Without FLECS_QUERY_PLANS, only variable 0 ($this) can be set.
5305 *
5306 * @param it The iterator.
5307 * @param var_id The variable index.
5308 * @param entity The entity variable value.
5309 *
5310 * @see ecs_iter_set_var_as_range()
5311 * @see ecs_iter_set_var_as_table()
5312 */
5313FLECS_API
5315 ecs_iter_t *it,
5316 int32_t var_id,
5317 ecs_entity_t entity);
5318
5319/** Same as ecs_iter_set_var(), but for a table.
5320 * This constrains the variable to all entities in a table.
5321 *
5322 * @param it The iterator.
5323 * @param var_id The variable index.
5324 * @param table The table variable value.
5325 *
5326 * @see ecs_iter_set_var()
5327 * @see ecs_iter_set_var_as_range()
5328 */
5329FLECS_API
5331 ecs_iter_t *it,
5332 int32_t var_id,
5333 const ecs_table_t *table);
5334
5335/** Same as ecs_iter_set_var(), but for a range of entities.
5336 * This constrains the variable to a range of entities in a table.
5337 *
5338 * @param it The iterator.
5339 * @param var_id The variable index.
5340 * @param range The range variable value.
5341 *
5342 * @see ecs_iter_set_var()
5343 * @see ecs_iter_set_var_as_table()
5344 */
5345FLECS_API
5347 ecs_iter_t *it,
5348 int32_t var_id,
5349 const ecs_table_range_t *range);
5350
5351#ifdef FLECS_QUERY_PLANS
5352
5353/** Get the value of an iterator variable as an entity.
5354 * A variable can be interpreted as an entity if it is set to an entity, or if it
5355 * is set to a table range with count 1.
5356 *
5357 * This operation can only be invoked on valid iterators. The variable index
5358 * must be smaller than the total number of variables provided by the iterator
5359 * (as returned by ecs_iter_get_var_count()).
5360 *
5361 * @param it The iterator.
5362 * @param var_id The variable index.
5363 * @return The variable value.
5364 */
5365FLECS_API
5367 ecs_iter_t *it,
5368 int32_t var_id);
5369
5370/** Get the variable name.
5371 *
5372 * @param it The iterator.
5373 * @param var_id The variable index.
5374 * @return The variable name.
5375 */
5376FLECS_API
5378 const ecs_iter_t *it,
5379 int32_t var_id);
5380
5381/** Get the number of variables.
5382 *
5383 * @param it The iterator.
5384 * @return The number of variables.
5385 */
5386FLECS_API
5388 const ecs_iter_t *it);
5389
5390/** Get the variable array.
5391 *
5392 * @param it The iterator.
5393 * @return The variable array (if any).
5394 */
5395FLECS_API
5397 const ecs_iter_t *it);
5398
5399/** Get the value of an iterator variable as a table.
5400 * A variable can be interpreted as a table if it is set as a table range with
5401 * both offset and count set to 0, or if offset is 0 and count matches the
5402 * number of elements in the table.
5403 *
5404 * This operation can only be invoked on valid iterators. The variable index
5405 * must be smaller than the total number of variables provided by the iterator
5406 * (as returned by ecs_iter_get_var_count()).
5407 *
5408 * @param it The iterator.
5409 * @param var_id The variable index.
5410 * @return The variable value.
5411 */
5412FLECS_API
5414 ecs_iter_t *it,
5415 int32_t var_id);
5416
5417/** Get the value of an iterator variable as a table range.
5418 * A value can be interpreted as a table range if it is set as a table range, or if
5419 * it is set to an entity with a non-empty type (the entity must have at least
5420 * one component, tag, or relationship in its type).
5421 *
5422 * This operation can only be invoked on valid iterators. The variable index
5423 * must be smaller than the total number of variables provided by the iterator
5424 * (as returned by ecs_iter_get_var_count()).
5425 *
5426 * @param it The iterator.
5427 * @param var_id The variable index.
5428 * @return The variable value.
5429 */
5430FLECS_API
5432 ecs_iter_t *it,
5433 int32_t var_id);
5434
5435/** Return whether a variable is constrained.
5436 * This operation returns true for variables set by one of the ecs_iter_set_var*
5437 * operations.
5438 *
5439 * A constrained variable is guaranteed not to change values while results are
5440 * being iterated.
5441 *
5442 * @param it The iterator.
5443 * @param var_id The variable index.
5444 * @return Whether the variable is constrained to a specified value.
5445 */
5446FLECS_API
5448 ecs_iter_t *it,
5449 int32_t var_id);
5450
5451#endif // FLECS_QUERY_PLANS
5452
5453#ifdef FLECS_CACHED_QUERIES
5454
5455/** Return the group ID for the currently iterated result.
5456 * This operation returns the group ID for queries that use group_by. If this
5457 * operation is called on an iterator that is not iterating a query that uses
5458 * group_by, it will fail.
5459 *
5460 * For queries that use cascade, this operation will return the hierarchy depth
5461 * of the currently iterated result.
5462 *
5463 * @param it The iterator.
5464 * @return The group ID of the currently iterated result.
5465 */
5466FLECS_API
5468 const ecs_iter_t *it);
5469
5470/** Return whether the current iterator result has changed.
5471 * This operation must be used in combination with a query that supports change
5472 * detection (e.g., is cached). The operation returns whether the currently
5473 * iterated result has changed since the last time it was iterated by the query.
5474 *
5475 * Change detection works on a per-table basis. Changes to individual entities
5476 * cannot be detected this way.
5477 *
5478 * @param it The iterator.
5479 * @return True if the result changed, false if it didn't.
5480 */
5481FLECS_API
5483 ecs_iter_t *it);
5484#endif
5485
5486/** Create a paged iterator.
5487 * Paged iterators limit the results to those starting from 'offset', and will
5488 * return at most 'limit' results.
5489 *
5490 * The iterator must be iterated with ecs_page_next().
5491 *
5492 * A paged iterator acts as a passthrough for data exposed by the parent
5493 * iterator, so that any data provided by the parent will also be provided by
5494 * the paged iterator.
5495 *
5496 * @param it The source iterator.
5497 * @param offset The number of entities to skip.
5498 * @param limit The maximum number of entities to iterate.
5499 * @return A page iterator.
5500 */
5501FLECS_API
5503 const ecs_iter_t *it,
5504 int32_t offset,
5505 int32_t limit);
5506
5507/** Progress a paged iterator.
5508 * Progress an iterator created by ecs_page_iter().
5509 *
5510 * @param it The iterator.
5511 * @return True if the iterator has more results, false if not.
5512 */
5513FLECS_API
5515 ecs_iter_t *it);
5516
5517/** Create a worker iterator.
5518 * Worker iterators can be used to equally divide the number of matched entities
5519 * across N resources (usually threads). Each resource will process the total
5520 * number of matched entities divided by 'count'.
5521 *
5522 * Entities are distributed across resources such that the distribution is
5523 * stable between queries. Two queries that match the same table are guaranteed
5524 * to match the same entities in that table.
5525 *
5526 * The iterator must be iterated with ecs_worker_next().
5527 *
5528 * A worker iterator acts as a passthrough for data exposed by the parent
5529 * iterator, so that any data provided by the parent will also be provided by
5530 * the worker iterator.
5531 *
5532 * @param it The source iterator.
5533 * @param index The index of the current resource.
5534 * @param count The total number of resources to divide entities between.
5535 * @return A worker iterator.
5536 */
5537FLECS_API
5539 const ecs_iter_t *it,
5540 int32_t index,
5541 int32_t count);
5542
5543/** Progress a worker iterator.
5544 * Progress an iterator created by ecs_worker_iter().
5545 *
5546 * @param it The iterator.
5547 * @return True if the iterator has more results, false if not.
5548 */
5549FLECS_API
5551 ecs_iter_t *it);
5552
5553/** Get data for a field.
5554 * This operation retrieves a pointer to an array of data that belongs to the
5555 * term in the query. The index refers to the location of the term in the query,
5556 * and starts counting from zero.
5557 *
5558 * For example, the query `"Position, Velocity"` will return the `Position` array
5559 * for index 0, and the `Velocity` array for index 1.
5560 *
5561 * When the specified field is not owned by the entity, this function returns a
5562 * pointer instead of an array. This happens when the source of a field is not
5563 * the entity being iterated, such as a shared component (from a prefab), a
5564 * component from a parent, or another entity. The ecs_field_is_self() operation
5565 * can be used to test dynamically if a field is owned.
5566 *
5567 * When a field contains a sparse component, use the ecs_field_at() function. When
5568 * a field is guaranteed to be set and owned, the ecs_field_self() function can be
5569 * used. ecs_field_self() has slightly better performance, and provides stricter
5570 * validity checking.
5571 *
5572 * The provided size must be either 0 or must match the size of the type
5573 * of the returned array. If the size does not match, the operation may assert.
5574 * The size can be dynamically obtained with ecs_field_size().
5575 *
5576 * An example:
5577 *
5578 * @code
5579 * while (ecs_query_next(&it)) {
5580 * Position *p = ecs_field(&it, Position, 0);
5581 * Velocity *v = ecs_field(&it, Velocity, 1);
5582 * for (int32_t i = 0; i < it.count; i ++) {
5583 * p[i].x += v[i].x;
5584 * p[i].y += v[i].y;
5585 * }
5586 * }
5587 * @endcode
5588 *
5589 * @param it The iterator.
5590 * @param size The size of the field type.
5591 * @param index The index of the field.
5592 * @return A pointer to the data of the field.
5593 */
5594FLECS_API
5596 const ecs_iter_t *it,
5597 size_t size,
5598 int8_t index);
5599
5600/** Get data for a field at a specified row.
5601 * This operation should be used instead of ecs_field_w_size() for sparse
5602 * component fields. This operation should be called for each returned row in a
5603 * result. In the following example, the Velocity component is sparse:
5604 *
5605 * @code
5606 * while (ecs_query_next(&it)) {
5607 * Position *p = ecs_field(&it, Position, 0);
5608 * for (int32_t i = 0; i < it.count; i ++) {
5609 * Velocity *v = ecs_field_at(&it, Velocity, 1, i);
5610 * p[i].x += v->x;
5611 * p[i].y += v->y;
5612 * }
5613 * }
5614 * @endcode
5615 *
5616 * @param it The iterator.
5617 * @param size The size of the field type.
5618 * @param index The index of the field.
5619 * @param row The row to get data for.
5620 * @return A pointer to the data of the field.
5621 */
5622FLECS_API
5624 const ecs_iter_t *it,
5625 size_t size,
5626 int8_t index,
5627 int32_t row);
5628
5629/** Test whether the field is read-only.
5630 * This operation returns whether the field is read-only. Read-only fields are
5631 * annotated with [in], or are added as a const type in the C++ API.
5632 *
5633 * @param it The iterator.
5634 * @param index The index of the field in the iterator.
5635 * @return Whether the field is read-only.
5636 */
5637FLECS_API
5639 const ecs_iter_t *it,
5640 int8_t index);
5641
5642/** Test whether the field is write-only.
5643 * This operation returns whether this is a write-only field. Write-only terms are
5644 * annotated with [out].
5645 *
5646 * Serializers are not required to serialize the values of a write-only field.
5647 *
5648 * @param it The iterator.
5649 * @param index The index of the field in the iterator.
5650 * @return Whether the field is write-only.
5651 */
5652FLECS_API
5654 const ecs_iter_t *it,
5655 int8_t index);
5656
5657/** Test whether a field is set.
5658 *
5659 * @param it The iterator.
5660 * @param index The index of the field in the iterator.
5661 * @return Whether the field is set.
5662 */
5663FLECS_API
5665 const ecs_iter_t *it,
5666 int8_t index);
5667
5668/** Return the ID matched for a field.
5669 *
5670 * @param it The iterator.
5671 * @param index The index of the field in the iterator.
5672 * @return The ID matched for the field.
5673 */
5674FLECS_API
5676 const ecs_iter_t *it,
5677 int8_t index);
5678
5679/** Return the index of a matched table column.
5680 * This function only returns column indices for fields that have been matched
5681 * on the $this variable. Fields matched on other tables will return -1.
5682 *
5683 * @param it The iterator.
5684 * @param index The index of the field in the iterator.
5685 * @return The index of the matched column, -1 if not matched.
5686 */
5687FLECS_API
5689 const ecs_iter_t *it,
5690 int8_t index);
5691
5692/** Return the field source.
5693 * The field source is the entity on which the field was matched.
5694 *
5695 * @param it The iterator.
5696 * @param index The index of the field in the iterator.
5697 * @return The source for the field.
5698 */
5699FLECS_API
5701 const ecs_iter_t *it,
5702 int8_t index);
5703
5704/** Return the field type size.
5705 * Returns the type size of the field. Returns 0 if the field has no data.
5706 *
5707 * @param it The iterator.
5708 * @param index The index of the field in the iterator.
5709 * @return The type size for the field.
5710 */
5711FLECS_API
5713 const ecs_iter_t *it,
5714 int8_t index);
5715
5716/** Test whether the field is matched on self.
5717 * This operation returns whether the field is matched on the currently iterated
5718 * entity. This function will return false when the field is owned by another
5719 * entity, such as a parent or a prefab.
5720 *
5721 * When this operation returns false, the field must be accessed as a single
5722 * value instead of an array. Fields for which this operation returns true
5723 * return arrays with it->count values.
5724 *
5725 * @param it The iterator.
5726 * @param index The index of the field in the iterator.
5727 * @return Whether the field is matched on self.
5728 */
5729FLECS_API
5731 const ecs_iter_t *it,
5732 int8_t index);
5733
5734/** @} */
5735
5736/**
5737 * @defgroup tables Tables
5738 * Functions for working with `ecs_table_t`.
5739 *
5740 * @{
5741 */
5742
5743/** Get the type for a table.
5744 * The table type is a vector that contains all component, tag, and pair IDs.
5745 *
5746 * @param table The table.
5747 * @return The type of the table.
5748 */
5749FLECS_API
5751 const ecs_table_t *table);
5752
5753/** Get the type index for a component.
5754 * This operation returns the index for a component in the table's type.
5755 *
5756 * @param world The world.
5757 * @param table The table.
5758 * @param component The component.
5759 * @return The index of the component in the table type, or -1 if not found.
5760 *
5761 * @see ecs_table_has_id()
5762 */
5763FLECS_API
5765 const ecs_world_t *world,
5766 const ecs_table_t *table,
5767 ecs_id_t component);
5768
5769/** Get the column index for a component.
5770 * This operation returns the column index for a component in the table's type.
5771 * If the component doesn't have data (it is a tag), the function will return -1.
5772 *
5773 * @param world The world.
5774 * @param table The table.
5775 * @param component The component.
5776 * @return The column index of the component ID, or -1 if not found or not a component.
5777 */
5778FLECS_API
5780 const ecs_world_t *world,
5781 const ecs_table_t *table,
5782 ecs_id_t component);
5783
5784/** Return the number of columns in a table.
5785 * Similar to `ecs_table_get_type(table)->count`, except that the column count
5786 * only counts the number of components in a table.
5787 *
5788 * @param table The table.
5789 * @return The number of columns in the table.
5790 */
5791FLECS_API
5793 const ecs_table_t *table);
5794
5795/** Convert a type index to a column index.
5796 * Tables have an array of columns for each component in the table. This array
5797 * does not include elements for tags, which means that the index for a
5798 * component in the table type is not necessarily the same as the index in the
5799 * column array. This operation converts from an index in the table type to an
5800 * index in the column array.
5801 *
5802 * @param table The table.
5803 * @param index The index in the table type.
5804 * @return The index in the table column array.
5805 *
5806 * @see ecs_table_column_to_type_index()
5807 */
5808FLECS_API
5810 const ecs_table_t *table,
5811 int32_t index);
5812
5813/** Convert a column index to a type index.
5814 * Same as ecs_table_type_to_column_index(), but converts from an index in the
5815 * column array to an index in the table type.
5816 *
5817 * @param table The table.
5818 * @param index The column index.
5819 * @return The index in the table type.
5820 */
5821FLECS_API
5823 const ecs_table_t *table,
5824 int32_t index);
5825
5826/** Get a column from a table by column index.
5827 * This operation returns the component array for the provided index.
5828 *
5829 * @param table The table.
5830 * @param index The column index.
5831 * @param offset The index of the first row to return (0 for entire column).
5832 * @return The component array, or NULL if the index is not a component.
5833 */
5834FLECS_API
5836 const ecs_table_t *table,
5837 int32_t index,
5838 int32_t offset);
5839
5840/** Get a column from a table by component.
5841 * This operation returns the component array for the provided component.
5842 *
5843 * @param world The world.
5844 * @param table The table.
5845 * @param component The component for the column.
5846 * @param offset The index of the first row to return (0 for entire column).
5847 * @return The component array, or NULL if the component is not found.
5848 */
5849FLECS_API
5851 const ecs_world_t *world,
5852 const ecs_table_t *table,
5853 ecs_id_t component,
5854 int32_t offset);
5855
5856/** Get the column size from a table.
5857 * This operation returns the component size for the provided index.
5858 *
5859 * @param table The table.
5860 * @param index The column index.
5861 * @return The component size, or 0 if the index is not a component.
5862 */
5863FLECS_API
5865 const ecs_table_t *table,
5866 int32_t index);
5867
5868/** Return the number of entities in the table.
5869 * This operation returns the number of entities in the table.
5870 *
5871 * @param table The table.
5872 * @return The number of entities in the table.
5873 */
5874FLECS_API
5876 const ecs_table_t *table);
5877
5878/** Return the allocated size of the table.
5879 * This operation returns the number of elements allocated in the table
5880 * per column.
5881 *
5882 * @param table The table.
5883 * @return The number of allocated elements in the table.
5884 */
5885FLECS_API
5887 const ecs_table_t *table);
5888
5889/** Return the array with entity IDs for the table.
5890 * The size of the returned array is the result of ecs_table_count().
5891 *
5892 * @param table The table.
5893 * @return The array with entity IDs for the table.
5894 */
5895FLECS_API
5897 const ecs_table_t *table);
5898
5899/** Test if a table has a component.
5900 * Same as `ecs_table_get_type_index(world, table, component) != -1`.
5901 *
5902 * @param world The world.
5903 * @param table The table.
5904 * @param component The component.
5905 * @return True if the table has the component ID, false if the table doesn't.
5906 *
5907 * @see ecs_table_get_type_index()
5908 */
5909FLECS_API
5911 const ecs_world_t *world,
5912 const ecs_table_t *table,
5913 ecs_id_t component);
5914
5915/** Get the relationship target for a table.
5916 *
5917 * @param world The world.
5918 * @param table The table.
5919 * @param relationship The relationship for which to obtain the target.
5920 * @param index The index, in case the table has multiple instances of the relationship.
5921 * @return The requested relationship target.
5922 *
5923 * @see ecs_get_target()
5924 */
5925FLECS_API
5927 const ecs_world_t *world,
5928 const ecs_table_t *table,
5929 ecs_entity_t relationship,
5930 int32_t index);
5931
5932/** Return the depth for a table in the tree for the specified relationship.
5933 * Depth is determined by counting the number of targets encountered while
5934 * traversing up the relationship tree. Only acyclic relationships are
5935 * supported.
5936 *
5937 * @param world The world.
5938 * @param table The table.
5939 * @param rel The relationship.
5940 * @return The depth of the table in the tree.
5941 */
5942FLECS_API
5944 const ecs_world_t *world,
5945 const ecs_table_t *table,
5946 ecs_entity_t rel);
5947
5948/** Get the table that has all components of the current table plus the specified ID.
5949 * If the provided table already has the provided ID, the operation will return
5950 * the provided table.
5951 *
5952 * @param world The world.
5953 * @param table The table.
5954 * @param component The component to add.
5955 * @return The resulting table.
5956 */
5957FLECS_API
5959 ecs_world_t *world,
5960 ecs_table_t *table,
5961 ecs_id_t component);
5962
5963/** Find a table from an ID array.
5964 * This operation finds or creates a table with the specified array of
5965 * (component) IDs. The IDs in the array must be sorted, and it may not contain
5966 * duplicate elements.
5967 *
5968 * @param world The world.
5969 * @param ids The ID array.
5970 * @param id_count The number of elements in the ID array.
5971 * @return The table with the specified (component) IDs.
5972 */
5973FLECS_API
5975 ecs_world_t *world,
5976 const ecs_id_t *ids,
5977 int32_t id_count);
5978
5979/** Get the table that has all components of the current table minus the specified component.
5980 * If the provided table doesn't have the provided component, the operation will
5981 * return the provided table.
5982 *
5983 * @param world The world.
5984 * @param table The table.
5985 * @param component The component to remove.
5986 * @return The resulting table.
5987 */
5988FLECS_API
5990 ecs_world_t *world,
5991 ecs_table_t *table,
5992 ecs_id_t component);
5993
5994/** Lock a table.
5995 * When a table is locked, modifications to it will throw an assert. When the
5996 * table is locked recursively, it will take an equal amount of unlock
5997 * operations to actually unlock the table.
5998 *
5999 * Table locks can be used to build safe iterators where it is guaranteed that
6000 * the contents of a table are not modified while it is being iterated.
6001 *
6002 * The operation only works when called on the world, and has no side effects
6003 * when called on a stage. The assumption is that when called on a stage,
6004 * operations are deferred already.
6005 *
6006 * @param world The world.
6007 * @param table The table to lock.
6008 */
6009FLECS_API
6011 ecs_world_t *world,
6012 ecs_table_t *table);
6013
6014/** Unlock a table.
6015 * Must be called after calling ecs_table_lock().
6016 *
6017 * @param world The world.
6018 * @param table The table to unlock.
6019 */
6020FLECS_API
6022 ecs_world_t *world,
6023 ecs_table_t *table);
6024
6025/** Test a table for flags.
6026 * Test if a table has all of the provided flags. See
6027 * include/flecs/private/api_flags.h for a list of table flags that can be used
6028 * with this function.
6029 *
6030 * @param table The table.
6031 * @param flags The flags to test for.
6032 * @return Whether the specified flags are set for the table.
6033 */
6034FLECS_API
6036 ecs_table_t *table,
6037 ecs_flags32_t flags);
6038
6039/** Check if a table has traversable entities.
6040 * Traversable entities are entities that are used as a target in a pair with a
6041 * relationship that has the Traversable trait.
6042 *
6043 * @param table The table.
6044 * @return Whether the table has traversable entities.
6045 */
6046FLECS_API
6048 const ecs_table_t *table);
6049
6050/** Swap two elements inside the table.
6051 * This is useful for implementing custom
6052 * table sorting algorithms.
6053 *
6054 * @param world The world.
6055 * @param table The table to swap elements in.
6056 * @param row_1 The table element to swap with row_2.
6057 * @param row_2 The table element to swap with row_1.
6058 */
6059FLECS_API
6061 ecs_world_t* world,
6062 ecs_table_t* table,
6063 int32_t row_1,
6064 int32_t row_2);
6065
6066/** Search for a component in a table type.
6067 * This operation returns the index of the first occurrence of the component in the
6068 * table type. The component may be a pair or a wildcard.
6069 *
6070 * When component_out is provided, the function will assign it with the found
6071 * component. The found component may be different from the provided component
6072 * if it is a wildcard.
6073 *
6074 * This is a constant-time operation.
6075 *
6076 * @param world The world.
6077 * @param table The table.
6078 * @param component The component to search for.
6079 * @param component_out If provided, it will be set to the found component (optional).
6080 * @return The index of the ID in the table type.
6081 *
6082 * @see ecs_search_offset()
6083 * @see ecs_search_relation()
6084 */
6085FLECS_API
6087 const ecs_world_t *world,
6088 const ecs_table_t *table,
6089 ecs_id_t component,
6090 ecs_id_t *component_out);
6091
6092/** Search for a component in a table type starting from an offset.
6093 * This operation is the same as ecs_search(), but starts searching from an offset
6094 * in the table type.
6095 *
6096 * This operation is typically called in a loop where the resulting index is
6097 * used in the next iteration as offset:
6098 *
6099 * @code
6100 * int32_t index = -1;
6101 * while ((index = ecs_search_offset(world, table, index + 1, id, NULL)) != -1) {
6102 * // do stuff
6103 * }
6104 * @endcode
6105 *
6106 * Depending on how the operation is used, it is either linear or constant time.
6107 * When the ID has the form `(id)` or `(rel, *)` and the operation is invoked as
6108 * in the above example, it is guaranteed to be constant time.
6109 *
6110 * If the provided component has the form `(*, tgt)`, the operation takes linear
6111 * time. The reason for this is that IDs for a target are not packed together,
6112 * as they are sorted relationship-first.
6113 *
6114 * If the component at the offset does not match the provided ID, the operation
6115 * will do a linear search to find a matching ID.
6116 *
6117 * @param world The world.
6118 * @param table The table.
6119 * @param offset The offset from where to start searching.
6120 * @param component The component to search for.
6121 * @param component_out If provided, it will be set to the found component (optional).
6122 * @return The index of the ID in the table type.
6123 *
6124 * @see ecs_search()
6125 * @see ecs_search_relation()
6126 */
6127FLECS_API
6129 const ecs_world_t *world,
6130 const ecs_table_t *table,
6131 int32_t offset,
6132 ecs_id_t component,
6133 ecs_id_t *component_out);
6134
6135/** Search for a component or relationship ID in a table type starting from an offset.
6136 * This operation is the same as ecs_search_offset(), but has the additional
6137 * capability of traversing relationships to find a component. For example, if
6138 * an application wants to find a component for either the provided table or a
6139 * prefab (using the `IsA` relationship) of that table, it could use the operation
6140 * like this:
6141 *
6142 * @code
6143 * int32_t index = ecs_search_relation(
6144 * world, // the world
6145 * table, // the table
6146 * 0, // offset 0
6147 * ecs_id(Position), // the component ID
6148 * EcsIsA, // the relationship to traverse
6149 * EcsSelf|EcsUp, // search self and up
6150 * NULL, // (optional) entity on which component was found
6151 * NULL, // (optional) found component ID
6152 * NULL); // internal type with information about matched ID
6153 * @endcode
6154 *
6155 * The operation searches depth-first. If a table type has 2 `IsA` relationships, the
6156 * operation will first search the `IsA` tree of the first relationship.
6157 *
6158 * When choosing between ecs_search(), ecs_search_offset(), and ecs_search_relation(),
6159 * the simpler the function, the better its performance.
6160 *
6161 * @param world The world.
6162 * @param table The table.
6163 * @param offset The offset from where to start searching.
6164 * @param component The component to search for.
6165 * @param rel The relationship to traverse (optional).
6166 * @param flags Whether to search EcsSelf and/or EcsUp.
6167 * @param tgt_out If provided, it will be set to the matched entity.
6168 * @param component_out If provided, it will be set to the found component (optional).
6169 * @param tr_out The internal datatype.
6170 * @return The index of the component in the table type.
6171 *
6172 * @see ecs_search()
6173 * @see ecs_search_offset()
6174 */
6175FLECS_API
6177 const ecs_world_t *world,
6178 const ecs_table_t *table,
6179 int32_t offset,
6180 ecs_id_t component,
6181 ecs_entity_t rel,
6182 ecs_flags64_t flags, /* EcsSelf and/or EcsUp */
6183 ecs_entity_t *tgt_out,
6184 ecs_id_t *component_out,
6185 struct ecs_table_record_t **tr_out);
6186
6187/** Search for a component ID by following a relationship, starting from an entity.
6188 * This operation is the same as ecs_search_relation(), but starts the search
6189 * from an entity rather than a table.
6190 *
6191 * @param world The world.
6192 * @param entity The entity from which to begin the search.
6193 * @param id The component ID to search for.
6194 * @param rel The relationship to follow.
6195 * @param self If true, also search components on the entity itself.
6196 * @param cr Optional component record for the component ID.
6197 * @param tgt_out Out parameter for the target entity.
6198 * @param id_out Out parameter for the found component ID.
6199 * @param tr_out Out parameter for the table record.
6200 * @return The index of the component ID in the entity's type, or -1 if not found.
6201 */
6202FLECS_API
6204 const ecs_world_t *world,
6205 ecs_entity_t entity,
6206 ecs_id_t id,
6207 ecs_entity_t rel,
6208 bool self,
6210 ecs_entity_t *tgt_out,
6211 ecs_id_t *id_out,
6212 struct ecs_table_record_t **tr_out);
6213
6214/** Remove all entities in a table. Does not deallocate table memory.
6215 * Retaining table memory can be efficient when planning
6216 * to refill the table with operations like ecs_bulk_init().
6217 *
6218 * @param world The world.
6219 * @param table The table to clear.
6220 */
6221FLECS_API
6223 ecs_world_t* world,
6224 ecs_table_t* table);
6225
6226/** @} */
6227
6228/** @} */
6229
6230/**
6231 * @defgroup c_addons Addons
6232 * @ingroup c
6233 * C APIs for addons.
6234 *
6235 * @{
6236 * @}
6237 */
6238
6239#include "flecs/addons/flecs_c.h"
6240
6241#ifdef __cplusplus
6242}
6243#endif
6244
6245#include "flecs/private/addons.h"
6246
6247#endif
The deprecated addon contains deprecated operations.
Extends the core API with convenience macros for C applications.
void ecs_remove_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t component)
Remove a component from an entity.
void ecs_auto_override_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t component)
Add an auto override for a component.
void ecs_remove_all(ecs_world_t *world, ecs_id_t component)
Remove all instances of the specified component.
void ecs_clear(ecs_world_t *world, ecs_entity_t entity)
Clear all components.
void ecs_add_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t component)
Add a (component) ID to an entity.
const ecs_entity_t EcsScopeClose
Marker used to indicate the end of a scope (}) in queries.
const ecs_entity_t EcsOnRemove
Event that triggers when an ID is removed from an entity.
const ecs_entity_t EcsThis
This entity.
const ecs_entity_t EcsWildcard
Wildcard entity ("*").
const ecs_entity_t EcsName
Tag to indicate name identifier.
const ecs_entity_t EcsAlias
Tag to indicate alias identifier.
const ecs_entity_t EcsOnSet
Event that triggers when a component is set for an entity.
const ecs_entity_t EcsReflexive
Mark a relationship as reflexive.
const ecs_entity_t EcsEmpty
Tag used to indicate a query is empty.
const ecs_entity_t EcsOnTableDelete
Event that triggers when a table is deleted.
const ecs_entity_t EcsOnTableCreate
Event that triggers when a table is created.
const ecs_entity_t EcsObserver
Tag added to observers.
const ecs_entity_t EcsQuery
Tag added to queries.
const ecs_entity_t EcsOnStart
OnStart pipeline phase.
const ecs_entity_t EcsNotQueryable
Trait added to entities that should never be returned by queries.
const ecs_entity_t EcsOnStore
OnStore pipeline phase.
const ecs_entity_t EcsOrderedChildren
Tag that, when added to a parent, ensures stable order of ecs_children() results.
const ecs_entity_t EcsDontFragment
Mark component as non-fragmenting.
const ecs_entity_t EcsTraversable
Mark a relationship as traversable.
const ecs_entity_t EcsPredLookup
Marker used to indicate $var ~= "pattern" matching in queries.
const ecs_entity_t EcsPreStore
PreStore pipeline phase.
const ecs_entity_t EcsOnLoad
OnLoad pipeline phase.
const ecs_entity_t EcsIsA
Used to express inheritance relationships.
const ecs_entity_t EcsExclusive
Can be added to a relationship to indicate that the relationship can only occur once on an entity.
const ecs_entity_t EcsSymbol
Tag to indicate symbol identifier.
const ecs_entity_t EcsDependsOn
Used to express dependency relationships.
const ecs_entity_t EcsTransitive
Mark a relationship as transitive.
const ecs_entity_t EcsDelete
Delete cleanup policy.
const ecs_entity_t EcsChildOf
Used to express parent-child relationships.
const ecs_entity_t EcsFlecsCore
Core module scope.
const ecs_entity_t EcsMonitor
Event that triggers an observer when an entity starts or stops matching a query.
const ecs_entity_t EcsCanToggle
Mark a component as toggleable with ecs_enable_id().
const ecs_entity_t EcsPredEq
Marker used to indicate $var == ... matching in queries.
const ecs_entity_t EcsPhase
Phase pipeline phase.
const ecs_entity_t EcsWorld
Entity associated with world (used for "attaching" components to world).
const ecs_entity_t EcsScopeOpen
Marker used to indicate the start of a scope ({) in queries.
const ecs_entity_t EcsPostUpdate
PostUpdate pipeline phase.
const ecs_entity_t EcsOnValidate
OnValidate pipeline phase.
const ecs_entity_t EcsRemove
Remove cleanup policy.
const ecs_entity_t EcsOverride
Override component on instantiate.
const ecs_entity_t EcsPredMatch
Marker used to indicate $var == "name" matching in queries.
const ecs_entity_t EcsInherit
Inherit component on instantiate.
const ecs_entity_t EcsModule
Tag added to module entities.
const ecs_entity_t EcsSparse
Mark component as sparse.
const ecs_entity_t EcsPreUpdate
PreUpdate pipeline phase.
const ecs_entity_t EcsOnAdd
Event that triggers when an ID is added to an entity.
const ecs_entity_t EcsPrefab
Tag added to prefab entities.
const ecs_entity_t EcsOnInstantiate
Relationship that specifies component inheritance behavior.
const ecs_entity_t EcsPostFrame
PostFrame pipeline phase.
const ecs_entity_t EcsInheritable
Mark component as inheritable.
const ecs_entity_t EcsAny
Any entity ("_").
const ecs_entity_t EcsParentDepth
Relationship storing the entity's depth in a non-fragmenting hierarchy.
const ecs_entity_t EcsWith
Ensure that a component is always added together with another component.
const ecs_entity_t EcsPostLoad
PostLoad pipeline phase.
const ecs_entity_t EcsOnDelete
Relationship used for specifying cleanup behavior.
const ecs_entity_t EcsOnDeleteTarget
Relationship used to define what should happen when a target entity (second element of a pair) is del...
const ecs_entity_t EcsPreFrame
PreFrame pipeline phase.
const ecs_entity_t EcsFlecs
Root scope for built-in Flecs entities.
const ecs_entity_t EcsSystem
Tag added to systems.
const ecs_entity_t EcsOnUpdate
OnUpdate pipeline phase.
const ecs_entity_t EcsDisabled
When this tag is added to an entity, it is skipped by queries, unless EcsDisabled is explicitly queri...
const ecs_entity_t EcsDontInherit
Never inherit component on instantiate.
const ecs_entity_t EcsPairIsTag
Can be added to a relationship to indicate that it should never hold data, even when it or the relati...
const ecs_entity_t EcsPanic
Panic cleanup policy.
const ecs_entity_t EcsVariable
Variable entity ("$").
const ecs_entity_t EcsConstant
Tag added to enum or bitmask constants.
FLECS_API const ecs_entity_t ecs_id(EcsDocDescription)
Component ID for EcsDocDescription.
ecs_world_t * ecs_stage_new(ecs_world_t *world)
Create an unmanaged stage.
bool ecs_defer_end(ecs_world_t *world)
End a block of operations to defer.
bool ecs_readonly_begin(ecs_world_t *world, bool multi_threaded)
Begin readonly mode.
void ecs_defer_resume(ecs_world_t *world)
Resume deferring.
bool ecs_defer_begin(ecs_world_t *world)
Defer operations until the end of the frame.
void ecs_defer_suspend(ecs_world_t *world)
Suspend deferring but do not flush queue.
bool ecs_is_deferred(const ecs_world_t *world)
Test if deferring is enabled for the current stage.
void ecs_stage_free(ecs_world_t *stage)
Free an unmanaged stage.
void ecs_merge(ecs_world_t *stage)
Merge a stage.
int32_t ecs_stage_get_id(const ecs_world_t *world)
Get the stage ID.
bool ecs_stage_is_readonly(const ecs_world_t *world)
Test whether the current world is readonly.
int32_t ecs_get_stage_count(const ecs_world_t *world)
Get the number of configured stages.
ecs_world_t * ecs_get_stage(const ecs_world_t *world, int32_t stage_id)
Get stage-specific world pointer.
void ecs_set_stage_count(ecs_world_t *world, int32_t stages)
Configure the world to have N stages.
void ecs_readonly_end(ecs_world_t *world)
End readonly mode.
bool ecs_is_defer_suspended(const ecs_world_t *world)
Test if deferring is suspended for the current stage.
const ecs_type_hooks_t * ecs_get_hooks_id(const ecs_world_t *world, ecs_entity_t component)
Get hooks for a component.
ecs_entity_t ecs_component_init(ecs_world_t *world, const ecs_component_desc_t *desc)
Find or create a component.
const ecs_type_info_t * ecs_get_type_info(const ecs_world_t *world, ecs_id_t component)
Get the type info for a component.
void ecs_set_hooks_id(ecs_world_t *world, ecs_entity_t component, const ecs_type_hooks_t *hooks)
Register hooks for a component.
struct ecs_component_record_t ecs_component_record_t
Information about a (component) ID, such as type info and tables with the ID.
Definition flecs.h:507
struct ecs_stage_t ecs_stage_t
A stage enables modification while iterating and from multiple threads.
Definition flecs.h:442
struct ecs_ref_t ecs_ref_t
A ref is a fast way to fetch a component for a specific entity.
Definition flecs.h:491
ecs_id_t ecs_entity_t
An entity identifier.
Definition flecs.h:395
struct ecs_table_record_t ecs_table_record_t
Opaque type for table record.
Definition flecs.h:541
struct ecs_world_t ecs_world_t
A world is the container for all ECS data and supporting features.
Definition flecs.h:439
struct ecs_mixins_t ecs_mixins_t
Type that stores poly mixins.
Definition flecs.h:531
uint64_t ecs_id_t
IDs are the things that can be added to an entity.
Definition flecs.h:388
struct ecs_observable_t ecs_observable_t
An observable produces events that can be listened for by an observer.
Definition flecs.h:475
struct ecs_record_t ecs_record_t
Information about an entity, like its table and row.
Definition flecs.h:504
struct ecs_table_t ecs_table_t
A table stores entities and components for a specific type.
Definition flecs.h:445
void ecs_poly_t
A poly object.
Definition flecs.h:528
ecs_entity_t ecs_new_low_id(ecs_world_t *world)
Create new low ID.
void ecs_set_child_order(ecs_world_t *world, ecs_entity_t parent, const ecs_entity_t *children, int32_t child_count)
Set child order for parent with OrderedChildren.
const ecs_entity_t * ecs_bulk_init(ecs_world_t *world, const ecs_bulk_desc_t *desc)
Bulk create or populate new entities.
ecs_entity_t ecs_insert_w_values(ecs_world_t *world, const ecs_value_t *values)
Create a new entity with a list of component values.
ecs_entity_t ecs_clone(ecs_world_t *world, ecs_entity_t dst, ecs_entity_t src, bool copy_value)
Clone an entity.
ecs_entity_t ecs_new(ecs_world_t *world)
Create new entity ID.
ecs_entity_t ecs_entity_init(ecs_world_t *world, const ecs_entity_desc_t *desc)
Find or create an entity.
void ecs_delete(ecs_world_t *world, ecs_entity_t entity)
Delete an entity.
void ecs_delete_with(ecs_world_t *world, ecs_id_t component)
Delete all entities with the specified component.
ecs_entity_t ecs_new_w_id(ecs_world_t *world, ecs_id_t component)
Create new entity with (component) ID.
ecs_entity_t ecs_new_w_table(ecs_world_t *world, ecs_table_t *table)
Create new entity in table.
const ecs_entity_t * ecs_bulk_new_w_id(ecs_world_t *world, ecs_id_t component, int32_t count)
Create N new entities.
ecs_entities_t ecs_get_ordered_children(const ecs_world_t *world, ecs_entity_t parent)
Get ordered children.
bool ecs_children_next(ecs_iter_t *it)
Progress an iterator created with ecs_children().
ecs_iter_t ecs_each_id(const ecs_world_t *world, ecs_id_t component)
Iterate all entities with a specified (component ID).
bool ecs_each_next(ecs_iter_t *it)
Progress an iterator created with ecs_each_id().
ecs_iter_t ecs_children(const ecs_world_t *world, ecs_entity_t parent)
Iterate children of a parent.
ecs_iter_t ecs_children_w_rel(const ecs_world_t *world, ecs_entity_t relationship, ecs_entity_t parent)
Same as ecs_children(), but with a custom relationship argument.
void ecs_enable_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t component, bool enable)
Enable or disable a component.
bool ecs_is_enabled_id(const ecs_world_t *world, ecs_entity_t entity, ecs_id_t component)
Test if a component is enabled.
void ecs_enable(ecs_world_t *world, ecs_entity_t entity, bool enabled)
Enable or disable an entity.
char * ecs_entity_str(const ecs_world_t *world, ecs_entity_t entity)
Convert an entity to a string.
ecs_entity_t ecs_get_target(const ecs_world_t *world, ecs_entity_t entity, ecs_entity_t rel, int32_t index)
Get the target of a relationship.
ecs_entity_t ecs_get_parent(const ecs_world_t *world, ecs_entity_t entity)
Get the parent (target of the ChildOf relationship) for an entity.
ecs_entity_t ecs_get_target_for_id(const ecs_world_t *world, ecs_entity_t entity, ecs_entity_t rel, ecs_id_t component)
Get the target of a relationship for a given component.
const ecs_type_t * ecs_get_type(const ecs_world_t *world, ecs_entity_t entity)
Get the type of an entity.
bool ecs_owns_id(const ecs_world_t *world, ecs_entity_t entity, ecs_id_t component)
Test if an entity owns a component.
char * ecs_type_str(const ecs_world_t *world, const ecs_type_t *type)
Convert a type to a string.
char * ecs_table_str(const ecs_world_t *world, const ecs_table_t *table)
Convert a table to a string.
int32_t ecs_count_id(const ecs_world_t *world, ecs_id_t entity)
Count entities that have the specified ID.
int32_t ecs_get_depth(const ecs_world_t *world, ecs_entity_t entity, ecs_entity_t rel)
Return the depth for an entity in the tree for the specified relationship.
ecs_table_t * ecs_get_table(const ecs_world_t *world, ecs_entity_t entity)
Get the table of an entity.
ecs_entity_t ecs_new_w_parent(ecs_world_t *world, ecs_entity_t parent, const char *name)
Create child with Parent component.
bool ecs_has_id(const ecs_world_t *world, ecs_entity_t entity, ecs_id_t component)
Test if an entity has a component.
void(*) ecs_ctx_free_t(void *ctx)
Function to clean up context data.
Definition flecs.h:660
uint64_t(*) ecs_group_by_action_t(ecs_world_t *world, ecs_table_t *table, ecs_id_t group_id, void *ctx)
Callback used for grouping tables in a query.
Definition flecs.h:631
void(*) ecs_copy_t(void *dst_ptr, const void *src_ptr, int32_t count, const ecs_type_info_t *type_info)
Copy is invoked when a component is copied into another component.
Definition flecs.h:679
void(*) ecs_xtor_t(void *ptr, int32_t count, const ecs_type_info_t *type_info)
Constructor/destructor callback.
Definition flecs.h:673
void(*) ecs_group_delete_action_t(ecs_world_t *world, uint64_t group_id, void *group_ctx, void *group_by_ctx)
Callback invoked when a query deletes an existing group.
Definition flecs.h:644
void(*) ecs_module_action_t(ecs_world_t *world)
Initialization action for modules.
Definition flecs.h:651
void(*) ecs_iter_fini_action_t(ecs_iter_t *it)
Function prototype for freeing an iterator.
Definition flecs.h:609
void(*) ecs_iter_action_t(ecs_iter_t *it)
Function prototype for iterables.
Definition flecs.h:591
void *(*) ecs_group_create_action_t(ecs_world_t *world, uint64_t group_id, void *group_by_ctx)
Callback invoked when a query creates a new group.
Definition flecs.h:638
void(*) ecs_sort_table_action_t(ecs_world_t *world, ecs_table_t *table, ecs_entity_t *entities, void *ptr, int32_t size, int32_t lo, int32_t hi, ecs_order_by_action_t order_by)
Callback used for sorting the entire table of components.
Definition flecs.h:620
uint64_t(*) ecs_hash_value_action_t(const void *ptr)
Callback used for hashing values.
Definition flecs.h:669
int(*) ecs_order_by_action_t(ecs_entity_t e1, const void *ptr1, ecs_entity_t e2, const void *ptr2)
Callback used for comparing components.
Definition flecs.h:613
void(*) ecs_run_action_t(ecs_iter_t *it)
Function prototype for runnables (systems, observers).
Definition flecs.h:582
void(*) ecs_move_t(void *dst_ptr, void *src_ptr, int32_t count, const ecs_type_info_t *type_info)
Move is invoked when a component is moved to another component.
Definition flecs.h:686
void(*) flecs_poly_dtor_t(ecs_poly_t *poly)
Destructor function for poly objects.
Definition flecs.h:713
void(*) ecs_fini_action_t(ecs_world_t *world, void *ctx)
Action callback on world exit.
Definition flecs.h:655
bool(*) ecs_equals_t(const void *a_ptr, const void *b_ptr, const ecs_type_info_t *type_info)
Equals operator hook.
Definition flecs.h:699
int(*) ecs_cmp_t(const void *a_ptr, const void *b_ptr, const ecs_type_info_t *type_info)
Compare hook to compare component instances.
Definition flecs.h:693
bool(*) ecs_iter_next_action_t(ecs_iter_t *it)
Function prototype for iterating an iterator.
Definition flecs.h:601
int(*) ecs_compare_action_t(const void *ptr1, const void *ptr2)
Callback used for sorting values.
Definition flecs.h:664
bool(*) ecs_on_validate_t(ecs_world_t *world, ecs_entity_t entity, void *ptr)
On validate hook.
Definition flecs.h:707
void * ecs_ref_get_id(const ecs_world_t *world, ecs_ref_t *ref, ecs_id_t component)
Get a component from a ref.
void * ecs_emplace_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t component, size_t size, bool *is_new)
Emplace a component.
void ecs_ref_update(const ecs_world_t *world, ecs_ref_t *ref, ecs_id_t component)
Update a ref.
const void * ecs_get_id(const ecs_world_t *world, ecs_entity_t entity, ecs_id_t component)
Get an immutable pointer to a component.
void ecs_modified_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t component)
Signal that a component has been modified.
ecs_ref_t ecs_ref_init_id(const ecs_world_t *world, ecs_entity_t entity, ecs_id_t component)
Create a component ref.
void * ecs_get_mut_id(const ecs_world_t *world, ecs_entity_t entity, ecs_id_t component)
Get a mutable pointer to a component.
void * ecs_ensure_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t component, size_t size)
Ensure an entity has a component and return a pointer.
void ecs_set_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t component, size_t size, const void *ptr)
Set the value of a component.
void * ecs_get_sparse_id(const ecs_world_t *world, ecs_entity_t entity, ecs_id_t component, size_t size)
Get a pointer to a sparse component.
const ecs_id_t ECS_PAIR
Indicate that the ID is a pair.
const ecs_id_t ECS_AUTO_OVERRIDE
Automatically override component when it is inherited.
const ecs_id_t ECS_TOGGLE
Add a bitset to storage, which allows a component to be enabled or disabled.
const ecs_id_t ECS_VALUE_PAIR
Indicate that the target of a pair is an integer value.
char * ecs_id_str(const ecs_world_t *world, ecs_id_t component)
Convert a component ID to a string.
bool ecs_id_is_tag(const ecs_world_t *world, ecs_id_t component)
Return whether a specified component is a tag.
ecs_flags32_t ecs_id_get_flags(const ecs_world_t *world, ecs_id_t component)
Get flags associated with an ID.
bool ecs_id_is_valid(const ecs_world_t *world, ecs_id_t component)
Utility to check if an ID is valid.
const char * ecs_id_flag_str(uint64_t component_flags)
Convert a component flag to a string.
bool ecs_id_in_use(const ecs_world_t *world, ecs_id_t component)
Return whether a specified component is in use.
bool ecs_id_match(ecs_id_t component, ecs_id_t pattern)
Utility to match a component with a pattern.
void ecs_id_str_buf(const ecs_world_t *world, ecs_id_t component, ecs_strbuf_t *buf)
Write a component string to a buffer.
bool ecs_id_is_pair(ecs_id_t component)
Utility to check if a component is a pair.
ecs_entity_t ecs_get_typeid(const ecs_world_t *world, ecs_id_t component)
Get the type for a component.
ecs_id_t ecs_id_from_str(const ecs_world_t *world, const char *expr)
Convert a string to a component.
bool ecs_id_is_wildcard(ecs_id_t component)
Utility to check if a component is a wildcard.
bool ecs_id_is_any(ecs_id_t component)
Utility to check if a component is an any wildcard.
ecs_entity_t ecs_field_src(const ecs_iter_t *it, int8_t index)
Return the field source.
bool ecs_iter_changed(ecs_iter_t *it)
Return whether the current iterator result has changed.
bool ecs_field_is_writeonly(const ecs_iter_t *it, int8_t index)
Test whether the field is write-only.
ecs_var_t * ecs_iter_get_vars(const ecs_iter_t *it)
Get the variable array.
bool ecs_field_is_readonly(const ecs_iter_t *it, int8_t index)
Test whether the field is read-only.
const char * ecs_iter_get_var_name(const ecs_iter_t *it, int32_t var_id)
Get the variable name.
ecs_iter_t ecs_worker_iter(const ecs_iter_t *it, int32_t index, int32_t count)
Create a worker iterator.
bool ecs_iter_is_true(ecs_iter_t *it)
Test if an iterator is true.
void ecs_iter_fini(ecs_iter_t *it)
Clean up iterator resources.
bool ecs_iter_var_is_constrained(ecs_iter_t *it, int32_t var_id)
Return whether a variable is constrained.
ecs_iter_t ecs_page_iter(const ecs_iter_t *it, int32_t offset, int32_t limit)
Create a paged iterator.
int32_t ecs_iter_get_var_count(const ecs_iter_t *it)
Get the number of variables.
void * ecs_field_at_w_size(const ecs_iter_t *it, size_t size, int8_t index, int32_t row)
Get data for a field at a specified row.
ecs_id_t ecs_field_id(const ecs_iter_t *it, int8_t index)
Return the ID matched for a field.
bool ecs_field_is_set(const ecs_iter_t *it, int8_t index)
Test whether a field is set.
void ecs_iter_set_var(ecs_iter_t *it, int32_t var_id, ecs_entity_t entity)
Set the value for an iterator variable.
bool ecs_field_is_self(const ecs_iter_t *it, int8_t index)
Test whether the field is matched on self.
bool ecs_iter_next(ecs_iter_t *it)
Progress any iterator.
ecs_entity_t ecs_iter_get_var(ecs_iter_t *it, int32_t var_id)
Get the value of an iterator variable as an entity.
bool ecs_worker_next(ecs_iter_t *it)
Progress a worker iterator.
ecs_entity_t ecs_iter_first(ecs_iter_t *it)
Get the first matching entity from an iterator.
int32_t ecs_field_column(const ecs_iter_t *it, int8_t index)
Return the index of a matched table column.
uint64_t ecs_iter_get_group(const ecs_iter_t *it)
Return the group ID for the currently iterated result.
void ecs_iter_set_var_as_table(ecs_iter_t *it, int32_t var_id, const ecs_table_t *table)
Same as ecs_iter_set_var(), but for a table.
ecs_table_t * ecs_iter_get_var_as_table(ecs_iter_t *it, int32_t var_id)
Get the value of an iterator variable as a table.
void * ecs_field_w_size(const ecs_iter_t *it, size_t size, int8_t index)
Get data for a field.
int32_t ecs_iter_count(ecs_iter_t *it)
Count the number of matched entities in a query.
bool ecs_page_next(ecs_iter_t *it)
Progress a paged iterator.
void ecs_iter_set_var_as_range(ecs_iter_t *it, int32_t var_id, const ecs_table_range_t *range)
Same as ecs_iter_set_var(), but for a range of entities.
size_t ecs_field_size(const ecs_iter_t *it, int8_t index)
Return the field type size.
ecs_table_range_t ecs_iter_get_var_as_range(ecs_iter_t *it, int32_t var_id)
Get the value of an iterator variable as a table range.
ecs_id_t ecs_strip_generation(ecs_entity_t e)
Remove the generation from an entity ID.
void ecs_make_alive_id(ecs_world_t *world, ecs_id_t component)
Same as ecs_make_alive(), but for components.
bool ecs_is_valid(const ecs_world_t *world, ecs_entity_t e)
Test whether an entity is valid.
void ecs_make_alive(ecs_world_t *world, ecs_entity_t entity)
Ensure an ID is alive.
ecs_entity_t ecs_get_alive(const ecs_world_t *world, ecs_entity_t e)
Get an alive identifier.
uint32_t ecs_get_version(ecs_entity_t entity)
Get the generation of an entity.
bool ecs_exists(const ecs_world_t *world, ecs_entity_t entity)
Test whether an entity exists.
bool ecs_is_alive(const ecs_world_t *world, ecs_entity_t e)
Test whether an entity is alive.
void ecs_set_version(ecs_world_t *world, ecs_entity_t entity)
Override the generation of an entity.
ecs_entity_t ecs_observer_update(ecs_world_t *world, ecs_entity_t observer, const ecs_observer_desc_t *desc)
Update an existing observer.
void ecs_emit(ecs_world_t *world, ecs_event_desc_t *desc)
Send an event.
void ecs_enqueue(ecs_world_t *world, ecs_event_desc_t *desc)
Enqueue an event.
ecs_entity_t ecs_observer_init(ecs_world_t *world, const ecs_observer_desc_t *desc)
Create an observer.
const ecs_observer_t * ecs_observer_get(const ecs_world_t *world, ecs_entity_t observer)
Get the observer object.
#define FLECS_EVENT_DESC_MAX
Maximum number of events in ecs_observer_desc_t.
Definition flecs.h:332
#define ecs_ftime_t
Customizable precision for scalar time values.
Definition flecs.h:59
#define FLECS_ID_DESC_MAX
Maximum number of IDs to add in ecs_entity_desc_t / ecs_bulk_desc_t.
Definition flecs.h:326
#define FLECS_TERM_COUNT_MAX
Maximum number of terms in queries.
Definition flecs.h:338
#define FLECS_TREE_SPAWNER_DEPTH_CACHE_SIZE
Size of the depth cache in the tree spawner component.
Definition flecs.h:373
char * ecs_get_path_w_sep(const ecs_world_t *world, ecs_entity_t parent, ecs_entity_t child, const char *sep, const char *prefix)
Get a path identifier for an entity.
ecs_entity_t ecs_new_from_path_w_sep(ecs_world_t *world, ecs_entity_t parent, const char *path, const char *sep, const char *prefix)
Find or create an entity from a path.
ecs_entity_t ecs_lookup_symbol(const ecs_world_t *world, const char *symbol, bool lookup_as_path, bool recursive)
Look up an entity by its symbol name.
void ecs_set_alias(ecs_world_t *world, ecs_entity_t entity, const char *alias)
Set an alias for an entity.
ecs_entity_t ecs_lookup(const ecs_world_t *world, const char *path)
Look up an entity by its path.
ecs_entity_t ecs_get_scope(const ecs_world_t *world)
Get the current scope.
void ecs_get_path_w_sep_buf(const ecs_world_t *world, ecs_entity_t parent, ecs_entity_t child, const char *sep, const char *prefix, ecs_strbuf_t *buf, bool escape)
Write a path identifier to a buffer.
ecs_entity_t * ecs_set_lookup_path(ecs_world_t *world, const ecs_entity_t *lookup_path)
Set the search path for lookup operations.
ecs_entity_t ecs_set_name(ecs_world_t *world, ecs_entity_t entity, const char *name)
Set the name of an entity.
const char * ecs_get_symbol(const ecs_world_t *world, ecs_entity_t entity)
Get the symbol of an entity.
ecs_entity_t ecs_lookup_path_w_sep(const ecs_world_t *world, ecs_entity_t parent, const char *path, const char *sep, const char *prefix, bool recursive)
Look up an entity from a path.
ecs_entity_t ecs_set_symbol(ecs_world_t *world, ecs_entity_t entity, const char *symbol)
Set the symbol of an entity.
ecs_entity_t ecs_lookup_child(const ecs_world_t *world, ecs_entity_t parent, const char *name)
Look up a child entity by name.
const char * ecs_get_name(const ecs_world_t *world, ecs_entity_t entity)
Get the name of an entity.
ecs_entity_t ecs_add_path_w_sep(ecs_world_t *world, ecs_entity_t entity, ecs_entity_t parent, const char *path, const char *sep, const char *prefix)
Add a specified path to an entity.
ecs_entity_t * ecs_get_lookup_path(const ecs_world_t *world)
Get the current lookup path.
ecs_entity_t ecs_set_scope(ecs_world_t *world, ecs_entity_t scope)
Set the current scope.
const char * ecs_set_name_prefix(ecs_world_t *world, const char *prefix)
Set a name prefix for newly created entities.
void ecs_iter_skip(ecs_iter_t *it)
Skip a table while iterating.
bool ecs_query_has_table(const ecs_query_t *query, ecs_table_t *table, ecs_iter_t *it)
Match a table with a query.
void ecs_iter_set_group(ecs_iter_t *it, uint64_t group_id)
Set the group to iterate for a query iterator.
const ecs_query_t * ecs_query_get(const ecs_world_t *world, ecs_entity_t query)
Get the query object.
bool ecs_query_next(ecs_iter_t *it)
Progress a query iterator.
const ecs_query_group_info_t * ecs_query_get_group_info(const ecs_query_t *query, uint64_t group_id)
Get information about a query group.
ecs_query_t * ecs_query_update(ecs_world_t *world, ecs_entity_t entity, const ecs_query_desc_t *desc)
Replace the query on an existing entity.
bool ecs_query_is_true(const ecs_query_t *query)
Test whether a query returns one or more results.
char * ecs_term_str(const ecs_world_t *world, const ecs_term_t *term)
Convert a term to a string expression.
bool ecs_query_has_range(const ecs_query_t *query, ecs_table_range_t *range, ecs_iter_t *it)
Match a range with a query.
int32_t ecs_query_find_var(const ecs_query_t *query, const char *name)
Find a variable index.
void ecs_query_fini(ecs_query_t *query)
Delete a query.
bool ecs_query_has(const ecs_query_t *query, ecs_entity_t entity, ecs_iter_t *it)
Match an entity with a query.
char * ecs_query_plan_w_profile(const ecs_query_t *query, const ecs_iter_t *it)
Convert a query to a string with a profile.
const char * ecs_query_args_parse(ecs_query_t *query, ecs_iter_t *it, const char *expr)
Populate variables from a key-value string.
int32_t ecs_query_match_count(const ecs_query_t *query)
Return how often a match event happened for a cached query.
char * ecs_query_plan(const ecs_query_t *query)
Convert a query to a string.
ecs_iter_t ecs_query_iter(const ecs_world_t *world, const ecs_query_t *query)
Create a query iterator.
char * ecs_query_str(const ecs_query_t *query)
Convert a query to a string expression.
bool ecs_query_var_is_entity(const ecs_query_t *query, int32_t var_id)
Test if a variable is an entity.
ecs_query_t * ecs_query_init(ecs_world_t *world, const ecs_query_desc_t *desc)
Create a query.
bool ecs_query_changed(ecs_query_t *query)
Return whether the query data changed since the last iteration.
const char * ecs_query_var_name(const ecs_query_t *query, int32_t var_id)
Get the variable name.
bool ecs_term_match_this(const ecs_term_t *term)
Is a term matched on the $this variable.
const ecs_entity_t EcsOnQueryCacheRevalidate
Event emitted when a table needs to be revalidated for a query cache.
char * ecs_query_plans(const ecs_query_t *query)
Same as ecs_query_plan(), but includes the plan for populating the cache (if any).
void * ecs_query_get_group_ctx(const ecs_query_t *query, uint64_t group_id)
Get the context of a query group.
ecs_query_count_t ecs_query_count(const ecs_query_t *query)
Return the number of entities and results the query matches with.
const ecs_map_t * ecs_query_get_groups(const ecs_query_t *query)
Return the map with query groups.
bool ecs_term_is_initialized(const ecs_term_t *term)
Test whether a term is set.
bool ecs_term_ref_is_set(const ecs_term_ref_t *ref)
Test whether a term ref is set.
bool ecs_term_match_0(const ecs_term_t *term)
Is a term matched on a 0 source.
const ecs_query_t * ecs_query_get_cache_query(const ecs_query_t *query)
Get the query used to populate the cache.
ecs_query_cache_kind_t
Specify cache policy for query.
Definition flecs.h:747
ecs_inout_kind_t
Specify read/write access for term.
Definition flecs.h:726
ecs_oper_kind_t
Specify operator for term.
Definition flecs.h:736
@ EcsQueryCacheAll
Require that all query terms can be cached.
Definition flecs.h:751
@ EcsQueryCacheDefault
Behavior determined by query creation context.
Definition flecs.h:748
@ EcsQueryCacheNone
No caching.
Definition flecs.h:753
@ EcsQueryCacheAuto
Cache query terms that are cacheable.
Definition flecs.h:749
@ EcsOut
Term is only written.
Definition flecs.h:732
@ EcsInOut
Term is both read and written.
Definition flecs.h:730
@ EcsInOutFilter
Same as InOutNone + prevents term from triggering observers.
Definition flecs.h:729
@ EcsInOutDefault
InOut for regular terms, In for shared terms.
Definition flecs.h:727
@ EcsInOutNone
Term is neither read nor written.
Definition flecs.h:728
@ EcsIn
Term is only read.
Definition flecs.h:731
@ EcsNot
The term must not match.
Definition flecs.h:739
@ EcsOptional
The term may match.
Definition flecs.h:740
@ EcsOr
One of the terms in an or chain must match.
Definition flecs.h:738
@ EcsOrFrom
Term must match at least one component from term ID.
Definition flecs.h:742
@ EcsAnd
The term must match.
Definition flecs.h:737
@ EcsNotFrom
Term must match none of the components from term ID.
Definition flecs.h:743
@ EcsAndFrom
Term must match all components from term ID.
Definition flecs.h:741
ecs_table_t * ecs_table_add_id(ecs_world_t *world, ecs_table_t *table, ecs_id_t component)
Get the table that has all components of the current table plus the specified ID.
int32_t ecs_search_offset(const ecs_world_t *world, const ecs_table_t *table, int32_t offset, ecs_id_t component, ecs_id_t *component_out)
Search for a component in a table type starting from an offset.
ecs_table_t * ecs_table_remove_id(ecs_world_t *world, ecs_table_t *table, ecs_id_t component)
Get the table that has all components of the current table minus the specified component.
const ecs_type_t * ecs_table_get_type(const ecs_table_t *table)
Get the type for a table.
int32_t ecs_table_get_column_index(const ecs_world_t *world, const ecs_table_t *table, ecs_id_t component)
Get the column index for a component.
int32_t ecs_search_relation_for_entity(const ecs_world_t *world, ecs_entity_t entity, ecs_id_t id, ecs_entity_t rel, bool self, ecs_component_record_t *cr, ecs_entity_t *tgt_out, ecs_id_t *id_out, struct ecs_table_record_t **tr_out)
Search for a component ID by following a relationship, starting from an entity.
void * ecs_table_get_id(const ecs_world_t *world, const ecs_table_t *table, ecs_id_t component, int32_t offset)
Get a column from a table by component.
int32_t ecs_table_size(const ecs_table_t *table)
Return the allocated size of the table.
bool ecs_table_has_flags(ecs_table_t *table, ecs_flags32_t flags)
Test a table for flags.
int32_t ecs_table_column_to_type_index(const ecs_table_t *table, int32_t index)
Convert a column index to a type index.
int32_t ecs_table_column_count(const ecs_table_t *table)
Return the number of columns in a table.
int32_t ecs_search_relation(const ecs_world_t *world, const ecs_table_t *table, int32_t offset, ecs_id_t component, ecs_entity_t rel, ecs_flags64_t flags, ecs_entity_t *tgt_out, ecs_id_t *component_out, struct ecs_table_record_t **tr_out)
Search for a component or relationship ID in a table type starting from an offset.
void * ecs_table_get_column(const ecs_table_t *table, int32_t index, int32_t offset)
Get a column from a table by column index.
int32_t ecs_search(const ecs_world_t *world, const ecs_table_t *table, ecs_id_t component, ecs_id_t *component_out)
Search for a component in a table type.
ecs_entity_t ecs_table_get_target(const ecs_world_t *world, const ecs_table_t *table, ecs_entity_t relationship, int32_t index)
Get the relationship target for a table.
int32_t ecs_table_count(const ecs_table_t *table)
Return the number of entities in the table.
const ecs_entity_t * ecs_table_entities(const ecs_table_t *table)
Return the array with entity IDs for the table.
void ecs_table_unlock(ecs_world_t *world, ecs_table_t *table)
Unlock a table.
bool ecs_table_has_traversable(const ecs_table_t *table)
Check if a table has traversable entities.
int32_t ecs_table_get_depth(const ecs_world_t *world, const ecs_table_t *table, ecs_entity_t rel)
Return the depth for a table in the tree for the specified relationship.
bool ecs_table_has_id(const ecs_world_t *world, const ecs_table_t *table, ecs_id_t component)
Test if a table has a component.
void ecs_table_swap_rows(ecs_world_t *world, ecs_table_t *table, int32_t row_1, int32_t row_2)
Swap two elements inside the table.
void ecs_table_lock(ecs_world_t *world, ecs_table_t *table)
Lock a table.
ecs_table_t * ecs_table_find(ecs_world_t *world, const ecs_id_t *ids, int32_t id_count)
Find a table from an ID array.
size_t ecs_table_get_column_size(const ecs_table_t *table, int32_t index)
Get the column size from a table.
int32_t ecs_table_get_type_index(const ecs_world_t *world, const ecs_table_t *table, ecs_id_t component)
Get the type index for a component.
void ecs_table_clear_entities(ecs_world_t *world, ecs_table_t *table)
Remove all entities in a table.
int32_t ecs_table_type_to_column_index(const ecs_table_t *table, int32_t index)
Convert a type index to a column index.
void ecs_atfini(ecs_world_t *world, ecs_fini_action_t action, void *ctx)
Register an action to be executed when the world is destroyed.
bool ecs_is_fini(const ecs_world_t *world)
Return whether the world is being deleted.
int ecs_fini(ecs_world_t *world)
Delete a world.
ecs_flags32_t ecs_world_get_flags(const ecs_world_t *world)
Get flags set on the world.
ecs_world_t * ecs_mini(void)
Create a new world with just the core module.
ecs_world_t * ecs_init(void)
Create a new world.
ecs_world_t * ecs_init_w_args(int argc, char *argv[])
Create a new world with arguments.
ecs_entities_t ecs_get_entities(const ecs_world_t *world)
Return entity identifiers in the world.
ecs_entity_t ecs_get_entity(const ecs_poly_t *poly)
Get the entity from a poly.
ecs_id_t ecs_make_pair(ecs_entity_t first, ecs_entity_t second)
Make a pair ID.
const ecs_build_info_t * ecs_get_build_info(void)
Get build info.
bool flecs_poly_is_(const ecs_poly_t *object, int32_t type)
Test if a pointer is of the specified type.
ecs_entity_t ecs_get_max_id(const ecs_world_t *world)
Get the largest issued entity ID (not counting generation).
void ecs_run_aperiodic(ecs_world_t *world, ecs_flags32_t flags)
Force aperiodic actions.
void * ecs_get_binding_ctx(const ecs_world_t *world)
Get the world binding context.
void ecs_shrink(ecs_world_t *world)
Free unused memory.
void ecs_dim(ecs_world_t *world, int32_t entity_count)
Dimension the world for a specified number of entities.
const ecs_world_info_t * ecs_get_world_info(const ecs_world_t *world)
Get the world info.
const ecs_world_t * ecs_get_world(const ecs_poly_t *poly)
Get the world from a poly.
void ecs_set_ctx(ecs_world_t *world, void *ctx, ecs_ctx_free_t ctx_free)
Set a world context.
void ecs_exclusive_access_begin(ecs_world_t *world, const char *thread_name)
Begin exclusive thread access.
int32_t ecs_delete_empty_tables(ecs_world_t *world, const ecs_delete_empty_tables_desc_t *desc)
Clean up empty tables.
void ecs_set_binding_ctx(ecs_world_t *world, void *ctx, ecs_ctx_free_t ctx_free)
Set a world binding context.
void ecs_exclusive_access_end(ecs_world_t *world, bool lock_world)
End exclusive thread access.
void * ecs_get_ctx(const ecs_world_t *world)
Get the world context.
Operating system abstraction API.
Component information.
Definition flecs.h:1620
ecs_size_t size
Component size.
Definition flecs.h:1621
ecs_size_t alignment
Component alignment.
Definition flecs.h:1622
A (string) identifier.
Definition flecs.h:1611
ecs_size_t length
Length of identifier.
Definition flecs.h:1613
char * value
Identifier string.
Definition flecs.h:1612
ecs_hashmap_t * index
Current index.
Definition flecs.h:1616
uint64_t hash
Hash of current value.
Definition flecs.h:1614
uint64_t index_hash
Hash of existing record in current index.
Definition flecs.h:1615
Non-fragmenting ChildOf relationship.
Definition flecs.h:1631
ecs_entity_t value
Parent entity.
Definition flecs.h:1632
Component for storing a poly object.
Definition flecs.h:1626
ecs_poly_t * poly
Pointer to poly object.
Definition flecs.h:1627
Apply a rate filter to a tick source.
Definition timer.h:45
Component used to provide a tick source to systems.
Definition system.h:32
Component used for one-shot and interval timer functionality.
Definition timer.h:35
Tree instantiation cache component.
Definition flecs.h:1655
ecs_tree_spawner_t data[(6)]
Cache data indexed by depth.
Definition flecs.h:1656
Type with information about the current Flecs build.
Definition flecs.h:1520
const char ** flags
Compile-time settings.
Definition flecs.h:1523
int16_t version_major
Major Flecs version.
Definition flecs.h:1525
const char ** addons
Addons included in the build.
Definition flecs.h:1522
const char * version
Stringified version.
Definition flecs.h:1524
const char * compiler
Compiler used to compile Flecs.
Definition flecs.h:1521
bool sanitize
Is this a sanitize build?
Definition flecs.h:1529
bool perf_trace
Is this a perf tracing build?
Definition flecs.h:1530
int16_t version_minor
Minor Flecs version.
Definition flecs.h:1526
bool debug
Is this a debug build?
Definition flecs.h:1528
int16_t version_patch
Patch Flecs version.
Definition flecs.h:1527
Used with ecs_bulk_init().
Definition flecs.h:1114
ecs_id_t ids[32]
IDs to create the entities with.
Definition flecs.h:1124
void ** data
Array with component data to insert.
Definition flecs.h:1126
int32_t count
Number of entities to create/populate.
Definition flecs.h:1122
int32_t _canary
Used for validity testing.
Definition flecs.h:1115
ecs_entity_t * entities
Entities to bulk insert.
Definition flecs.h:1117
ecs_table_t * table
Table to insert the entities into.
Definition flecs.h:1133
Used with ecs_component_init().
Definition flecs.h:1144
int32_t _canary
Used for validity testing.
Definition flecs.h:1145
ecs_type_info_t type
Parameters for type (size, hooks, ...).
Definition flecs.h:1151
ecs_entity_t entity
Existing entity to associate with a component (optional).
Definition flecs.h:1148
Used with ecs_delete_empty_tables().
Definition flecs.h:2530
uint16_t delete_generation
Delete table when generation > delete_generation.
Definition flecs.h:2535
double time_budget_seconds
Amount of time operation is allowed to spend.
Definition flecs.h:2538
uint16_t clear_generation
Free table data when generation > clear_generation.
Definition flecs.h:2532
int32_t offset
Table index to start scanning at.
Definition flecs.h:2542
Type returned by ecs_get_entities().
Definition flecs.h:2061
int32_t alive_count
Number of alive entity IDs.
Definition flecs.h:2064
int32_t count
Total number of entity IDs.
Definition flecs.h:2063
const ecs_entity_t * ids
Array with all entity IDs in the world.
Definition flecs.h:2062
Used with ecs_entity_init().
Definition flecs.h:1077
const char * sep
Optional custom separator for hierarchical names.
Definition flecs.h:1089
const char * root_sep
Optional, used for identifiers relative to the root.
Definition flecs.h:1093
const char * name
Name of the entity.
Definition flecs.h:1084
bool use_low_id
When set to true, a low id (typically reserved for components) will be used to create the entity,...
Definition flecs.h:1105
const char * symbol
Optional entity symbol.
Definition flecs.h:1095
int32_t _canary
Used for validity testing.
Definition flecs.h:1078
ecs_entity_t id
Set to modify existing entity (optional).
Definition flecs.h:1080
ecs_entity_t parent
Parent entity.
Definition flecs.h:1082
Used with ecs_emit().
Definition flecs.h:1460
ecs_entity_t entity
Single-entity alternative to setting table / offset / count.
Definition flecs.h:1485
const void * const_param
Same as param, but with the guarantee that the value won't be modified.
Definition flecs.h:1496
ecs_table_t * table
The table for which to notify.
Definition flecs.h:1470
int32_t count
Limit number of notified entities to count.
Definition flecs.h:1482
ecs_table_t * other_table
Optional second table to notify.
Definition flecs.h:1474
int32_t offset
Limit notified entities to ones starting from offset (row) in table.
Definition flecs.h:1477
const ecs_type_t * ids
Component IDs.
Definition flecs.h:1467
void * set_ptr
Optional pointer to the value of the component for which the event is emitted.
Definition flecs.h:1502
ecs_poly_t * observable
Observable (usually the world).
Definition flecs.h:1505
ecs_entity_t event
The event ID.
Definition flecs.h:1462
ecs_flags32_t flags
Event flags.
Definition flecs.h:1508
void * param
Optional context.
Definition flecs.h:1491
Header for ecs_poly_t objects.
Definition flecs.h:534
int32_t type
Magic number indicating which type of Flecs object.
Definition flecs.h:535
int32_t refcount
Refcount, to enable RAII handles.
Definition flecs.h:536
ecs_mixins_t * mixins
Table with offsets to (optional) mixins.
Definition flecs.h:537
Iterator.
Definition flecs.h:1192
ecs_world_t * real_world
Actual world.
Definition flecs.h:1195
void * param
Param passed to ecs_run().
Definition flecs.h:1231
ecs_entity_t event
The event (if applicable).
Definition flecs.h:1219
int32_t frame_offset
Offset relative to the start of iteration.
Definition flecs.h:1242
ecs_flags32_t ref_fields
Bitset with fields that aren't component arrays.
Definition flecs.h:1213
ecs_entity_t interrupted_by
When set, system execution is interrupted.
Definition flecs.h:1246
ecs_flags32_t flags
Iterator flags.
Definition flecs.h:1245
ecs_iter_t * chain_it
Optional, allows for creating iterator chains.
Definition flecs.h:1253
void * ctx
System context.
Definition flecs.h:1232
void * run_ctx
Run language binding context.
Definition flecs.h:1235
ecs_table_t * table
Current table.
Definition flecs.h:1205
int32_t offset
Offset relative to the current table.
Definition flecs.h:1198
ecs_id_t event_id
The (component) ID for the event.
Definition flecs.h:1220
ecs_iter_fini_action_t fini
Function to clean up iterator resources.
Definition flecs.h:1252
ecs_iter_private_t priv_
Private data.
Definition flecs.h:1247
ecs_world_t * world
The world.
Definition flecs.h:1194
void * callback_ctx
Callback language binding context.
Definition flecs.h:1234
ecs_entity_t * sources
Entity on which the ID was matched (0 if same as entities).
Definition flecs.h:1208
void * binding_ctx
System binding context.
Definition flecs.h:1233
ecs_flags32_t row_fields
Fields that must be obtained with field_at.
Definition flecs.h:1214
float delta_system_time
Time elapsed since last system invocation.
Definition flecs.h:1239
const ecs_query_t * query
Query being evaluated.
Definition flecs.h:1228
int8_t term_index
Index of the term that emitted an event.
Definition flecs.h:1225
ecs_entity_t system
The system (if applicable).
Definition flecs.h:1218
ecs_flags32_t set_fields
Fields that are set.
Definition flecs.h:1212
const ecs_size_t * sizes
Component sizes.
Definition flecs.h:1204
float delta_time
Time elapsed since last frame.
Definition flecs.h:1238
ecs_flags32_t up_fields
Bitset with fields matched through up traversal.
Definition flecs.h:1215
ecs_iter_action_t callback
Callback of system or observer.
Definition flecs.h:1251
int8_t field_count
Number of fields in the iterator.
Definition flecs.h:1224
ecs_table_t * other_table
Previous or next table when adding or removing.
Definition flecs.h:1206
int32_t event_cur
Unique event ID.
Definition flecs.h:1221
const ecs_table_record_t ** trs
Info on where to find the field in the table.
Definition flecs.h:1202
int32_t count
Number of entities to iterate.
Definition flecs.h:1199
void ** ptrs
Component pointers.
Definition flecs.h:1201
ecs_iter_next_action_t next
Function to progress iterator.
Definition flecs.h:1250
const ecs_entity_t * entities
Entity identifiers.
Definition flecs.h:1200
ecs_id_t * ids
(Component) IDs.
Definition flecs.h:1207
ecs_flags64_t constrained_vars
Bitset that marks constrained variables.
Definition flecs.h:1210
Used with ecs_observer_init().
Definition flecs.h:1399
ecs_ctx_free_t run_ctx_free
Callback to free run ctx.
Definition flecs.h:1448
ecs_ctx_free_t ctx_free
Callback to free ctx.
Definition flecs.h:1436
void * run_ctx
Context associated with run (for language bindings).
Definition flecs.h:1445
ecs_entity_t entity
Existing entity to associate with an observer (optional).
Definition flecs.h:1404
int32_t * last_event_id
Used for internal purposes.
Definition flecs.h:1451
void * callback_ctx
Context associated with callback (for language bindings).
Definition flecs.h:1439
void * ctx
User context to pass to callback.
Definition flecs.h:1433
ecs_query_desc_t query
Query for observer.
Definition flecs.h:1407
ecs_flags32_t flags_
Used for internal purposes.
Definition flecs.h:1453
ecs_ctx_free_t callback_ctx_free
Callback to free callback ctx.
Definition flecs.h:1442
int8_t term_index_
Used for internal purposes.
Definition flecs.h:1452
bool global_observer
Global observers are tied to the lifespan of the world.
Definition flecs.h:1419
ecs_iter_action_t callback
Callback to invoke on an event, invoked when the observer matches.
Definition flecs.h:1422
bool yield_existing
When an observer is created, generate events from existing data.
Definition flecs.h:1414
ecs_run_action_t run
Callback invoked on an event.
Definition flecs.h:1430
ecs_entity_t events[8]
Events to observe (OnAdd, OnRemove, OnSet).
Definition flecs.h:1410
int32_t _canary
Used for validity testing.
Definition flecs.h:1401
An observer reacts to events matching a query.
Definition flecs.h:903
int32_t event_count
Number of events.
Definition flecs.h:910
ecs_iter_action_t callback
See ecs_observer_desc_t::callback.
Definition flecs.h:912
ecs_entity_t entity
Entity associated with the observer.
Definition flecs.h:926
ecs_observable_t * observable
Observable for the observer.
Definition flecs.h:923
ecs_run_action_t run
See ecs_observer_desc_t::run.
Definition flecs.h:913
ecs_header_t hdr
Object header.
Definition flecs.h:904
ecs_ctx_free_t ctx_free
Callback to free ctx.
Definition flecs.h:919
ecs_entity_t events[8]
Observer events.
Definition flecs.h:909
void * run_ctx
Run language binding context.
Definition flecs.h:917
ecs_world_t * world
The world.
Definition flecs.h:925
ecs_ctx_free_t run_ctx_free
Callback to free run_ctx.
Definition flecs.h:921
void * callback_ctx
Callback language binding context.
Definition flecs.h:916
void * ctx
Observer context.
Definition flecs.h:915
ecs_ctx_free_t callback_ctx_free
Callback to free callback_ctx.
Definition flecs.h:920
ecs_query_t * query
Observer query.
Definition flecs.h:906
Payload for EcsOnQueryCacheRevalidate event.
Definition flecs.h:4837
ecs_entity_t query
Query for which to revalidate table.
Definition flecs.h:4838
uint64_t table_id
Id of table to revalidate.
Definition flecs.h:4839
Struct returned by ecs_query_count().
Definition flecs.h:5053
int32_t entities
Number of entities returned by the query.
Definition flecs.h:5055
int32_t results
Number of results returned by the query.
Definition flecs.h:5054
int32_t tables
Number of tables returned by the query.
Definition flecs.h:5056
Used with ecs_query_init().
Definition flecs.h:1325
ecs_ctx_free_t ctx_free
Callback to free ctx.
Definition flecs.h:1386
ecs_id_t group_by
Component ID to be used for grouping.
Definition flecs.h:1356
ecs_term_t terms[32]
Query terms.
Definition flecs.h:1330
int32_t _canary
Used for validity testing.
Definition flecs.h:1327
void * ctx
User context to pass to callback.
Definition flecs.h:1380
ecs_ctx_free_t group_by_ctx_free
Function to free group_by_ctx.
Definition flecs.h:1377
void * group_by_ctx
Context to pass to group_by.
Definition flecs.h:1374
void * binding_ctx
Context to be used for language bindings.
Definition flecs.h:1383
ecs_entity_t order_by
Component to sort on, used together with order_by_callback or order_by_table_callback.
Definition flecs.h:1352
ecs_order_by_action_t order_by_callback
Callback used for ordering query results.
Definition flecs.h:1344
ecs_group_create_action_t on_group_create
Callback that is invoked when a new group is created.
Definition flecs.h:1367
ecs_entity_t entity
Entity associated with query (optional).
Definition flecs.h:1392
ecs_ctx_free_t binding_ctx_free
Callback to free binding_ctx.
Definition flecs.h:1389
ecs_group_by_action_t group_by_callback
Callback used for grouping results.
Definition flecs.h:1363
ecs_group_delete_action_t on_group_delete
Callback that is invoked when an existing group is deleted.
Definition flecs.h:1371
ecs_sort_table_action_t order_by_table_callback
Callback used for ordering query results.
Definition flecs.h:1348
ecs_flags32_t flags
Flags for enabling query features.
Definition flecs.h:1339
ecs_query_cache_kind_t cache_kind
Caching policy of the query.
Definition flecs.h:1336
const char * expr
Query DSL expression (optional).
Definition flecs.h:1333
Type that contains information about a query group.
Definition flecs.h:1594
int32_t table_count
Number of tables in group.
Definition flecs.h:1597
void * ctx
Group context, returned by on_group_create.
Definition flecs.h:1598
uint64_t id
Group ID.
Definition flecs.h:1595
int32_t match_count
How often tables have been matched or unmatched.
Definition flecs.h:1596
Queries are lists of constraints (terms) that match entities.
Definition flecs.h:858
ecs_flags32_t row_fields
Fields that must be acquired with field_at.
Definition flecs.h:880
ecs_flags32_t data_fields
Fields that have data.
Definition flecs.h:877
ecs_flags32_t read_fields
Fields that read data.
Definition flecs.h:879
ecs_header_t hdr
Object header.
Definition flecs.h:859
int8_t var_count
Number of query variables.
Definition flecs.h:868
ecs_flags32_t fixed_fields
Bitmasks for quick field information lookups.
Definition flecs.h:874
ecs_flags32_t var_fields
Fields with non-$this variable source.
Definition flecs.h:875
ecs_flags32_t write_fields
Fields that write data.
Definition flecs.h:878
int32_t eval_count
Number of times the query is evaluated.
Definition flecs.h:897
ecs_world_t * world
World or stage the query was created with.
Definition flecs.h:895
ecs_flags32_t static_id_fields
Fields with a static (component) id.
Definition flecs.h:876
uint64_t bloom_filter
Bitmask used to quickly discard tables.
Definition flecs.h:865
ecs_world_t * real_world
Actual world.
Definition flecs.h:894
ecs_flags32_t set_fields
Fields that will be set.
Definition flecs.h:882
ecs_entity_t entity
Entity associated with query (optional).
Definition flecs.h:893
void * ctx
User context to pass to callback.
Definition flecs.h:890
int32_t * sizes
Component sizes.
Definition flecs.h:862
ecs_id_t * ids
Component ids.
Definition flecs.h:863
ecs_flags32_t shared_readonly_fields
Fields that don't write shared data.
Definition flecs.h:881
ecs_term_t * terms
Query terms.
Definition flecs.h:861
ecs_query_cache_kind_t cache_kind
Caching policy of the query.
Definition flecs.h:884
int8_t term_count
Number of query terms.
Definition flecs.h:870
char ** vars
Array with variable names for the iterator.
Definition flecs.h:887
int8_t field_count
Number of fields returned by the query.
Definition flecs.h:871
ecs_flags32_t flags
Query flags.
Definition flecs.h:866
void * binding_ctx
Context to be used for language bindings.
Definition flecs.h:891
Type that describes a reference to an entity or variable in a term.
Definition flecs.h:819
const char * name
Name.
Definition flecs.h:826
ecs_entity_t id
Entity ID.
Definition flecs.h:820
Type that describes a term (single element in a query).
Definition flecs.h:834
ecs_term_ref_t src
Source of term.
Definition flecs.h:840
int8_t field_index
Index of the field for the term in the iterator.
Definition flecs.h:851
ecs_id_t id
Component ID to be matched by term.
Definition flecs.h:835
int16_t oper
Operator of term.
Definition flecs.h:849
ecs_term_ref_t second
Second element of pair.
Definition flecs.h:842
ecs_flags16_t flags_
Flags that help evaluation, set by ecs_query_init().
Definition flecs.h:852
ecs_entity_t trav
Relationship to traverse when looking for the component.
Definition flecs.h:844
int16_t inout
Access to contents matched by term.
Definition flecs.h:848
ecs_term_ref_t first
Component or first element of pair.
Definition flecs.h:841
Component with data to instantiate a non-fragmenting tree.
Definition flecs.h:1636
uint32_t child
Prefab child entity (without generation).
Definition flecs.h:1639
int32_t parent_index
Index into the children vector.
Definition flecs.h:1640
const char * child_name
Name of the prefab child.
Definition flecs.h:1637
ecs_table_t * table
Table in which the child will be stored.
Definition flecs.h:1638
Tree spawner data for a single hierarchy depth.
Definition flecs.h:1644
ecs_vec_t children
vector<ecs_tree_spawner_child_t>.
Definition flecs.h:1645
ecs_copy_t copy_ctor
Ctor + copy.
Definition flecs.h:985
void * lifecycle_ctx
Component lifecycle context (see meta addon).
Definition flecs.h:1041
void * ctx
User-defined context.
Definition flecs.h:1039
ecs_iter_action_t on_remove
Callback that is invoked when an instance of the component is removed.
Definition flecs.h:1026
void * binding_ctx
Language binding context.
Definition flecs.h:1040
ecs_move_t move_dtor
Move + dtor.
Definition flecs.h:1000
ecs_flags32_t flags
Hook flags.
Definition flecs.h:1012
ecs_cmp_t cmp
Compare hook.
Definition flecs.h:1003
ecs_copy_t copy
copy assignment.
Definition flecs.h:981
ecs_ctx_free_t lifecycle_ctx_free
Callback to free lifecycle_ctx.
Definition flecs.h:1045
ecs_on_validate_t on_validate
Callback that is invoked before the on_set/OnSet hooks and observers are invoked.
Definition flecs.h:1037
ecs_iter_action_t on_set
Callback that is invoked when an instance of the component is set.
Definition flecs.h:1021
ecs_move_t move
move assignment.
Definition flecs.h:982
ecs_xtor_t ctor
ctor.
Definition flecs.h:979
ecs_iter_action_t on_replace
Callback that is invoked with the existing and new value before the value is assigned.
Definition flecs.h:1032
ecs_ctx_free_t ctx_free
Callback to free ctx.
Definition flecs.h:1043
ecs_iter_action_t on_add
Callback that is invoked when an instance of a component is added.
Definition flecs.h:1016
ecs_ctx_free_t binding_ctx_free
Callback to free binding_ctx.
Definition flecs.h:1044
ecs_move_t move_ctor
Ctor + move.
Definition flecs.h:988
ecs_equals_t equals
Equals hook.
Definition flecs.h:1006
ecs_move_t ctor_move_dtor
Ctor + move + dtor (or move_ctor + dtor).
Definition flecs.h:994
ecs_xtor_t dtor
dtor.
Definition flecs.h:980
Type that contains component information (passed to ctors/dtors/...).
Definition flecs.h:1052
ecs_size_t alignment
Alignment of the type.
Definition flecs.h:1054
ecs_size_t size
Size of the type.
Definition flecs.h:1053
const char * name
Type name.
Definition flecs.h:1057
ecs_entity_t component
Handle to component (do not set).
Definition flecs.h:1056
ecs_type_hooks_t hooks
Type hooks.
Definition flecs.h:1055
int32_t refcount
Refcount (do not set).
Definition flecs.h:1058
A type is a list of (component) IDs.
Definition flecs.h:412
ecs_id_t * array
Array with IDs.
Definition flecs.h:413
int32_t count
Number of elements in array.
Definition flecs.h:414
Value of a dynamic type.
Definition flecs.h:1068
void * ptr
Pointer to value.
Definition flecs.h:1070
ecs_entity_t type
Type of value.
Definition flecs.h:1069
int64_t event_count
Enqueued custom events.
int64_t remove_count
Remove commands processed.
int64_t add_count
Add commands processed.
int64_t modified_count
Modified commands processed.
int64_t clear_count
Clear commands processed.
int64_t ensure_count
Ensure or emplace commands processed.
int64_t delete_count
Delete commands processed.
int64_t other_count
Other commands processed.
int64_t batched_entity_count
Entities for which commands were batched.
int64_t discard_count
Commands discarded, happens when the entity is no longer alive when running the command.
int64_t batched_command_count
Commands batched.
int64_t set_count
Set commands processed.
Type that contains information about the world.
Definition flecs.h:1534
int64_t observers_ran_total
Total number of times an observer was invoked.
Definition flecs.h:1560
float delta_time
Time passed to or computed by ecs_progress().
Definition flecs.h:1538
int64_t systems_ran_total
Total number of systems run.
Definition flecs.h:1559
double world_time_total
Time elapsed in simulation.
Definition flecs.h:1546
int32_t tag_id_count
Number of tag (no data) IDs in the world.
Definition flecs.h:1563
int32_t component_id_count
Number of component (data) IDs in the world.
Definition flecs.h:1564
int64_t pipeline_build_count_total
Total number of pipeline builds.
Definition flecs.h:1558
int64_t eval_comp_monitors_total
Total number of monitor evaluations.
Definition flecs.h:1551
int32_t table_count
Number of tables.
Definition flecs.h:1567
float delta_time_raw
Raw delta time (no time scaling).
Definition flecs.h:1537
float frame_time_total
Total time spent processing a frame.
Definition flecs.h:1541
int64_t table_create_total
Total number of times a table was created.
Definition flecs.h:1556
float system_time_total
Total time spent in systems.
Definition flecs.h:1542
int64_t id_delete_total
Total number of times an ID was deleted.
Definition flecs.h:1555
struct ecs_world_info_t::@354013130002221015352342176135321160002353227336 cmd
Command statistics.
float merge_time_total
Total time spent in merges.
Definition flecs.h:1544
ecs_entity_t last_component_id
Last issued component entity ID.
Definition flecs.h:1535
int64_t frame_count_total
Total number of frames.
Definition flecs.h:1549
int64_t merge_count_total
Total number of merges.
Definition flecs.h:1550
int64_t table_delete_total
Total number of times a table was deleted.
Definition flecs.h:1557
int64_t queries_ran_total
Total number of times a query was evaluated.
Definition flecs.h:1561
int64_t rematch_count_total
Total number of rematches.
Definition flecs.h:1552
int64_t id_create_total
Total number of times a new ID was created.
Definition flecs.h:1554
float time_scale
Time scale applied to delta_time.
Definition flecs.h:1539
int32_t pair_id_count
Number of pair IDs in the world.
Definition flecs.h:1565
float rematch_time_total
Time spent on query rematching.
Definition flecs.h:1545
float target_fps
Target FPS.
Definition flecs.h:1540
double world_time_total_raw
Time elapsed in simulation (no scaling).
Definition flecs.h:1547
float emit_time_total
Total time spent notifying observers.
Definition flecs.h:1543
uint32_t creation_time
Time when world was created.
Definition flecs.h:1569
const char * name_prefix
Value set by ecs_set_name_prefix().
Definition flecs.h:1587