Skip to content
Flecs v4.1
entity.hpp
Go to the documentation of this file.
1/**
2 * @file addons/cpp/entity.hpp
3 * @brief Entity class.
4 *
5 * This class provides read/write access to entities.
6 */
7
8#pragma once
9
10#include "entity_view.hpp"
13/**
14 * @defgroup cpp_entities Entities
15 * @ingroup cpp_core
16 * Entity operations.
17 *
18 * @{
19 */
20
21namespace flecs
22{
23
24/** Entity.
25 * Class with read/write operations for entities.
26 *
27 * @ingroup cpp_entities
28 */
29struct entity : entity_builder<entity>
30{
31 /** Default constructor. Creates an empty entity. */
33
34 /** Wrap an existing entity ID.
35 *
36 * @param world The world in which the entity is created.
37 * @param id The entity ID.
38 */
40 world_ = const_cast<flecs::world_t*>(world);
41 id_ = id;
42 }
43
44 /** Create a new entity.
45 *
46 * @param world The world in which to create the entity.
47 */
48 explicit entity(world_t *world)
50 {
51 world_ = world;
52 id_ = ecs_cpp_new(world, 0, nullptr, nullptr, nullptr);
53 }
54
55 /** Create a named entity.
56 *
57 * @param world The world in which to create the entity.
58 * @param name The entity name.
59 * @param sep String used to indicate scoping (Foo::Bar).
60 * @param root_sep String used to indicate name is fully scoped (::Foo::Bar).
61 */
62 explicit entity(
63 world_t *world,
64 const char *name,
65 const char *sep = "::",
66 const char *root_sep = "::") : entity_builder()
67 {
68 world_ = world;
69
70 ecs_entity_desc_t desc = {};
71 desc.name = name;
72 desc.sep = sep;
73 desc.root_sep = root_sep;
74 id_ = ecs_entity_init(world, &desc);
75 }
76
77 /** Create a named entity for a parent using ChildOf hierarchy storage.
78 *
79 * @param world The world in which to create the entity.
80 * @param parent The parent entity ID.
81 * @param name The entity name.
82 * @param sep String used to indicate scoping (Foo::Bar).
83 * @param root_sep String used to indicate name is fully scoped (::Foo::Bar).
84 */
85 explicit entity(
88 const char *name,
89 const char *sep = "::",
90 const char *root_sep = "::") : entity_builder()
91 {
92 world_ = world;
93
94 ecs_entity_desc_t desc = {};
95 desc.name = name;
96 desc.parent = parent;
97 desc.sep = sep;
98 desc.root_sep = root_sep;
99 id_ = ecs_entity_init(world, &desc);
100 }
101
102 /** Create a named entity for a parent using Parent hierarchy storage.
103 * The specified name cannot be a scoped identifier. For example:
104 * - OK: "Foo"
105 * - Not OK: "Foo::Bar"
106 *
107 * @param world The world in which to create the entity.
108 * @param parent The parent entity.
109 * @param name The entity name (optional).
110 */
111 explicit entity(
112 world_t *world,
113 const flecs::Parent& parent,
114 const char *name = nullptr) : entity_builder()
115 {
116 world_ = world;
118 }
119
120 /** Conversion from flecs::entity_t to flecs::entity.
121 *
122 * @param id The entity_t value to convert.
123 */
124 explicit entity(entity_t id)
125 : entity_builder( nullptr, id ) { }
126
127 #ifndef ensure
128
129 template <typename... T, typename... Args>
130 decltype(auto) ensure(Args... args) const {
131 return _::get_component<true, true, true>(world_, id_,
132 _::make_id<T...>(world_, args...));
133 }
134
135 template <typename Second>
136 Second& ensure_second(entity_t first) const {
137 return _::get_component<true, true, true>(world_, id_,
138 _::second_id<Second>(world_, first));
139 }
140
141 #endif
142
143 template <typename... T, typename... Args>
144 void modified(Args... args) const {
145 auto id = _::make_id<T...>(world_, args...);
146 using A = typename decltype(id)::type;
147 if constexpr (!std::is_void_v<A>) {
148 ecs_assert(_::type<A>::size() != 0, ECS_INVALID_PARAMETER,
149 "operation invalid for empty type");
150 }
151 ecs_modified_id(world_, id_, id.id);
152 }
153
154 /** Get reference to component specified by component ID.
155 * A reference allows for quick and safe access to a component value, and is
156 * a faster alternative to repeatedly calling get() for the same component.
157 *
158 * The method accepts a component ID argument, which can be used to create a
159 * ref to a component that is different from the provided type. This allows
160 * for creating a base type ref that points to a derived type:
161 *
162 * @code
163 * flecs::ref<Base> r = e.get_ref_w_id<Base>(world.id<Derived>());
164 * @endcode
165 *
166 * If the provided component ID is not binary compatible with the specified
167 * type, the behavior is undefined.
168 *
169 * @tparam T Component for which to get a reference.
170 * @param component The component ID to reference.
171 * @return The reference.
172 */
173 template <typename T, if_t< is_actual<T>::value > = 0>
175 _::type<T>::id(world_); // Ensure type is registered.
176 return ref<T>(world_, id_, component);
177 }
178
179 template <typename... T, typename... Args>
180 auto get_ref(Args... args) const {
181 auto id = _::make_id<T...>(world_, args...);
182 using A = typename decltype(id)::type;
183 using Ref = conditional_t<std::is_void_v<A>, untyped_ref, ref<A>>;
184 return Ref(world_, id_, id.id);
185 }
186
187 template <typename Second>
188 ref<Second> get_ref_second(flecs::entity_t first) const {
189 return ref<Second>(world_, id_, _::second_id<Second>(world_, first).id);
190 }
191
192 /** Clear an entity.
193 * This operation removes all components from an entity without recycling
194 * the entity ID.
195 *
196 * @see ecs_clear()
197 */
198 void clear() const {
200 }
201
202 /** Delete an entity.
203 * Entities have to be deleted explicitly, and are not deleted when the
204 * entity object goes out of scope.
205 *
206 * @see ecs_delete()
207 */
208 void destruct() const {
210 }
211
212 /** Set child order.
213 * Changes the order of children as returned by entity::children(). Only
214 * applicable to entities with the flecs::OrderedChildren trait.
215 *
216 * @param children Array of child entity IDs in the desired order.
217 * @param child_count Number of children in the array.
218 *
219 * @see ecs_set_child_order()
220 */
221 void set_child_order(flecs::entity_t *children, int32_t child_count) const {
222 ecs_set_child_order(world_, id_, children, child_count);
223 }
224
225 /** Return the entity as an entity_view.
226 * This returns an entity_view instance for the entity, which is a read-only
227 * version of the entity class.
228 *
229 * This is similar to a regular upcast, except that this method ensures that
230 * the entity_view instance is instantiated with a world vs. a stage, which
231 * a regular upcast does not guarantee.
232 *
233 * @return The entity_view.
234 */
236 return flecs::entity_view(
237 const_cast<flecs::world_t*>(ecs_get_world(world_)), id_);
238 }
239
240 /** Entity ID 0.
241 * This function is useful when the API must provide an entity that
242 * belongs to a world, but the entity ID is 0.
243 *
244 * @param world The world.
245 * @return An entity with ID 0.
246 */
247 static
249 flecs::entity result;
250 result.world_ = const_cast<flecs::world_t*>(world);
251 return result;
252 }
253
254 /** Entity ID 0 without a world.
255 *
256 * @return An entity with ID 0 and no world.
257 */
258 static
260 return flecs::entity();
261 }
262
263# ifdef FLECS_JSON
264# include "mixins/json/entity.inl"
265# endif
266};
267
268} // namespace flecs
269
270/** @} */
JSON entity mixin.
Entity class with only read-only operations.
FLECS_API FLECS_ALWAYS_INLINE ecs_entity_t ecs_cpp_new(ecs_world_t *world, ecs_entity_t parent, const char *name, const char *sep, const char *root_sep)
Create a new entity from C++.
void ecs_clear(ecs_world_t *world, ecs_entity_t entity)
Clear all components.
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:473
#define ECS_INVALID_PARAMETER
Invalid parameter error code.
Definition log.h:671
ecs_id_t id_t
ID type.
Definition c_types.hpp:20
ecs_entity_t entity_t
Entity type.
Definition c_types.hpp:21
EcsParent Parent
Built-in EcsParent type.
Definition c_types.hpp:86
ecs_world_t world_t
World type.
Definition c_types.hpp:18
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.
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.
ecs_entity_t ecs_new_w_parent(ecs_world_t *world, ecs_entity_t parent, const char *name)
Create child with Parent component.
void ecs_modified_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t component)
Signal that a component has been modified.
const ecs_world_t * ecs_get_world(const ecs_poly_t *poly)
Get the world from a poly.
Entity builder.
ecs_entity_t value
Parent entity.
Definition flecs.h:1632
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
ecs_entity_t parent
Parent entity.
Definition flecs.h:1082
Component class.
Entity builder.
Definition builder.hpp:15
flecs::type type() const
Get the entity's type.
Definition impl.hpp:94
flecs::string_view name() const
Return the entity name.
flecs::entity parent() const
Get parent of entity.
Definition impl.hpp:68
entity_t id() const
Get entity ID.
void children(flecs::entity_t rel, Func &&func) const
Iterate children for an entity.
Entity.
Definition entity.hpp:30
void destruct() const
Delete an entity.
Definition entity.hpp:208
static flecs::entity null()
Entity ID 0 without a world.
Definition entity.hpp:259
void clear() const
Clear an entity.
Definition entity.hpp:198
ref< T > get_ref_w_id(flecs::id_t component) const
Get reference to component specified by component ID.
Definition entity.hpp:174
entity(const flecs::world_t *world, flecs::entity_t id)
Wrap an existing entity ID.
Definition entity.hpp:39
static flecs::entity null(const flecs::world_t *world)
Entity ID 0.
Definition entity.hpp:248
entity(world_t *world, flecs::entity_t parent, const char *name, const char *sep="::", const char *root_sep="::")
Create a named entity for a parent using ChildOf hierarchy storage.
Definition entity.hpp:85
entity(entity_t id)
Conversion from flecs::entity_t to flecs::entity.
Definition entity.hpp:124
entity(world_t *world, const char *name, const char *sep="::", const char *root_sep="::")
Create a named entity.
Definition entity.hpp:62
entity(world_t *world)
Create a new entity.
Definition entity.hpp:48
flecs::entity_view view() const
Return the entity as an entity_view.
Definition entity.hpp:235
void set_child_order(flecs::entity_t *children, int32_t child_count) const
Set child order.
Definition entity.hpp:221
entity()
Default constructor.
Definition entity.hpp:32
entity(world_t *world, const flecs::Parent &parent, const char *name=nullptr)
Create a named entity for a parent using Parent hierarchy storage.
Definition entity.hpp:111
Class that wraps around a flecs::id_t.
Definition decl.hpp:27
flecs::id_t id_
The raw ID value.
Definition decl.hpp:154
flecs::world_t * world_
World is optional, but guarantees that entity identifiers extracted from the ID are valid.
Definition decl.hpp:152
flecs::entity first() const
Get first element from a pair.
Definition impl.hpp:20
Component reference.
Definition ref.hpp:109
ref()
Default constructor.
Definition ref.hpp:111
Type class.
Definition type.hpp:21
Untyped component reference.
Definition ref.hpp:22
The world.
Definition world.hpp:129