Skip to content
Flecs v4.1
builder_i.hpp
Go to the documentation of this file.
1/**
2 * @file addons/cpp/mixins/system/builder_i.hpp
3 * @brief System builder interface.
4 */
5
6#pragma once
7
9
10namespace flecs
11{
12
13/** System builder interface.
14 *
15 * @ingroup cpp_addons_systems
16 */
17template<typename Base, typename ... Components>
18struct system_builder_i : query_builder_i<Base, Components ...> {
19private:
20 using BaseClass = query_builder_i<Base, Components ...>;
21
22public:
23 system_builder_i(ecs_system_desc_t *desc, int32_t term_index = 0)
24 : BaseClass(&desc->query, term_index)
25 , desc_(desc) { }
26
27 /** Specify in which phase the system should run.
28 *
29 * @param phase The phase.
30 */
31 Base& kind(entity_t phase) {
32 desc_->phase = phase;
33 return *this;
34 }
35
36 /** Specify in which phase the system should run, using an enum constant.
37 *
38 * @param phase The enum phase value.
39 */
40 template <typename E, if_t<is_enum<E>::value> = 0>
41 Base& kind(E phase)
42 {
43 const auto& et = enum_type<E>(this->world_v());
44 flecs::entity_t target = et.entity(phase);
45 return this->kind(target);
46 }
47
48 /** Specify in which phase the system should run.
49 *
50 * @tparam Phase The phase.
51 */
52 template <typename Phase>
53 Base& kind() {
54 return this->kind(_::type<Phase>::id(world_v()));
55 }
56
57 /** Specify whether the system can run on multiple threads.
58 *
59 * @param value If false, the system will always run on a single thread.
60 */
61 Base& multi_threaded(bool value = true) {
62 desc_->multi_threaded = value;
63 return *this;
64 }
65
66 /** Specify whether the system should be run in an immediate (non-staged) context.
67 *
68 * @param value If false, the system will always run staged.
69 */
70 Base& immediate(bool value = true) {
71 desc_->immediate = value;
72 return *this;
73 }
74
75 /** Set the system interval.
76 * This operation will cause the system to be run at the specified interval.
77 *
78 * The timer is synchronous, and is incremented each frame by delta_time.
79 *
80 * @param interval The interval value.
81 */
83 desc_->interval = interval;
84 return *this;
85 }
86
87 /** Set the system rate.
88 * This operation will cause the system to be run at a multiple of the
89 * provided tick source. The tick source may be any entity, including
90 * another system.
91 *
92 * @param tick_source The tick source.
93 * @param rate The multiple at which to run the system.
94 */
95 Base& rate(const entity_t tick_source, int32_t rate) {
96 desc_->rate = rate;
97 desc_->tick_source = tick_source;
98 return *this;
99 }
100
101 /** Set the system rate.
102 * This operation will cause the system to be run at a multiple of the
103 * frame tick frequency. If a tick source was provided, this just updates
104 * the rate of the system.
105 *
106 * @param rate The multiple at which to run the system.
107 */
108 Base& rate(int32_t rate) {
109 desc_->rate = rate;
110 return *this;
111 }
112
113 /** Set the tick source.
114 * This operation sets a shared tick source for the system.
115 *
116 * @tparam T The type associated with the singleton tick source to use for the system.
117 */
118 template<typename T>
119 Base& tick_source() {
120 desc_->tick_source = _::type<T>::id(world_v());
121 return *this;
122 }
123
124 /** Set the tick source.
125 * This operation sets a shared tick source for the system.
126 *
127 * @param tick_source The tick source to use for the system.
128 */
130 desc_->tick_source = tick_source;
131 return *this;
132 }
133
134 /** Set the system context. */
135 Base& ctx(void *ptr) {
136 desc_->ctx = ptr;
137 return *this;
138 }
139
140 /** Set the system run callback. */
141 Base& run(ecs_iter_action_t action) {
142 desc_->run = action;
143 return *this;
144 }
145
146protected:
147 virtual flecs::world_t* world_v() override = 0;
148
149private:
150 operator Base&() {
151 return *static_cast<Base*>(this);
152 }
153
154 ecs_system_desc_t *desc_;
155};
156
157}
enum_data< E > enum_type(flecs::world_t *world)
Convenience function for getting enum reflection data.
Definition enum.hpp:457
ecs_entity_t entity_t
Entity type.
Definition c_types.hpp:21
ecs_world_t world_t
World type.
Definition c_types.hpp:18
void(*) ecs_iter_action_t(ecs_iter_t *it)
Function prototype for iterables.
Definition flecs.h:591
#define ecs_ftime_t
Customizable precision for scalar time values.
Definition flecs.h:59
Query builder interface.
Use with ecs_system_init() and ecs_system_update().
Definition system.h:38
ecs_query_desc_t query
System query parameters.
Definition system.h:45
query_builder_i(ecs_query_desc_t *desc, int32_t term_index=0)
Definition builder_i.hpp:20
Base & kind(entity_t phase)
Specify in which phase the system should run.
Definition builder_i.hpp:31
Base & kind(E phase)
Specify in which phase the system should run, using an enum constant.
Definition builder_i.hpp:41
Base & tick_source(flecs::entity_t tick_source)
Set the tick source.
Base & rate(int32_t rate)
Set the system rate.
Base & immediate(bool value=true)
Specify whether the system should be run in an immediate (non-staged) context.
Definition builder_i.hpp:70
Base & ctx(void *ptr)
Set the system context.
Base & rate(const entity_t tick_source, int32_t rate)
Set the system rate.
Definition builder_i.hpp:95
Base & run(ecs_iter_action_t action)
Set the system run callback.
Base & kind()
Specify in which phase the system should run.
Definition builder_i.hpp:53
Base & interval(ecs_ftime_t interval)
Set the system interval.
Definition builder_i.hpp:82
Base & tick_source()
Set the tick source.
Base & multi_threaded(bool value=true)
Specify whether the system can run on multiple threads.
Definition builder_i.hpp:61
Base & desc()
Use with cascade() to iterate results in descending (bottom-to-top) order.