Jump to content

Adding a sound effect to a non-engine part?


Nuka

Recommended Posts

Hey, i'm trying to make a sort of police car rover for recovering pods and Kerbals on Kerbin. I've already got the flashing lights working with Aviation Lights, but i'd like to be able to switch on a siren on a part or at least add a command to a part such as the lights to turn on a looping siren.

I've looked at sound FX for the engines but i'm guessing this is only with throttle up, i'd just like to add a sound to a command, :/

Link to comment
Share on other sites

I would also like to know if this is possible, although I suspect it would require a plugin.

Last time I did a plug-in, there was a way to add sound on activation in the .cfg: http://wiki.kerbalspaceprogram.com/wiki/CFG_File_Documentation#Sound_FX_definition

You could make a horn part and play the sound when it is active (it loops).

Link to comment
Share on other sites

Here, have some code:


using UnityEngine;

class PartSound : PartModule
{
public FXGroup MySound = null; // Make sure this is public so it can be initialised internally.
private bool _playSound = false;

/// <summary>
/// Called during the Part startup.
/// StartState gives flag values of initial state
/// </summary>
public override void OnStart(StartState state)
{
if (state == StartState.Editor || state == StartState.None) return; // Don't play sounds in the editor view.

MySound.audio = gameObject.AddComponent<AudioSource>();
MySound.audio.volume = GameSettings.SHIP_VOLUME;
MySound.audio.Stop();

// Be sure not to include the file extension of the sound here.
// Unity can play WAV and OGG, possibly some others, but not MP3.
MySound.audio.clip = GameDatabase.Instance.GetAudioClip("folderUnderGameData/SoundFileName");
MySound.audio.loop = true; // Repeat the sound from the start if we reach the end of the file. Use this for engine sounds etc.

base.OnStart(state); // Allow OnStart to do what it usually does.
}


/// <summary>
/// Per-frame update
/// Called ONLY when Part is ACTIVE!
/// </summary>
public override void OnUpdate()
{
if (_playSound)
{
if (!MySound.audio.isPlaying)
MySound.audio.Play();
}
else
MySound.audio.Stop();

base.OnUpdate();
}

// Add a right-click option to the part to play the sound.
[KSPEvent(guiActive = true, guiName = "Play sound")]
public void PlaySound()
{
_playSound = true;

// Make sure the quoted text matches the method names.
Events["PlaySound"].active = false; // Disable the "Play sound" right-click option.
Events["StopSound"].active = true; // Enable the "Stop playing sound" right-click option.
}

// Add a right-click option to the part to stop playing the sound.
[KSPEvent(guiActive = true, guiName = "Stop playing sound")]
public void StopSound()
{
_playSound = false;

Events["PlaySound"].active = true; // Enable the "Play sound" right-click option.
Events["StopSound"].active = false; // Disable the "Stop playing sound" right-click option.
}
}

Once you have the DLL compiled and in a directory under GameData, add it to whatever part you want by adding before the last '}' in the file:


MODULE
{
name = PartSound
}

If you need anything else, just ask :)

Edited by pizzaoverhead
Link to comment
Share on other sites

Here, have some code:


using UnityEngine;

class PartSound : PartModule
{
public FXGroup MySound = null; // Make sure this is public so it can be initialised internally.
private bool _playSound = false;

/// <summary>
/// Called during the Part startup.
/// StartState gives flag values of initial state
/// </summary>
public override void OnStart(StartState state)
{
if (state == StartState.Editor || state == StartState.None) return; // Don't play sounds in the editor view.

MySound.audio = gameObject.AddComponent<AudioSource>();
MySound.audio.volume = GameSettings.SHIP_VOLUME;
MySound.audio.Stop();

// Be sure not to include the file extension of the sound here.
// Unity can play WAV and OGG, possibly some others, but not MP3.
MySound.audio.clip = GameDatabase.Instance.GetAudioClip("folderUnderGameData/SoundFileName");
MySound.audio.loop = true; // Repeat the sound from the start if we reach the end of the file. Use this for engine sounds etc.

base.OnStart(state); // Allow OnStart to do what it usually does.
}


/// <summary>
/// Per-frame update
/// Called ONLY when Part is ACTIVE!
/// </summary>
public override void OnUpdate()
{
if (_playSound)
{
if (!MySound.audio.isPlaying)
MySound.audio.Play();
}
else
MySound.audio.Stop();

base.OnUpdate();
}

// Add a right-click option to the part to play the sound.
[KSPEvent(guiActive = true, guiName = "Play sound")]
public void PlaySound()
{
_playSound = true;

// Make sure the quoted text matches the method names.
Events["PlaySound"].active = false; // Disable the "Play sound" right-click option.
Events["StopSound"].active = true; // Enable the "Stop playing sound" right-click option.
}

// Add a right-click option to the part to stop playing the sound.
[KSPEvent(guiActive = true, guiName = "Stop playing sound")]
public void StopSound()
{
_playSound = false;

Events["PlaySound"].active = true; // Enable the "Play sound" right-click option.
Events["StopSound"].active = false; // Disable the "Stop playing sound" right-click option.
}
}

Once you have the DLL compiled and in a directory under GameData, add it to whatever part you want by adding before the last '}' in the file:


MODULE
{
name = PartSound
}

If you need anything else, just ask :)

That's super helpful, still trying to understand it though, lol.

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