@@ -41,52 +41,67 @@ void execution_callbacks::invoke(
41
41
42
42
lk.unlock ();
43
43
44
- for (auto & component_info : init_callbacks) {
45
- const void * component_data = ecsact_get_component (
46
- registry_id,
47
- component_info.entity_id ,
48
- component_info.component_id
49
- );
50
-
51
- execution_events->init_callback (
52
- component_info.event ,
53
- component_info.entity_id ,
54
- component_info.component_id ,
55
- component_data,
56
- execution_events->init_callback_user_data
57
- );
44
+ if (execution_events->init_callback != nullptr ) {
45
+ for (auto & component_info : init_callbacks) {
46
+ const void * component_data = ecsact_get_component (
47
+ registry_id,
48
+ component_info.entity_id ,
49
+ component_info.component_id
50
+ );
51
+
52
+ execution_events->init_callback (
53
+ component_info.event ,
54
+ component_info.entity_id ,
55
+ component_info.component_id ,
56
+ component_data,
57
+ execution_events->init_callback_user_data
58
+ );
59
+ }
58
60
}
59
61
60
- for (auto & component_info : update_callbacks) {
61
- const void * component_data = ecsact_get_component (
62
- registry_id,
63
- component_info.entity_id ,
64
- component_info.component_id
65
- );
66
-
67
- execution_events->update_callback (
68
- component_info.event ,
69
- component_info.entity_id ,
70
- component_info.component_id ,
71
- component_data,
72
- execution_events->update_callback_user_data
73
- );
62
+ if (execution_events->update_callback != nullptr ) {
63
+ for (auto & component_info : update_callbacks) {
64
+ const void * component_data = ecsact_get_component (
65
+ registry_id,
66
+ component_info.entity_id ,
67
+ component_info.component_id
68
+ );
69
+
70
+ execution_events->update_callback (
71
+ component_info.event ,
72
+ component_info.entity_id ,
73
+ component_info.component_id ,
74
+ component_data,
75
+ execution_events->update_callback_user_data
76
+ );
77
+ }
74
78
}
75
79
76
- for (auto & component_info : remove_callbacks) {
77
- const void * component_data = ecsact_get_component (
78
- registry_id,
79
- component_info.entity_id ,
80
- component_info.component_id
81
- );
82
-
83
- execution_events->remove_callback (
84
- component_info.event ,
85
- component_info.entity_id ,
86
- component_info.component_id ,
87
- component_data,
88
- execution_events->remove_callback_user_data
89
- );
80
+ if (execution_events->remove_callback != nullptr ) {
81
+ for (auto & component_info : remove_callbacks) {
82
+ for (auto itr = removed_execute_components.begin ();
83
+ itr != removed_execute_components.end ();) {
84
+ auto & execute_component = *itr;
85
+
86
+ if (execute_component.entity_id != component_info.entity_id && execute_component._id != component_info.component_id ) {
87
+ ++itr;
88
+ continue ;
89
+ }
90
+
91
+ auto deserialized_component =
92
+ ecsact::deserialize (execute_component._id , execute_component.data );
93
+
94
+ execution_events->remove_callback (
95
+ component_info.event ,
96
+ component_info.entity_id ,
97
+ component_info.component_id ,
98
+ deserialized_component.data (),
99
+ execution_events->remove_callback_user_data
100
+ );
101
+
102
+ itr = removed_execute_components.erase (itr);
103
+ }
104
+ }
90
105
}
91
106
}
92
107
@@ -98,6 +113,29 @@ void execution_callbacks::init_callback(
98
113
void * callback_user_data
99
114
) {
100
115
auto self = static_cast <execution_callbacks*>(callback_user_data);
116
+
117
+ auto result =
118
+ std::erase_if (self->remove_callbacks_info , [&](auto & remove_cb_info) {
119
+ return remove_cb_info.component_id == component_id &&
120
+ remove_cb_info.entity_id == entity_id;
121
+ });
122
+
123
+ std::erase_if (self->update_callbacks_info , [&](auto & update_cb_info) {
124
+ return update_cb_info.component_id == component_id &&
125
+ update_cb_info.entity_id == entity_id;
126
+ });
127
+
128
+ if (result > 0 ) {
129
+ for (int i = 0 ; i < self->removed_execute_components .size (); ++i) {
130
+ auto & execute_component = self->removed_execute_components [i];
131
+ if (execute_component._id == component_id) {
132
+ self->removed_execute_components .erase (
133
+ self->removed_execute_components .begin () + i
134
+ );
135
+ }
136
+ }
137
+ }
138
+
101
139
auto info = types::callback_info{};
102
140
103
141
info.event = event;
@@ -114,6 +152,7 @@ void execution_callbacks::update_callback(
114
152
void * callback_user_data
115
153
) {
116
154
auto self = static_cast <execution_callbacks*>(callback_user_data);
155
+
117
156
auto info = types::callback_info{};
118
157
119
158
info.event = event;
@@ -132,12 +171,34 @@ void execution_callbacks::remove_callback(
132
171
auto self = static_cast <execution_callbacks*>(callback_user_data);
133
172
134
173
std::erase_if (self->init_callbacks_info , [&](auto & init_cb_info) {
135
- return init_cb_info.component_id == component_id;
174
+ return init_cb_info.component_id == component_id &&
175
+ init_cb_info.entity_id == entity_id;
176
+ });
177
+
178
+ // Maybe I can store data from here?
179
+ std::erase_if (self->update_callbacks_info , [&](auto & update_cb_info) {
180
+ return update_cb_info.component_id == component_id &&
181
+ update_cb_info.entity_id == entity_id;
136
182
});
137
183
138
184
auto info = types::callback_info{};
139
185
info.event = event;
140
186
info.entity_id = entity_id;
141
187
info.component_id = component_id;
188
+
189
+ auto component_to_serialize = ecsact_component{
190
+ .component_id = component_id,
191
+ .component_data = component_data};
192
+
193
+ auto serialized_component = ecsact::serialize (component_to_serialize);
194
+
195
+ self->removed_execute_components .push_back (types::cpp_execution_component{
196
+ .entity_id = entity_id,
197
+ ._id = component_id,
198
+ .data = serialized_component,
199
+ });
200
+
201
+ // Same for remove, I could store data here
202
+ // maybe...?
142
203
self->remove_callbacks_info .push_back (info);
143
204
}
0 commit comments