Board index Roller Coaster Games NoLimits Coaster 2 NoLimits Coaster 2 Scripting Traffic Light Script

Traffic Light Script

Post your Nolimits 2 scripts here! Also any discussion regarding scription for NoLimits Coaster 2 should be placed in this forum

Post September 1st, 2016, 10:15 am

Posts: 8
Points on hand: 157.00 Points
Hello! I'm new on this forum.
I wanted to ask you how can I create a script for a traffic light on my roller coasters.

Thank You!
Bye.

Post September 1st, 2016, 11:17 am

Posts: 8144
Points on hand: 13,491.00 Points
Welcome! Our members can help you do that. :)
-- I was happy to be with NL1 - [:')] --

Post September 1st, 2016, 11:57 am

Posts: 622
Points on hand: 6,144.00 Points
Not to be rude but this is the theme park news and construction section of the forum! :)
Taron>Helix get mad

Post September 1st, 2016, 12:33 pm

Posts: 8
Points on hand: 157.00 Points
Ops...excuse me.
But...How i can create a Traffic Light on a roller coaster???

Post September 1st, 2016, 12:47 pm
herman116 User avatar
Premium Member
Premium Member

Posts: 636
Points on hand: 7,471.00 Points
Location: The Netherlands
If you want to make a traffic light, you should create a traffic light 3D model, after it's created you'll have to attach lights to it in the NoLimits editor and then you'll have to make a script that will switch the lights on or off. ;)

Post September 1st, 2016, 2:23 pm
TTD03 User avatar
True Addicts
True Addicts

Posts: 7306
Points on hand: 738.00 Points
Bank: 111,265.11 Points

Post September 1st, 2016, 2:31 pm

Posts: 8
Points on hand: 157.00 Points
Ok. Thank You.
My problem is: how can I create a script???

Post September 1st, 2016, 2:44 pm

Posts: 8
Points on hand: 157.00 Points
I need a tutorial to create a traffic light script

Post September 2nd, 2016, 11:47 am
herman116 User avatar
Premium Member
Premium Member

Posts: 636
Points on hand: 7,471.00 Points
Location: The Netherlands
If you allready have a 3D model and have the lights configured in NoLimits, it's actually pretty easy. Below is a small script which I made to turn on the lights of my coaster The Big Race. I made it a bit simpler so it's easier to understand but if you're not having any problems understanding this, maybe you could take a look at the original script.

public class LightScript
{
   private SceneObjectLight redLight;
   private SceneObjectLight yellowLight;
   private SceneObjectLight greenLight;
   
   public void Init(Simulator sim)
   {
      SceneObject light = sim.getSceneObject("Light");
      redLight = light.getLightForName("red");
      yellowLight = light.getLightForName("yellow");
      greenLight = light.getLightForName("green");
   }
   
   public void Red()
   {
      redLight.setEnabled(true);
   }
   
   public void Yellow()
   {
      redLight.setEnabled(false);
      yellowLight.setEnabled(on);
   }
   
   public void Green()
   {
      yellowLight.setEnabled(false);
      greenLight.setEnabled(on);
   }
   
   public void Off()
   {
      greenLight.setEnabled(false);
   }
}


In the init method, it will search for an object called "light". In this object, it searches for three lights called "red", "yellow" and "green".

When the function Red() is called, the red light will go on.
When the function Yellow() is called, the red light will go off and the yellow light will go on.
When the function Green() is called, the yellow light will go off and the green light will go on.
When the function Off() is called, the green light will go off.

You'll have to make something that will trigger the functions in the right time though. At my coaster I called the Red() function when the car arrived at the launch section, the Yellow() function when the car went backward a small bit and the green light when the coaster launched. The Off() function was called a second after the launch was started.

Post September 11th, 2016, 2:56 am

Posts: 8
Points on hand: 157.00 Points

Post September 11th, 2016, 4:03 am
herman116 User avatar
Premium Member
Premium Member

Posts: 636
Points on hand: 7,471.00 Points
Location: The Netherlands

Post September 13th, 2016, 9:33 am

Posts: 8
Points on hand: 157.00 Points
Help me!!!!! I've an another problem!!!
How can i set up the lights and the script on they in the traffic light mode??????

Post September 13th, 2016, 10:19 am

Posts: 8144
Points on hand: 13,491.00 Points
^ Herman just above you, will help you again! Thanks for posting! :D
-- I was happy to be with NL1 - [:')] --

Post September 13th, 2016, 11:12 am
herman116 User avatar
Premium Member
Premium Member

Posts: 636
Points on hand: 7,471.00 Points
Location: The Netherlands
Do you want to run them in a cycle? Or on a specific moment like a launch? If you want them to change on a specific moment, when should it be green, yellow and red?

Post September 14th, 2016, 2:05 pm

Posts: 8
Points on hand: 157.00 Points
I want run them in a specific moment (as the launch).

Post September 14th, 2016, 2:06 pm

Posts: 8
Points on hand: 157.00 Points
And... How can i set the lights in the traffic light model?

Post September 15th, 2016, 1:49 pm
herman116 User avatar
Premium Member
Premium Member

Posts: 636
Points on hand: 7,471.00 Points
Location: The Netherlands
This can become quite complicated if you don't have much experience with scripting. The script below will change the traffic light from green, to yellow, to red and back to green again, to yellow again etc.. This will be done every 2000 milliseconds (2 seconds) this time can be changed in the top of the script. The function onNextFrame() should be called every frame. Most object scripts need this function so that it can update itself.

In the onNextFrame() function, the program will look in which state it currently is (green, red or yellow) and will call the functions from the traffic light script. The function will also call the wait() function which will check if the time is over to switch to another light. It will save the current time in its first call and will check every frame if the saved time + the time it should wait is bigger than the now current time.

private static final double TimeRed = 2000;
private static final double TimeYellow = 2000;
private static final double TimeGreen = 2000;

private static final int STATE_GREEN = 0;
private static final int STATE_YELLOW= 1;
private static final int STATE_RED = 2;

private double TimeStartDelay = 0;

private byte state = STATE_GREEN;

// Put the traffic light code in here

public void onNextFrame(float tick)
{
  switch (state)
  {
    case STATE_GREEN:
      Green();
      if (wait(TimeGreen))
      {
        state = STATE_YELLOW;
      }
      break;

    case STATE_YELLOW:
      Yellow();
      if (wait(TimeYellow))
      {
        state = STATE_Red;
      }
      break;

    case STATE_Red:
      Red();
      if (wait(TimeRed))
      {
        state = STATE_Green;
      }
      break;
  }
}

private bool wait(double time)
{
  if (TimeStartDelay == 0) {
    TimeStartDelay = System.currentTimeMillis();
  }
  if (TimeStartDelay + time < System.currentTimeMillis()) {
    TimeStartDelay = 0;
    return true;
  }
  return false;
}


The easiest way to add a track trigger for you light is by looking into the door script. This shows how the door looks up the track 5 meters in front of the object and checks if it sees a train. I've copied the most important parts down here.

  private TrackTrigger beforeDoorTrigger;
  private TrackTrigger behindDoorTrigger;
 
  private static final String scriptName = "DoorScript";
 
  public bool onInit()
  {
    sco = sim.getSceneObjectForEntityId(getParentEntityId());
    if (sco == null)
    {
      System.err.println(scriptName + ": the script is not part of a scene object");
      return false;
    }

    Vector3f pos = new Vector3f();

    sco.getTranslation(pos);

    final float range = 10;

    // create triggers...
    TrackPos trackPos = sim.findNearestCoasterTrack(pos, range);
    if (trackPos == null)
    {
      System.err.println(scriptName + ": no coaster found within a range of "+ range + " meters");
      return false;
    }
    else
    {
      Coaster coaster = trackPos.getCoaster();
     
      System.out.println(scriptName + ": Coaster found: " + coaster);

      System.out.println("Coaster Style is: " + coaster.getCoasterStyleId());
     
      // create a trigger 5 meters in front of the track pos
      beforeDoorTrigger = TrackTrigger.createTrackTriggerAtOffset(trackPos, -5.0);
     
      // create a trigger 5 meters after the track pos
      behindDoorTrigger = TrackTrigger.createTrackTriggerAtOffset(trackPos, +5.0);
     
      // tell the triggers that they should call our onTrainEntering/onTrainLeaving methods
      // We have to implement the TrackTriggerListener interface
      beforeDoorTrigger.addTrackTriggerListener(this);
      behindDoorTrigger.addTrackTriggerListener(this);
    }

    return true;
  }

  /**
   * This method is part of the TrackTriggerListener Interface implementation
   *
   * It will be called if the interface is registered for a trigger using the
   * addTrackTriggerListener method.
   */
  public void onTrainEntering(TrackTrigger trigger, Train train)
  {
    if (trigger == beforeDoorTrigger)
    {
      //train is seen before the door
    }
    else if (trigger == behindDoorTrigger)
    {
      //train is seen behind the door
    }
  }


When it sees the train, you could change the trafficlight state to STATE_GREEN and let it run the cycle once.

Post September 16th, 2016, 10:33 pm

Posts: 8144
Points on hand: 13,491.00 Points
^ Thanks for making impressive and informative posts, always! :)
-- I was happy to be with NL1 - [:')] --


Return to NoLimits Coaster 2 Scripting

 


  • Related topics
    Replies
    Views
    Last post