Jump to content

Need new and more experienced eyes to take a look - what am I doing wrong?


Recommended Posts

I had really hoped to get this myself, but after getting this to (I thought) work properly, I find that it sometimes works as intended, sometimes refires the sound over and over and over forever and never lets the ports actually dock... :/

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


namespace DockingSounds
{
class DPSoundFX : PartModule
{
[KSPField]
public string sound_docking = "DPSoundFX/Sounds/dock";
[KSPField]
public string sound_undocking = "DPSoundFX/Sounds/undock";
public FXGroup dockSound = null;
public FXGroup undockSound = null;

public bool createGroup (FXGroup group, string name, bool loop)
{
if (name != string.Empty) {
if (!GameDatabase.Instance.ExistsAudioClip (name))
return false;
group.audio = gameObject.AddComponent<AudioSource> ();
group.audio.volume = GameSettings.SHIP_VOLUME;
group.audio.rolloffMode = AudioRolloffMode.Logarithmic;
group.audio.dopplerLevel = 0f;
group.audio.panLevel = 1f;
group.audio.clip = GameDatabase.Instance.GetAudioClip (name);
group.audio.loop = loop;
group.audio.playOnAwake = false;
return true;
}
return false;
}


public void DPFXdock (GameEvents.FromToAction<Part, Part> partAction)
{
//Debug.LogError ("[DPSoundFX] Docking");
//Debug.LogError (" Docked FROM : " + partAction.from.vessel.vesselName);
//Debug.LogError (" Docked TO : " + partAction.to.vessel.vesselName);
//Debug.LogError (" Docked FROM ID: " + partAction.from.vessel.id.ToString ());
//Debug.LogError (" Docked TO ID : " + partAction.to.vessel.id.ToString ());


Debug.LogError ("[DPSoundFX] Playing: " + sound_docking);
if (!dockSound.audio.isPlaying){
dockSound.audio.Play ();
}
}

public void DPFXundock (Part part)
{
print ("[DPSoundFX] Undocking " + part.vessel);
Debug.LogError ("[DPSoundFX] Playing: " + sound_undocking);
undockSound.audio.Play ();
}

public override void OnStart (PartModule.StartState state)
{
Debug.LogError ("[DPSoundFX] OnStart Called: State is " + state);

if (HighLogic.LoadedScene != GameScenes.FLIGHT)
return;

base.OnStart (state);

if (createGroup (undockSound, sound_undocking, false)) {
GameEvents.onPartUndock.Add (DPFXundock);
} else {
Debug.LogError ("[DPSoundFX]ERROR - file " + sound_undocking + ".* not found!");
}
if (createGroup (dockSound, sound_docking, false)) {
GameEvents.onPartCouple.Add (DPFXdock);
} else {
Debug.LogError ("[DPSoundFX]ERROR - file " + sound_docking + ".* not found!");
}

Debug.LogError ("[DPSoundFX] OnStart Executed: State was " + state);
}
}
}

When it goes wrong, I get NRE's in the log (below) there's only one isPlaying conditional statement because I removed them at one point as a test and then replaced the one for docking on my last try. All it did was change it from a machingun buzz of repeated playbacks to one after another after another...

Output Log

I'm sure this is just a matter of me being stupid, or knowing just enough to get myself into trouble, as I've never taken a single class of any kind on anything beyond BASIC programming (as in BASIC language, I'm 45)

Edited by tg626
Link to comment
Share on other sites

Well for one, that's going to add the DPFXundock once for every part that has the module every time you enter the flight scene (so if you have 5 parts with the module active, the function will get called 5 times for each time you enter the flight scene on an undock event). Try the KSPAddon type of plugin for which there is only a single instance per scene (simple example), and remove the event in OnDestroy so you only ever have a single event trigger

Edited by Crzyrndm
Link to comment
Share on other sites

So there is on instance of my code running for every docking port in the scene? (It's added to all ports via module manager).

And every instance if the code is called when any port is docked or undocked?

Beyond that, having no idea how to do what you suggested ( even with the example) just proves how out of my depth I am!!! :D

Link to comment
Share on other sites

Dammit, I've confused myself. I was wrong, each event belongs to an instance of the module so the function wouldn't be called multiple times if you only entered the scene once. Addon type plugin also won't work because you need the part location.

Start again from the beginning.

Event subscription in OnStart. Check. That's fine because the callback is specific to the module. You need one for each module so the sound gets played in the correct location

Event subscription removed in OnDestroy(). Not happening. If you enter the flight scene, leave it, and then come back, that original event is still floating around waiting to fire (with a null function to call because the part module instance it was attached to is dead

void OnDestroy()
{
GameEvents.On*Dock.Remove(<func>);
}

Event callback checks to see if either of the parts that are undocking are the one it is attached to. Also not happening. The callback will fire for all subscribers, not just the ones attached to parts involved in the event.

if (!dockSound.audio.isPlaying && [B]partAction.part = this.part[/B]) // or w/e the part lookup is in the event parameter
{
dockSound.audio.Play ();
}

My bad for making things confusing ;)

EDIT

And now I'm really confused, if the first one worked...

Edited by Crzyrndm
Link to comment
Share on other sites

OMG! That was IT (I think... it worked just now, but it's done that before and then goobered up on me when I restarted the game later, so...)

Here's what I did based on your advice (that last post I was able to follow LOL)

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


namespace DockingSounds
{
class DPSoundFX : PartModule
{
[KSPField]
public string sound_docking = "DPSoundFX/Sounds/dock";
[KSPField]
public string sound_undocking = "DPSoundFX/Sounds/undock";
static bool eventAddedDocking = false;
static bool eventAddedUndocking = false;
static bool groupCreatedDocking = false;
static bool groupCreatedUndocking = false;
public FXGroup dockSound = null;
public FXGroup undockSound = null;

public bool createGroup (FXGroup group, string name, bool loop)
{
if (name != string.Empty) {
if (!GameDatabase.Instance.ExistsAudioClip (name)) {
Debug.LogError ("[DPSoundFX]ERROR - file " + name + ".* not found!");
return false;
}
group.audio = gameObject.AddComponent<AudioSource> ();
group.audio.volume = GameSettings.SHIP_VOLUME;
group.audio.rolloffMode = AudioRolloffMode.Logarithmic;
group.audio.dopplerLevel = 0f;
group.audio.panLevel = 1f;
group.audio.clip = GameDatabase.Instance.GetAudioClip (name);
group.audio.loop = loop;
group.audio.playOnAwake = false;
return true;
}
return false;
}


public void DPFXdock (GameEvents.FromToAction<Part, Part> partAction)
{
//Debug.LogError ("[DPSoundFX] Docking");
//Debug.LogError (" Docked FROM : " + partAction.from.vessel.vesselName);
//Debug.LogError (" Docked TO : " + partAction.to.vessel.vesselName);
//Debug.LogError (" Docked FROM ID: " + partAction.from.vessel.id.ToString ());
//Debug.LogError (" Docked TO ID : " + partAction.to.vessel.id.ToString ());


Debug.LogError ("[DPSoundFX] Playing: " + sound_docking);
if (!this.dockSound.audio.isPlaying) {
this.dockSound.audio.Play ();
}
}

public void DPFXundock (Part part)
{
print ("[DPSoundFX] Undocking " + part.vessel);
Debug.LogError ("[DPSoundFX] Playing: " + sound_undocking);
if (!this.dockSound.audio.isPlaying) {
this.undockSound.audio.Play ();
}
}

public override void OnStart (PartModule.StartState state)
{
Debug.LogError ("[DPSoundFX] OnStart Called: State is " + state);

if (HighLogic.LoadedScene != GameScenes.FLIGHT)
return;

base.OnStart (state);

groupCreatedDocking = createGroup (dockSound, sound_docking, false);
groupCreatedUndocking = createGroup (undockSound, sound_undocking, false);


if (groupCreatedDocking && !eventAddedDocking) {
GameEvents.onPartCouple.Add (DPFXdock);
eventAddedDocking = true;
}


if (groupCreatedUndocking && !eventAddedUndocking) {
GameEvents.onPartUndock.Add (DPFXundock);
eventAddedUndocking = true;
}

Debug.LogError ("[DPSoundFX] OnStart Executed: State was " + state);
}
}
}

- - - Updated - - -

%^&%*^@# it all...

Still getting:

NullReferenceException  at (wrapper managed-to-native) UnityEngine.AudioSource:get_isPlaying ()


at DockingSounds.DPSoundFX.DPFXdock (FromToAction`2 partAction) [0x00000] in <filename unknown>:0


at EventData`1[GameEvents+FromToAction`2[Part,Part]].Fire (FromToAction`2 data) [0x00000] in <filename unknown>:0


at Part.Couple (.Part tgtPart) [0x00000] in <filename unknown>:0


at ModuleDockingNode.DockToVessel (.ModuleDockingNode node) [0x00000] in <filename unknown>:0


at ModuleDockingNode.<SetupFSM>m__166 () [0x00000] in <filename unknown>:0


at KerbalFSM.RunEvent (.KFSMEvent evt) [0x00000] in <filename unknown>:0


at KerbalFSM.updateFSM (KFSMUpdateMode mode) [0x00000] in <filename unknown>:0


at KerbalFSM.UpdateFSM () [0x00000] in <filename unknown>:0


at ModuleDockingNode.Update () [0x00000] in <filename unknown>:0

(Filename: Line: -1)

in the log, only now I get NO sound and no docking, they are just sticking to each other "magnetically"... In the first test, I was getting the sound, using 2 rovers with ports. Then I launched 2 capsules and I get this magneto thing, and no sound.

I know darn well there's no voodoo in coding, but it sure seems like it's possessed!!! :huh:

Edited by tg626
Reformatted code
Link to comment
Share on other sites

Ok... I'll add the onDestroy bit - I forgot about it honestly but that won't happen until tomorrow evening, gotta sleep now and work in the am.

This is going to nag my mind all day too... :sticktongue:

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