Skip to content

Commit d7fd097

Browse files
committed
Fix #24: better handling for NRE spam on unloading root part
1 parent 372d516 commit d7fd097

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

Source/RocketSoundEnhancement/RocketSoundEnhancement.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using KSP.UI.Screens;
22
using RocketSoundEnhancement.AudioFilters;
33
using RocketSoundEnhancement.Unity;
4+
using System;
45
using System.Collections.Generic;
56
using System.Linq;
67
using UnityEngine;
@@ -191,14 +192,14 @@ public void LateUpdate()
191192
// if the source was already in the set, we're done
192193
if (!managedSources.Add(instanceID)) continue;
193194

194-
if (source.name.Contains(AudioUtility.RSETag)) continue;
195-
196195
// assume this source is a GUI source and ignore
197196
if (source.transform.position == Vector3.zero || source.spatialBlend == 0)
198197
{
199198
continue;
200199
}
201200

201+
if (source.name.Contains(AudioUtility.RSETag)) continue;
202+
202203
if (source.GetComponentInParent<InternalProp>())
203204
{
204205
source.outputAudioMixerGroup = InteriorMixer;
@@ -282,6 +283,16 @@ public void Initialize(Part part, AudioSource source)
282283
this.part = part;
283284
this.source = source;
284285
managedMinDistance = source.minDistance;
286+
287+
// The root gameobject of a vessel holds both the vessel component and the part component
288+
// The part component is created and destroyed as the vessel is loaded and unloaded but the gameobject is never destroyed (until the vessel is...)
289+
// So we need to make sure to delete the RSE_PartAudioManager when the part is destroyed.
290+
part.OnJustAboutToBeDestroyed += OnPartDestroyed;
291+
}
292+
293+
private void OnPartDestroyed()
294+
{
295+
Destroy(this);
285296
}
286297

287298
void LateUpdate()

Source/RocketSoundEnhancement/ShipEffects.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -198,16 +198,6 @@ public override void OnLoadVessel()
198198

199199
public override void OnUnloadVessel()
200200
{
201-
// strange stock behavior: when the vessel is unloaded, all parts *except* for the root are destroyed
202-
if (vessel.rootPart != null)
203-
{
204-
var partAudioManager = vessel.rootPart.GetComponent<RSE_PartAudioManager>();
205-
if (partAudioManager != null)
206-
{
207-
Destroy(partAudioManager);
208-
}
209-
}
210-
211201
if (!initialized) return;
212202
Unload();
213203
}

0 commit comments

Comments
 (0)