From 32029567193e3aa9fd9c93ffc191acd914683bef Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Thu, 24 Feb 2022 13:37:16 -0600 Subject: [PATCH] Fix crashes caused by non-existent entities. --- src/sound.rs | 41 ++++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/src/sound.rs b/src/sound.rs index e15a094..160a894 100644 --- a/src/sound.rs +++ b/src/sound.rs @@ -6,6 +6,7 @@ use bevy_openal::{Buffer, Context, Sound, SoundState}; use rand::random; use crate::{ + commands::RunIfExistsExt, core::{Coordinates, CoreConfig, Player, PointLike}, exploration::ExplorationFocused, visibility::{Viewshed, VisibleEntities}, @@ -91,11 +92,14 @@ fn add_footstep_sounds( ) { for (entity, footstep) in footsteps.iter() { let buffer = assets.get_handle(footstep.sound); - commands.entity(entity).insert(Sound { - buffer, - state: SoundState::Stopped, - gain: footstep.gain, - ..Default::default() + let gain = footstep.gain; + commands.run_if_exists(entity, move |mut entity| { + entity.insert(Sound { + buffer, + state: SoundState::Stopped, + gain, + ..Default::default() + }); }); } } @@ -138,17 +142,24 @@ fn add_sound_icon_sounds( ) { for (entity, icon) in icons.iter() { let buffer = assets.get_handle(icon.sound); + let gain = icon.gain; + let pitch = icon.pitch; let looping = icon.interval.is_none(); - commands.entity(entity).insert(Sound { - buffer, - gain: icon.gain, - pitch: icon.pitch, - looping, - state: SoundState::Stopped, - reference_distance: icon.reference_distance, - max_distance: icon.max_distance, - rolloff_factor: icon.rolloff_factor, - ..Default::default() + let reference_distance = icon.reference_distance; + let max_distance = icon.max_distance; + let rolloff_factor = icon.rolloff_factor; + commands.run_if_exists(entity, move |mut entity| { + entity.insert(Sound { + buffer, + gain, + pitch, + looping, + state: SoundState::Stopped, + reference_distance, + max_distance, + rolloff_factor, + ..Default::default() + }); }); } }