From 7c79e792f41fb449a14a7487d6fd65b046ee27f3 Mon Sep 17 00:00:00 2001 From: Ced Naru Date: Mon, 18 Mar 2024 17:53:51 +0100 Subject: [PATCH] Change List to Vector and copy on iteration --- src/fmod_server.cpp | 12 +++++++----- src/fmod_server.h | 27 +++++++++++++++------------ 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/fmod_server.cpp b/src/fmod_server.cpp index a89f26c4..ee8fcf48 100644 --- a/src/fmod_server.cpp +++ b/src/fmod_server.cpp @@ -212,19 +212,21 @@ void FmodServer::update() { callback_mutex->unlock(); - for (OneShot* oneShot : oneShots) { + + Vector 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& event : runningEvents) { + Vector> events_copy = runningEvents; + for (const Ref& event : events_copy) { if (!event->is_valid()) { runningEvents.erase(event); } } diff --git a/src/fmod_server.h b/src/fmod_server.h index 2bf803b6..56a920c4 100644 --- a/src/fmod_server.h +++ b/src/fmod_server.h @@ -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 @@ -95,8 +96,8 @@ namespace godot { bool listenerWarning = true; Listener listeners[FMOD_MAX_LISTENERS]; - List oneShots; - List> runningEvents; + Vector oneShots; + Vector> runningEvents; Ref performanceData; @@ -274,14 +275,14 @@ namespace godot { ERROR_CHECK(fetch_event_description(identifier)->get_wrapped()->createInstance(&eventInstance)); Ref 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; } @@ -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(); }