Handling Corona Effects.

From TrainzOnline
Jump to: navigation, search

You will have noticed the call SetFXCoronaTexture("corona",null); in the previous code examples. Corona effects specified in config.txt will be turned on when the object is loaded. This call turns the corona off to begin with since it won't be needed until the doors start to open. To deal with coronas we need to add some code, new sections are in blue.

include "Buildable.gs"

class Tutorial isclass Buildable {

   bool doorsOpen = false;

   public void Init(void) {
      inherited();
      SetFXCoronaTexture("corona",null);
      AddHandler(me,"Object","","ObjectHandler");
   }

/* GetCorona is a utility method that will read the texture-kuid tag from the 
   config.txt of our shed.  When we turned the corona off in Init() the reference 
   to the KUID of the corona was lost and there is no easy way to reset it. This 
   method retrieves the value and returns it as a corona asset. Code which reads data 
   from a Soup, which is the Trainz word for database, is often very convoluted and 
   difficult to follow.  I have split the code into different calls here, and made 
   it a separate method in order to keep it readable, but it could have been written
   as one long statement.*/ 
 
   Asset GetCorona(string mesh, string effect) {
      Soup meshtable = GetAsset().GetConfigSoup().GetNamedSoup("mesh-table");
      Soup effects = meshtable.GetNamedSoup(mesh).GetNamedSoup("effects");
      KUID kuid = effects.GetNamedSoup(effect).GetNamedTagAsKUID("texture-kuid");
      return World.FindAsset(kuid);
   }

/* We can use the same mechanism to control the coronas that we are using to open 
      and close the shed doors, we just need to add the appropriate statements. */ 

   void ObjectHandler(Message msg) {
      Vehicle vehicle = cast<Vehicle>msg.src;
      if (!vehicle) return;
      if (msg.minor == "InnerEnter") {
         doorsOpen = true;
         SetMeshAnimationState("default", true);
         SetFXCoronaTexture("corona",GetCorona("default","corona"));
      }
      else if (msg.minor == "InnerLeave") {
         doorsOpen = false;
         SetMeshAnimationState("default", false);
         SetFXCoronaTexture("corona",null);
      }
   }

};

Accessing texture-kuid requires a lot of words but you should not be misled by this. Soups are very powerful and flexible and you will come across them quite frequently. If you look at the config.txt structure you will see that the method GetCorona() needed to access a value in a database which is nested four levels deep!

Turning them on and off and, if you wish, changing the texture that they use is just about all that you can do with coronas. Size and frequency are preset in config.txt and can't be altered by script.

Next Tutorial: Playing Sounds.

Back to Getting Started

Personal tools