Skip to content

Onboard Music controled by triggers

1 to 12 of 12

Hi, I hope someone can help me.

I´m trying to play onboard sounds / music on my coaster using the "Onboard Music" scenery object.
When the train passes the first start trigger the music starts as it should. But when it reaches the stop trigger the music keeps playing.
When the train reaches the second start trigger (that one should start a different .ogg sound file) the first sound file gets restartet again.

This is what I want:

Trigger 1 >>> Start .ogg file A
Trigger 2 >>> Stop .ogg file A
Trigger 3 >>> Start .ogg file B
Trigger 4 >>> Stop .ogg file B

This is what it actually does:

Trigger 1 >>> Start .ogg file A
Trigger 2 >>> nothing, keeps playing file A
Trigger 3 >>> Restart .ogg file A
Trigger 4 >>> nothing, keeps playing file A

Does anybody have an idea how to fix that?

Thx

Hi MrRC,

im using the original script included in NL2.

SCRIPT START

import com.nolimitscoaster.*;

public class OnboardMusic extends Script implements TrackTriggerListener
{

private static final String _errprefix = "Onboard Music";

private SceneObject _sco;

private TrackTrigger _startTrig;
private TrackTrigger _stopTrig;

public bool onInit()
{
String errPrefix = _errprefix;
_sco = sim.getSceneObjectForEntityId(getParentEntityId());
if (_sco == null) return false;

// hide all elements
_sco.setElementsVisible(false);

String scoName = _sco.getName();
if (scoName.length() != 0)
{
  errPrefix = errPrefix + " (" + scoName + ')';
}
errPrefix = errPrefix + ": ";

ResourcePath filePath = _sco.getResourcePathParameter("soundfile");
if (filePath == null)
{
  System.err.println(errPrefix + "No sound file selected"); 
  return false;
}  

System.out.println("Soundfile: " + filePath);

Coaster coaster = _sco.getCoasterParameter("coaster");
if (coaster == null)
{
  System.err.println(errPrefix + "No coaster selected");
  return false;
}

int nNumTrains = coaster.getTrainCount();
if (nNumTrains == 0)
{
  System.err.println(errPrefix + "Coaster has no trains");
  return false;
}

_startTrig = _sco.getTrackTriggerParameter("starttrigger");
if (_startTrig == null)
{
  System.err.println(errPrefix + "No start trigger selected");
  return false;
}

_startTrig.addTrackTriggerListener(this);
 
_stopTrig = coaster.getTrackTrigger("stoptrigger");
if (_stopTrig != null)
{
  _stopTrig.addTrackTriggerListener(this);
}

Integer ivalue = _sco.getIntegerParameter("train");
if (ivalue == null)
{
  System.err.println(errPrefix + "No train index set");
  return false;
}

int trainIndex = ivalue.intValue();

if (trainIndex <= 0 || trainIndex > nNumTrains)
{
  System.err.println(errPrefix + "Invalid train index = " + trainIndex + ", allowed values: 1..."+nNumTrains);
  return false;
}

float gain = 1;

Double dval = _sco.getDoubleParameter("gain");
if (dval != null)
{
  gain = dval.floatValue();
}

 
 
StreamedSound music = StreamedSound.loadFromResource(filePath);
if (music == null)
{
  System.err.println(errPrefix + "Music file cannot be found or opened: '" + filePath + "'");
  return false;
}

music.setGain(gain);
   
Train train = coaster.getTrainAt(trainIndex-1);     
 
train.setOnboardStreamedSound(music);
 
return true;

}

public void onTrainEntering(TrackTrigger trigger, Train train)
{
StreamedSound music = train.getOnboardStreamedSound();
if (music != null)
{
if (trigger == _startTrig)
{
music.stop();
music.play();
}
else if (trigger == _stopTrig)
{
music.stopFaded(5.0f);
}
}
}

public void onTrainLeaving(TrackTrigger trigger, Train train)
{
}

}

SCRIPT END

And I made a little mistake in my last post. The Result is:

Trigger 1 >>> Start .ogg file B
Trigger 2 >>> nothing, keeps playing file B
Trigger 3 >>> Restart .ogg file B
Trigger 4 >>> nothing, keeps playing file B

So every Start Trigger is startimg file B and the stop triggers do nothing.
I Already tried to change "music.stopFaded(5.0f);" ti "music.stop()". But it also didn´t work.

Yes. I know. But even with one start trigger and one stop trigger the music doesn’t stop at the stop trigger.
And I would like to use the script two times in a row in one coaster. Do you think that is possible?
Or does anybody have a script doing what I want?

Yes there is a script that does it for u. I will modify a script for u. I will let u know when its ready. Its 3 pm here now, maybe late this evening.

Posted October 22, 2019· Edited October 22, 2019

public class Onboard_Multi_Music extends Script implements TrackTriggerListener
{
   private static final String soundFile1 = "Launch Sequence.ogg";
   private static final String soundFile2 = "Sound 2.ogg";
   private static final String soundFile3 = "Sound 3.ogg";

   private static final String sound1TriggerName = "LaunchSequence";
   private static final String sound2TriggerName = "Sound 2";
   private static final String sound3TriggerName = "Sound 3";

   private TrackTrigger sound1Trig;
   private TrackTrigger sound2Trig;
   private TrackTrigger sound3Trig;
   
 
   public bool onInit()
   {
     Coaster coaster = sim.getCoasterForEntityId(getParentEntityId());

     sound1Trig = coaster.getTrackTrigger(sound1TriggerName);
     sound1Trig.addTrackTriggerListener(this);
     
     sound2Trig = coaster.getTrackTrigger(sound2TriggerName);
     sound2Trig.addTrackTriggerListener(this);
     
     sound3Trig = coaster.getTrackTrigger(sound3TriggerName);
     sound3Trig.addTrackTriggerListener(this);

     return true;
   }

   public void onTrainEntering(TrackTrigger trigger, Train train)
   {
     
       StreamedSound sound1 = StreamedSound.loadFromFile(soundFile1);
       StreamedSound sound2 = StreamedSound.loadFromFile(soundFile2);
       StreamedSound sound3 = StreamedSound.loadFromFile(soundFile3);
     
       if (trigger == sound1Trig)
       {
    train.setOnboardStreamedSound(sound2);
         sound1.play();
       }
       else if (trigger == sound2Trig)
       {
         train.setOnboardStreamedSound(sound2);
         sound2.play();
       }
       else if (trigger == sound3Trig)
       {
         train.setOnboardStreamedSound(sound3);
         sound3.play();
       }
   }

   public void onTrainLeaving(TrackTrigger trigger, Train train)
   {
   }

}

U might need some tweaking like, soundfile names and triggernames
Let me know if u need help with this.

Hey MrRC,

thanks for the script.

I modified the .nlvm script with my own .ogg-file names and created a NL2Script with the NL2 Editor.

*public class Onboard_Multi_Music extends Script implements TrackTriggerListener
{
private static final String soundFile1 = "Sound 1.ogg";
private static final String soundFile2 = "Sound 2.ogg";
private static final String soundFile3 = "Sound 3.ogg";

private static final String sound1TriggerName = "Sound 1";
private static final String sound2TriggerName = "Sound 2";
private static final String sound3TriggerName = "Sound 3";

private TrackTrigger sound1Trig;
private TrackTrigger sound2Trig;
private TrackTrigger sound3Trig;

public bool onInit()
{
Coaster coaster = sim.getCoasterForEntityId(getParentEntityId());

 sound1Trig = coaster.getTrackTrigger(sound1TriggerName);
 sound1Trig.addTrackTriggerListener(this);
 
 sound2Trig = coaster.getTrackTrigger(sound2TriggerName);
 sound2Trig.addTrackTriggerListener(this);
 
 sound3Trig = coaster.getTrackTrigger(sound3TriggerName);
 sound3Trig.addTrackTriggerListener(this);

 return true;

}

public void onTrainEntering(TrackTrigger trigger, Train train)
{

   StreamedSound sound1 = StreamedSound.loadFromFile(soundFile1);
   StreamedSound sound2 = StreamedSound.loadFromFile(soundFile2);
   StreamedSound sound3 = StreamedSound.loadFromFile(soundFile3);
 
   if (trigger == sound1Trig)
   {
train.setOnboardStreamedSound(sound1);
     sound1.play();
   }
   else if (trigger == sound2Trig)
   {
     train.setOnboardStreamedSound(sound2);
     sound2.play();
   }
   else if (trigger == sound3Trig)
   {
     train.setOnboardStreamedSound(sound3);
     sound3.play();
   }

}

public void onTrainLeaving(TrackTrigger trigger, Train train)
{
}

}*

But there is always an error:

"Execution time: 3ms
Execute Plugin..
Error in File '....\Users\marku\OneDrive\Dokumente\com.nolimitscoaster.nolimits2\TestPark\Onboard_Multi_Music.nlvm' in Line 1: Unknown type: Script
Error in File '....\Users\marku\OneDrive\Dokumente\com.nolimitscoaster.nolimits2\TestPark\Onboard_Multi_Music.nlvm' in Line 1: Unknown type: TrackTriggerListener
...2 error(s), 0 warning(s)"

Then I added

import com.nolimitscoaster.;*

at the beginning of the script and got another error:

*"Execute Plugin..
Executing Plugin...

Exception: NoSuchMethodError: public static void Onboard_Multi_Music.main()
Execution time: 2ms"*

Is that maybe because of a wrong file system structure?

Windows(C:)

users

me

documents

com.nolimitscoaster.nolimits2
That file includes:

  • Onboard_Multi_Music.nl2script
  • Onboard_Multi_Music.nlvm
  • preview-speakr.png
  • sound1.ogg
  • sound2.ogg
  • sound3.ogg
  • TestPark.nl2park
  • TestPark.nl2park_backup

Sorry for my "stupd" questions. But I´m a absolute beginner in scripting.

Okay, I got it.
Sorry for the confusion. I was trying to use the script for a scenery object wich was not possible.
As a script in the coaster properties it works very well.
The only thing is that there is still no possibility to stop the sound by a stop trigger.

Posted October 25, 2019· Edited October 25, 2019

This should work
WARNING : UNTESTED

// Made by MrRC (Sjaak van Horne) 25-10-2019

public class Onboard_Multi_Music extends Script implements TrackTriggerListener
{
private static final String soundFile1 = "Sound 1.ogg";
private static final String soundFile2 = "Sound 2.ogg";
private static final String soundFile3 = "Sound 3.ogg";

private static final String sound1TriggerName = "Sound 1"; // start trigger
private static final String sound2TriggerName = "Sound 2";
private static final String sound3TriggerName = "Sound 3";
private static final String sound4TriggerName = "Sound 1a"; // stop trigger
private static final String sound5TriggerName = "Sound 2a";
private static final String sound6TriggerName = "Sound 3a";

private TrackTrigger sound1Trig;
private TrackTrigger sound2Trig;
private TrackTrigger sound3Trig;
private TrackTrigger sound4Trig;
private TrackTrigger sound5Trig;
private TrackTrigger sound6Trig;

public bool onInit()
{
Coaster coaster = sim.getCoasterForEntityId(getParentEntityId());

 sound1Trig = coaster.getTrackTrigger(sound1TriggerName);
 sound1Trig.addTrackTriggerListener(this);
 
 sound2Trig = coaster.getTrackTrigger(sound2TriggerName);
 sound2Trig.addTrackTriggerListener(this);
 
 sound3Trig = coaster.getTrackTrigger(sound3TriggerName);
 sound3Trig.addTrackTriggerListener(this);

 sound4Trig = coaster.getTrackTrigger(sound4TriggerName);
 sound4Trig.addTrackTriggerListener(this);
 
 sound5Trig = coaster.getTrackTrigger(sound5TriggerName);
 sound5Trig.addTrackTriggerListener(this);
 
 sound6Trig = coaster.getTrackTrigger(sound6TriggerName);
 sound6Trig.addTrackTriggerListener(this);

 return true;

}

public void onTrainEntering(TrackTrigger trigger, Train train)
{

   StreamedSound sound1 = StreamedSound.loadFromFile(soundFile1);
   StreamedSound sound2 = StreamedSound.loadFromFile(soundFile2);
   StreamedSound sound3 = StreamedSound.loadFromFile(soundFile3);
 
   if (trigger == sound1Trig)
   {
	 train.setOnboardStreamedSound(sound1);
     sound1.play();
   }
   if (trigger == sound2Trig)
   {
     train.setOnboardStreamedSound(sound2);
     sound2.play();
   }
   if (trigger == sound3Trig)
   {
     train.setOnboardStreamedSound(sound3);
     sound3.play();
   }
	if (trigger == sound4Trig)
   {
     train.setOnboardStreamedSound(sound1);
     sound1.stop();
   }
	if (trigger == sound5Trig)
   {
     train.setOnboardStreamedSound(sound2);
     sound2.stop();
   }
	if (trigger == sound6Trig)
   {
     train.setOnboardStreamedSound(sound3);
     sound3.stop();
   }

}

public void onTrainLeaving(TrackTrigger trigger, Train train)
{
}

}
1 to 12 of 12