Skip to content

Commit

Permalink
Remove elements from monitored_bodies and monitored_areas as they are
Browse files Browse the repository at this point in the history
processed before calling the callback, instead of after they have all
been processed, because the callbacks may readd them.
  • Loading branch information
madmiraal committed Jul 4, 2020
1 parent 037571d commit ccdfaf2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 23 deletions.
29 changes: 18 additions & 11 deletions servers/physics/area_sw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,24 +189,28 @@ void AreaSW::call_queries() {
return;
}

for (Map<BodyKey, BodyState>::Element *E = monitored_bodies.front(); E; E = E->next()) {
for (Map<BodyKey, BodyState>::Element *E = monitored_bodies.front(); E;) {

if (E->get().state == 0)
continue; //nothing happened
if (E->get().state == 0) { // Nothing happened
E = E->next();
continue;
}

res[0] = E->get().state > 0 ? PhysicsServer::AREA_BODY_ADDED : PhysicsServer::AREA_BODY_REMOVED;
res[1] = E->key().rid;
res[2] = E->key().instance_id;
res[3] = E->key().body_shape;
res[4] = E->key().area_shape;

Map<BodyKey, BodyState>::Element *next = E->next();
monitored_bodies.erase(E);
E = next;

Variant::CallError ce;
obj->call(monitor_callback_method, (const Variant **)resptr, 5, ce);
}
}

monitored_bodies.clear();

if (area_monitor_callback_id && !monitored_areas.empty()) {

Variant res[5];
Expand All @@ -221,24 +225,27 @@ void AreaSW::call_queries() {
return;
}

for (Map<BodyKey, BodyState>::Element *E = monitored_areas.front(); E; E = E->next()) {
for (Map<BodyKey, BodyState>::Element *E = monitored_areas.front(); E;) {

if (E->get().state == 0)
continue; //nothing happened
if (E->get().state == 0) { // Nothing happened
E = E->next();
continue;
}

res[0] = E->get().state > 0 ? PhysicsServer::AREA_BODY_ADDED : PhysicsServer::AREA_BODY_REMOVED;
res[1] = E->key().rid;
res[2] = E->key().instance_id;
res[3] = E->key().body_shape;
res[4] = E->key().area_shape;

Map<BodyKey, BodyState>::Element *next = E->next();
monitored_areas.erase(E);
E = next;

Variant::CallError ce;
obj->call(area_monitor_callback_method, (const Variant **)resptr, 5, ce);
}
}

monitored_areas.clear();
//get_space()->area_remove_from_monitor_query_list(&monitor_query_list);
}

AreaSW::AreaSW() :
Expand Down
30 changes: 18 additions & 12 deletions servers/physics_2d/area_2d_sw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,24 +189,28 @@ void Area2DSW::call_queries() {
return;
}

for (Map<BodyKey, BodyState>::Element *E = monitored_bodies.front(); E; E = E->next()) {
for (Map<BodyKey, BodyState>::Element *E = monitored_bodies.front(); E;) {

if (E->get().state == 0)
continue; //nothing happened
if (E->get().state == 0) { // Nothing happened
E = E->next();
continue;
}

res[0] = E->get().state > 0 ? Physics2DServer::AREA_BODY_ADDED : Physics2DServer::AREA_BODY_REMOVED;
res[1] = E->key().rid;
res[2] = E->key().instance_id;
res[3] = E->key().body_shape;
res[4] = E->key().area_shape;

Map<BodyKey, BodyState>::Element *next = E->next();
monitored_bodies.erase(E);
E = next;

Variant::CallError ce;
obj->call(monitor_callback_method, (const Variant **)resptr, 5, ce);
}
}

monitored_bodies.clear();

if (area_monitor_callback_id && !monitored_areas.empty()) {

Variant res[5];
Expand All @@ -221,25 +225,27 @@ void Area2DSW::call_queries() {
return;
}

for (Map<BodyKey, BodyState>::Element *E = monitored_areas.front(); E; E = E->next()) {
for (Map<BodyKey, BodyState>::Element *E = monitored_areas.front(); E;) {

if (E->get().state == 0)
continue; //nothing happened
if (E->get().state == 0) { // Nothing happened
E = E->next();
continue;
}

res[0] = E->get().state > 0 ? Physics2DServer::AREA_BODY_ADDED : Physics2DServer::AREA_BODY_REMOVED;
res[1] = E->key().rid;
res[2] = E->key().instance_id;
res[3] = E->key().body_shape;
res[4] = E->key().area_shape;

Map<BodyKey, BodyState>::Element *next = E->next();
monitored_areas.erase(E);
E = next;

Variant::CallError ce;
obj->call(area_monitor_callback_method, (const Variant **)resptr, 5, ce);
}
}

monitored_areas.clear();

//get_space()->area_remove_from_monitor_query_list(&monitor_query_list);
}

Area2DSW::Area2DSW() :
Expand Down

0 comments on commit ccdfaf2

Please sign in to comment.