Skip to content
Flecs v4.1
module.h
Go to the documentation of this file.
1/**
2 * @file addons/module.h
3 * @brief Module addon.
4 *
5 * The module addon allows for creating and importing modules. Flecs modules
6 * enable applications to organize components and systems into reusable units of
7 * code that can easily be used across projects.
8 */
9
10#ifdef FLECS_MODULE
11
12/**
13 * @defgroup c_addons_module Module
14 * @ingroup c_addons
15 * Modules organize components, systems, and more in reusable units of code.
16 *
17 * @{
18 */
19
20#ifndef FLECS_MODULE_H
21#define FLECS_MODULE_H
22
23#ifdef __cplusplus
24extern "C" {
25#endif
26
27/** Import a module.
28 * This operation will load a module. The module name will be used to verify
29 * whether the module was already loaded, in which case it won't be reimported.
30 * The name will be translated from PascalCase to an entity path (pascal.case)
31 * before the lookup occurs.
32 *
33 * Module contents will be stored as children of the module entity. This
34 * prevents modules from accidentally defining conflicting identifiers. This is
35 * enforced by setting the scope before and after loading the module to the
36 * module entity ID.
37 *
38 * A more convenient way to import a module is by using the ECS_IMPORT macro.
39 *
40 * @param world The world.
41 * @param module The module import function.
42 * @param module_name The name of the module.
43 * @return The module entity.
44 */
45FLECS_API
47 ecs_world_t *world,
49 const char *module_name);
50
51/** Same as ecs_import(), but with name to scope conversion.
52 * PascalCase names are automatically converted to scoped names.
53 *
54 * @param world The world.
55 * @param module The module import function.
56 * @param module_name_c The name of the module.
57 * @return The module entity.
58 */
59FLECS_API
61 ecs_world_t *world,
63 const char *module_name_c);
64
65/** Import a module from a library.
66 * Similar to ecs_import(), except that this operation will attempt to load the
67 * module from a dynamic library.
68 *
69 * A library may contain multiple modules, which is why both a library name and
70 * a module name need to be provided. If only a library name is provided, the
71 * library name will be reused for the module name.
72 *
73 * The library will be looked up using a canonical name, which is in the same
74 * form as a module, like `flecs.components.transform`. To transform this
75 * identifier to a platform-specific library name, the operation relies on the
76 * module_to_dl callback of the os_api, which the application has to override if
77 * the default does not yield the correct library name.
78 *
79 * @param world The world.
80 * @param library_name The name of the library to load.
81 * @param module_name The name of the module to load.
82 * @return The module entity.
83 */
84FLECS_API
86 ecs_world_t *world,
87 const char *library_name,
88 const char *module_name);
89
90/** Register a new module.
91 *
92 * @param world The world.
93 * @param c_name The name of the module.
94 * @param desc The component descriptor for the module component.
95 * @return The module entity.
96 */
97FLECS_API
99 ecs_world_t *world,
100 const char *c_name,
101 const ecs_component_desc_t *desc);
102
103/** Define module. */
104#define ECS_MODULE_DEFINE(world, id)\
105 {\
106 ecs_component_desc_t desc = {0};\
107 desc.entity = ecs_id(id);\
108 ecs_id(id) = ecs_module_init(world, #id, &desc);\
109 ecs_set_scope(world, ecs_id(id));\
110 }
111
112/** Create a module. */
113#define ECS_MODULE(world, id)\
114 ecs_entity_t ecs_id(id) = 0; ECS_MODULE_DEFINE(world, id)\
115 (void)ecs_id(id)
116
117/** Wrapper around ecs_import().
118 * This macro provides a convenient way to load a module with the world. It can
119 * be used like this:
120 *
121 * @code
122 * ECS_IMPORT(world, FlecsSystemsPhysics);
123 * @endcode
124 */
125#define ECS_IMPORT(world, id) ecs_import_c(world, id##Import, #id)
126
127#ifdef __cplusplus
128}
129#endif
130
131#endif
132
133/** @} */
134
135#endif
FLECS_API ecs_entity_t ecs_import_from_library(ecs_world_t *world, const char *library_name, const char *module_name)
Import a module from a library.
FLECS_API ecs_entity_t ecs_module_init(ecs_world_t *world, const char *c_name, const ecs_component_desc_t *desc)
Register a new module.
FLECS_API ecs_entity_t ecs_import(ecs_world_t *world, ecs_module_action_t module, const char *module_name)
Import a module.
FLECS_API ecs_entity_t ecs_import_c(ecs_world_t *world, ecs_module_action_t module, const char *module_name_c)
Same as ecs_import(), but with name to scope conversion.
ecs_id_t ecs_entity_t
An entity identifier.
Definition flecs.h:394
struct ecs_world_t ecs_world_t
A world is the container for all ECS data and supporting features.
Definition flecs.h:438
void(*) ecs_module_action_t(ecs_world_t *world)
Initialization action for modules.
Definition flecs.h:650
Used with ecs_component_init().
Definition flecs.h:1143