Flecs: include/flecs/addons/cpp/mixins/script/impl.hpp Source File
Description: A fast entity component system (ECS) for C & C++

| Flecs v4.1 A fast entity component system (ECS) for C & C++

Loading...
Searching...
No Matches
impl.hpp
Go to the documentation of this file.
1
5
6#pragma once
7
8#include "builder.hpp"
9
10namespace flecs
11{
12
13 inline flecs::entity script_builder::run() const {
14 ecs_entity_t e = ecs_script_init(world_, &desc_);
15 return flecs::entity(world_, e);
16}
17
18namespace _ {
19
20 inline ecs_value_t get_const_var(const flecs::world_t *world, const char *name) {
21 flecs::entity_t v = ecs_lookup_path_w_sep(
22 world, 0, name, "::", "::", false);
23 if (!v) {
24 ecs_warn("unresolved const variable '%s', returning default", name);
25 return {};
26 }
27
28 ecs_value_t value = ecs_const_var_get(world, v);
29 if (value.ptr == nullptr) {
30 ecs_warn("entity '%s' is not a const variable, returning default",
31 name);
32 return {};
33 }
34
35 return value;
36 }
37
38 template <typename T>
39 inline T get_const_value(
40 flecs::world_t *world, const char *name, ecs_value_t value, ecs_entity_t type, const T& default_value)
41 {
42 char *requested_type = ecs_get_path(world, type);
43 char *var_type = ecs_get_path(world, value.type);
44 ecs_warn("cannot cast const variable '%s' from type '%s' to '%s', "
45 "returning default", name, var_type, requested_type);
46 ecs_os_free(requested_type);
47 ecs_os_free(var_type);
48 return default_value;
49 }
50
51 template <>
52 inline char get_const_value<char>(
53 flecs::world_t *world, const char *name, ecs_value_t value, ecs_entity_t type, const char& default_value)
54 {
55 (void)name; (void)type; (void)default_value;
56 ecs_meta_cursor_t cur = ecs_meta_cursor(world, value.type, value.ptr);
57 return ecs_meta_get_char(&cur);
58 }
59
60 template <>
61 inline int8_t get_const_value<int8_t>(
62 flecs::world_t *world, const char *name, ecs_value_t value, ecs_entity_t type, const int8_t& default_value)
63 {
64 (void)name; (void)type; (void)default_value;
65 ecs_meta_cursor_t cur = ecs_meta_cursor(world, value.type, value.ptr);
66 return static_cast<int8_t>(ecs_meta_get_int(&cur));
67 }
68
69 template <>
70 inline int16_t get_const_value<int16_t>(
71 flecs::world_t *world, const char *name, ecs_value_t value, ecs_entity_t type, const int16_t& default_value)
72 {
73 (void)name; (void)type; (void)default_value;
74 ecs_meta_cursor_t cur = ecs_meta_cursor(world, value.type, value.ptr);
75 return static_cast<int16_t>(ecs_meta_get_int(&cur));
76 }
77
78 template <>
79 inline int32_t get_const_value<int32_t>(
80 flecs::world_t *world, const char *name, ecs_value_t value, ecs_entity_t type, const int32_t& default_value)
81 {
82 (void)name; (void)type; (void)default_value;
83 ecs_meta_cursor_t cur = ecs_meta_cursor(world, value.type, value.ptr);
84 return static_cast<int32_t>(ecs_meta_get_int(&cur));
85 }
86
87 template <>
88 inline int64_t get_const_value<int64_t>(
89 flecs::world_t *world, const char *name, ecs_value_t value, ecs_entity_t type, const int64_t& default_value)
90 {
91 (void)name; (void)type; (void)default_value;
92 ecs_meta_cursor_t cur = ecs_meta_cursor(world, value.type, value.ptr);
93 return ecs_meta_get_int(&cur);
94 }
95
96 template <>
97 inline uint8_t get_const_value<uint8_t>(
98 flecs::world_t *world, const char *name, ecs_value_t value, ecs_entity_t type, const uint8_t& default_value)
99 {
100 (void)name; (void)type; (void)default_value;
101 ecs_meta_cursor_t cur = ecs_meta_cursor(world, value.type, value.ptr);
102 return static_cast<uint8_t>(ecs_meta_get_uint(&cur));
103 }
104
105 template <>
106 inline uint16_t get_const_value<uint16_t>(
107 flecs::world_t *world, const char *name, ecs_value_t value, ecs_entity_t type, const uint16_t& default_value)
108 {
109 (void)name; (void)type; (void)default_value;
110 ecs_meta_cursor_t cur = ecs_meta_cursor(world, value.type, value.ptr);
111 return static_cast<uint16_t>(ecs_meta_get_uint(&cur));
112 }
113
114 template <>
115 inline uint32_t get_const_value<uint32_t>(
116 flecs::world_t *world, const char *name, ecs_value_t value, ecs_entity_t type, const uint32_t& default_value)
117 {
118 (void)name; (void)type; (void)default_value;
119 ecs_meta_cursor_t cur = ecs_meta_cursor(world, value.type, value.ptr);
120 return static_cast<uint32_t>(ecs_meta_get_uint(&cur));
121 }
122
123 template <>
124 inline uint64_t get_const_value<uint64_t>(
125 flecs::world_t *world, const char *name, ecs_value_t value, ecs_entity_t type, const uint64_t& default_value)
126 {
127 (void)name; (void)type; (void)default_value;
128 ecs_meta_cursor_t cur = ecs_meta_cursor(world, value.type, value.ptr);
129 return ecs_meta_get_uint(&cur);
130 }
131
132 template <>
133 inline float get_const_value<float>(
134 flecs::world_t *world, const char *name, ecs_value_t value, ecs_entity_t type, const float& default_value)
135 {
136 (void)name; (void)type; (void)default_value;
137 ecs_meta_cursor_t cur = ecs_meta_cursor(world, value.type, value.ptr);
138 return static_cast<float>(ecs_meta_get_float(&cur));
139 }
140
141 template <>
142 inline double get_const_value<double>(
143 flecs::world_t *world, const char *name, ecs_value_t value, ecs_entity_t type, const double& default_value)
144 {
145 (void)name; (void)type; (void)default_value;
146 ecs_meta_cursor_t cur = ecs_meta_cursor(world, value.type, value.ptr);
147 return ecs_meta_get_float(&cur);
148 }
149}
150
151template <typename T>
152 inline T world::get_const_var(
153 const char *name,
154 const T& default_value) const
155{
156 ecs_value_t value = flecs::_::get_const_var(world_, name);
157 if (!value.ptr) {
158 return default_value;
159 }
160
161 flecs::id_t type = flecs::_::type<T>::id(world_);
162 if (type == value.type) {
163 return *(static_cast<T*>(value.ptr));
164 }
165
166 return flecs::_::get_const_value<T>(
167 world_, name, value, type, default_value);
168}
169
170template <typename T>
171 void world::get_const_var(
172 const char *name,
173 T& out,
174 const T& default_value) const
175{
176 ecs_value_t value = flecs::_::get_const_var(world_, name);
177 if (!value.ptr) {
178 out = default_value;
179 return;
180 }
181
182 flecs::id_t type = flecs::_::type<T>::id(world_);
183 if (type == value.type) {
184 out = *(static_cast<T*>(value.ptr));
185 return;
186 }
187
188 out = flecs::_::get_const_value<T>(
189 world_, name, value, type, default_value);
190}
191
192
193namespace script {
194namespace _ {
195
196inline void init(flecs::world& world) {
197 world.component<Script>("flecs::script::Script");
198}
199
200}
201}
202
203}
ecs_value_t
struct ecs_value_t ecs_value_t
Utility to hold a value of a dynamic type.
ecs_warn
#define ecs_warn(...)
Warning macro.
Definition log.h:314
ecs_meta_cursor_t
struct ecs_meta_cursor_t ecs_meta_cursor_t
Type that enables iterating and populating a value using reflection data.
ecs_meta_cursor
FLECS_API ecs_meta_cursor_t ecs_meta_cursor(const ecs_world_t *world, ecs_entity_t type, void *ptr)
Create meta cursor.
ecs_meta_get_float
FLECS_API double ecs_meta_get_float(const ecs_meta_cursor_t *cursor)
Get field value as float.
ecs_meta_get_uint
FLECS_API uint64_t ecs_meta_get_uint(const ecs_meta_cursor_t *cursor)
Get field value as unsigned integer.
ecs_meta_get_char
FLECS_API char ecs_meta_get_char(const ecs_meta_cursor_t *cursor)
Get field value as char.
ecs_meta_get_int
FLECS_API int64_t ecs_meta_get_int(const ecs_meta_cursor_t *cursor)
Get field value as signed integer.
ecs_script_init
FLECS_API ecs_entity_t ecs_script_init(ecs_world_t *world, const ecs_script_desc_t *desc)
Load managed script.
ecs_const_var_get
FLECS_API ecs_value_t ecs_const_var_get(const ecs_world_t *world, ecs_entity_t var)
Return the value for a const variable.
ecs_entity_t
ecs_id_t ecs_entity_t
An entity identifier.
Definition flecs.h:385
flecs::world::get_const_var
T get_const_var(const char *name, const T &default_value={}) const
Get the value of an exported script variable.
Definition impl.hpp:152
get_const_var
T get_const_var(const char *name, const T &default_value={}) const
Get the value of an exported script variable.
flecs::world::component
flecs::component< T > component(Args &&... args) const
Find or register a component.
Definition impl.hpp:11
flecs::id_t
ecs_id_t id_t
ID type.
Definition c_types.hpp:20
flecs::entity_t
ecs_entity_t entity_t
Entity type.
Definition c_types.hpp:21
flecs::world_t
ecs_world_t world_t
World type.
Definition c_types.hpp:18
ecs_get_path
#define ecs_get_path(world, child)
Get path from root.
Definition flecs_c.h:601
ecs_lookup_path_w_sep
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.
builder.hpp
Script builder.
flecs::_
Int to enum.
Definition component.hpp:18
ecs_value_t
Utility to hold a value of a dynamic type.
Definition flecs.h:1037
ecs_value_t::ptr
void * ptr
Pointer to value.
Definition flecs.h:1039
ecs_value_t::type
ecs_entity_t type
Type of value.
Definition flecs.h:1038
flecs::_::type
Definition flecs.hpp:39
flecs::entity
Entity.
Definition entity.hpp:30
flecs::script_builder::run
flecs::entity run() const
Run the script and return the script entity.
Definition impl.hpp:13
flecs::type
Type class.
Definition type.hpp:21
flecs::world
The world.
Definition world.hpp:246
flecs::world::world_
world_t * world_
Pointer to the underlying C world.
Definition world.hpp:1540