Jump to content

AudioSource component collision on vessel gameObject


Recommended Posts

I was looking into an issue with the ShipEffects sound mod. On some launches, one of the sound effects would play the wrong clip (e.g. a decoupler sound) instead of the mod-provided sound. Additional logging showed that the first AudioSource component the mod attaches to a vessel's gameObject sometimes got its audio clip changed on launch.

"Using GameObject.GetComponent will return the first component that is found and the order is undefined." - So I *guess* KSP does GetComponent<AudioSource>() somewhere and re-uses the returned component for the vessel's launch sound effect. Does that sound like a plausible cause or am I missing something?

Here's a reduced variation of what ShipEffects does: It adds AudioSource(s) to the active vessel's gameObject and logs a line when its clip name changes.

Spoiler

using System;
using UnityEngine;
using KSP;

namespace AudioTest
{
	[KSPAddon(KSPAddon.Startup.Flight, false)]
	public class AudioTest : MonoBehaviour
	{
		AudioSource source1;
		String sourceName1 = null;

		void Start()
		{
			Vessel vessel = FlightGlobals.ActiveVessel;

			if (vessel != null)
			{
				source1 = vessel.gameObject.AddComponent<AudioSource>();
			}
		}

		void Update()
		{
			if (source1 != null && source1.clip?.name != sourceName1)
			{
				Debug.Log("AudioTest: source1 changed: " + source1.name + ":" + source1.clip?.name);
				sourceName1 = source1.clip?.name;
			}
		}
	}
}

 

 

 

Various engines cause the audio source to be changed on rollout from the VAB, e.g.:

[LOG] AudioTest: source1 changed: Thoroughbred:sound_rocket_hard

 

A tentative workaround is to create a sacrifical, unused AudioSource component but there's the "the order is undefined" of getComponent() to consider. It could as well pick a different component frorm the first one.

Should the mod create a new GameObject, attach it to the vessel and add the audio components to that child? Would that protect them from takeover and still play the sound? (I'll give it a try but didn't have time left to tests it right now)

Does it warrant a bug report?

 

 

 

Edited by HansAcker
Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...