Jump to content

Play Sound without Part


Recommended Posts

Hi, I'm trying to play a sound from a file on certain events during flight, like launch, or decoupling.

All examples I have found, seem to be using parts to play the sounds, and even Chatterer seems to use a dummy part.

Is there any way I could play a sound file without the need of parts?

The class I'm using is MonoBehaviour, and GameDatabase.Instance.ExistsAudioClip() finds the file correctly, and I can see it in the database too.

Link to comment
Share on other sites

EvilReaper showed me that you can add a PartModule to a vessel like this:


namespace MySomething
{
[KSPAddon(KSPAddon.Startup.Flight, false)]
public class MySomethingBehaviour : MonoBehaviour
{
void Start()
{
FlightGlobals.ActiveVessel.rootPart.AddModule("MySomethingModule");
}


}
}

The PartModule MySomethingModule essentially gets applied to the root part just as if you'd added it to that parts .cfg. You can hook into and extend a lot of OnUndock() etc. functions quite easily too, though I'd have to look up how to do that as I can't remember off the top of my head. The caveat is it gets applied to every vessel unless you add some conditional rules inside the start routine, though that may be what you want.

All that being said, I would use with caution as it's quite a far-reaching modification.

Good luck, I Hope that helps.

Link to comment
Share on other sites

Thanks lo-fi. I'm very new in KSP modding, sorry. Do I have to create that module somewhere else too? Does it work like a dummy part? I guess it's not as easy as copying that line inside my Start()... The log says it can't find the part module.

I'm trying to play the sound with this:

SoundGroup.audio = gameObject.AddComponent<AudioSource>();
SoundGroup.audio.clip = GameDatabase.Instance.GetAudioClip(SoundFile_OnLaunch);
SoundGroup.audio.Play();

But I get the following: NullReferenceException: Object reference not set to an instance of an object

I guess setting an FXGroup is only good for a part...

[EDIT]: By the way, I found this discussion between Blizzy and Athlonic, but I think I didn't understand how to implement it: http://forum.kerbalspaceprogram.com/threads/53305-Playing-sound-on-AudioSource-correctly-%28solved%29?highlight=play+sound

Edited by Dielos
Link to comment
Share on other sites

Hi blizzy, no it's not. Just to be sure, I have this in front:

if (!GameDatabase.Instance.ExistsAudioClip(SoundFile_OnLaunch))
{
Debug.LogError("KerbalComms: Audio file not found: " + SoundFile_OnLaunch);
return;
}

I even renamed my sound file to check that the error is displayed correctly...

[EDIT]: Ok, just in case I'm forgetting something very basic, this is the code I have. It's mostly ripped off examples or other posts, as I'm still too new to this. The sound file is from Chatterer, for testing purposes.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

[KSPAddon(KSPAddon.Startup.Flight, false)]

public class KerbalComms : MonoBehaviour
{

public string SoundFile_OnLaunch = "KerbalComms/Sounds/capcom_01";
public FXGroup SoundGroup = null;

public void Awake() { }

public void Start() {
print("KerbalComms: Plugin started correctly!!");
GameEvents.onLaunch.Add(OnLaunch);
}

private void OnLaunch(EventReport eventReport) {
print("KerbalComms: Aaaaaand... LIFTOFF !!! (OnLaunch)");
ScreenMessages.PostScreenMessage("Aaaaaand... LIFTOFF !!!", 5.0f, ScreenMessageStyle.UPPER_CENTER);

if (!GameDatabase.Instance.ExistsAudioClip(SoundFile_OnLaunch))
{
Debug.LogError("KerbalComms: Audio file not found: " + SoundFile_OnLaunch);
return;
}

SoundGroup.audio = gameObject.AddComponent<AudioSource>();
SoundGroup.audio.clip = GameDatabase.Instance.GetAudioClip(SoundFile_OnLaunch);
SoundGroup.audio.Play();
SoundGroup.audio.loop = false;
}

}

Edited by Dielos
Link to comment
Share on other sites

Most likely it's too far away to be heard by any AudioListener. If you don't need positional sound, you can disable it by setting panLevel to 0. Here's a complete example (excluding an actual clip) that will play a sound whenever the game is paused or the player decouples a stage:

[KSPAddon(KSPAddon.Startup.Flight, false)]
[RequireComponent(typeof(AudioSource))] // AddComponent works too
class TestAudio : MonoBehaviour
{
void Start()
{
audio.clip = GameDatabase.Instance.GetAudioClip("folder/clip");
audio.panLevel = 0f;

GameEvents.onStageSeparation.Add(StageSeparated);
GameEvents.onGamePause.Add(OnPause);
}

void OnDestroy() {
GameEvents.onStageSeparation.Remove(StageSeparated);
GameEvents.onGamePause.Remove(OnPause);
}

void StageSeparated(EventReport report) { audio.Play(); }

void OnPause() { audio.Play(); }
}

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...