Skip to content

Commit

Permalink
Change List to Vector and copy on iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
CedNaru committed Mar 18, 2024
1 parent 23ee6bf commit 7c79e79
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
12 changes: 7 additions & 5 deletions src/fmod_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,19 +212,21 @@ void FmodServer::update() {

callback_mutex->unlock();

for (OneShot* oneShot : oneShots) {

Vector<OneShot*> one_shots_copy = oneShots;
for (OneShot* oneShot : one_shots_copy) {

if (!oneShot->instance->is_valid() || is_dead(oneShot->gameObj)) {
//We release one-shots as soon as they are started, the event becomes invalid as soon as it ends
//We release one-shots when they are started, the event becomes invalid as soon as it ends
oneShots.erase(oneShot);
delete oneShot;
continue;
}

if (Node* obj = oneShot->gameObj) { oneShot->instance->set_node_attributes(obj); }
oneShot->instance->set_node_attributes(oneShot->gameObj);
}

for (const Ref<FmodEvent>& event : runningEvents) {
Vector<Ref<FmodEvent>> events_copy = runningEvents;
for (const Ref<FmodEvent>& event : events_copy) {
if (!event->is_valid()) { runningEvents.erase(event); }
}

Expand Down
27 changes: 15 additions & 12 deletions src/fmod_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "studio/fmod_event_description.h"
#include "studio/fmod_vca.h"
#include "templates/hash_map.hpp"
#include "templates/vector.hpp"
#include "variant/string.hpp"

#include <callback/event_callbacks.h>
Expand Down Expand Up @@ -95,8 +96,8 @@ namespace godot {
bool listenerWarning = true;
Listener listeners[FMOD_MAX_LISTENERS];

List<OneShot*> oneShots;
List<Ref<FmodEvent>> runningEvents;
Vector<OneShot*> oneShots;
Vector<Ref<FmodEvent>> runningEvents;

Ref<FmodPerformanceData> performanceData;

Expand Down Expand Up @@ -274,14 +275,14 @@ namespace godot {
ERROR_CHECK(fetch_event_description<parameter_type>(identifier)->get_wrapped()->createInstance(&eventInstance));

Ref<FmodEvent> ref = FmodEvent::create_ref(eventInstance);
if (ref.is_valid()) {
runningEvents.push_back(ref);
if (ref.is_null() || !ref->is_valid()) {
GODOT_LOG_WARNING("Event Instance is invalid.")
return {};
}


ref->get_wrapped()->setUserData(ref.ptr());
ref->set_distance_scale(distanceScale);

runningEvents.push_back(ref);
return ref;
}

Expand All @@ -301,17 +302,19 @@ namespace godot {

if (ref.is_null()) { return; }

auto* oneShot = new OneShot();
oneShot->gameObj = game_obj;
oneShot->instance = ref;
oneShots.push_back(oneShot);

if (game_obj) { ref->set_node_attributes(game_obj); }
if (!parameters.is_empty()) {
// set the initial parameter values
_apply_parameter_dict_to_event(ref, parameters);
}

if(game_obj){
auto* oneShot = new OneShot();
oneShot->gameObj = game_obj;
oneShot->instance = ref;
ref->set_node_attributes(game_obj);
oneShots.push_back(oneShot);
}

ref->start();
ref->release();
}
Expand Down

0 comments on commit 7c79e79

Please sign in to comment.