Setting Up the Asset.

From TrainzOnline
(Difference between revisions)
Jump to: navigation, search
(Added link to previous tutorial)
Line 118: Line 118:
  
 
When working with script it's usually advisable to add short sections of code and test them out thoroughly. Even though it will mean frequently restarting Trainz, it will be quicker to do this in the end. There are very few facilities for debugging and you can be reasonably sure that any errors or problems that you find will be connected to the last change you made. Don't forget to check the compilation before restarting Trainz, this will save you a lot of aggravation.<br><br>
 
When working with script it's usually advisable to add short sections of code and test them out thoroughly. Even though it will mean frequently restarting Trainz, it will be quicker to do this in the end. There are very few facilities for debugging and you can be reasonably sure that any errors or problems that you find will be connected to the last change you made. Don't forget to check the compilation before restarting Trainz, this will save you a lot of aggravation.<br><br>
Next Tutorial: [[Automating Animations.]]<br>
+
Previous Tutorial: [[Saving_and_Restoring_Data,_Using_Properties.]]  Next Tutorial: [[Automating Animations.]]<br>

Revision as of 16:03, 31 May 2016

Now we will examine the creation of a simple scripted asset and will deal with managing messages sent by other objects, with handling animations, corona and name effects and with playing sound files via script. We will be setting up methods to react automatically to events in the Trainz environment and we will be scripting a simple interface to allow the user to edit some of the object's properties.

We will need a simple mesh asset to hang all of this script on and here is the asset we will be using. In case it's difficult for you to tell, this is an engine shed with animated opening doors, a flashing light and a name written on the pediment above. If you want to texture it properly and add windows and drainpipes, be my guest. It will have a roof mesh which can be hidden, so that we can see what is going on inside, and we will be arranging for bells to ring when the doors open.

Since the asset will need attached tracks and a scenery trigger we will make it as a kind buildable. The shed has been designed to suit diesel engines or railcars about twenty metres in length, steam engines with tenders or articulated trams introduce some complications so leave those for later.

The shed is being set up so that an engine can be issued with a Drive command and sent towards the shed. The doors will open on the train's approach and close as it draws to a halt, all without user intervention. You will need to back out very slowly and under human control.

Here is a diagram of the building with the dimensions that I used to build my version. All of the X dimensions are zero and the width and height of the shed itself are not particularly important as long as there is room for the train. The two helper points above the doors are a.corona and a.name, you might need to refer to the CCG to get the orientation right for the name effect.

Engine Shed.jpg

You will need to create this, or something similar, in Max and animate the front doors, I used 50 frames. Each door should be linked to its respective dummy and everything else, including the two door dummies themselves, should be linked to b.r.main at the centre of the object. Make sure that a.tracka1 and a.tracka3 are aligned as shown, the alignment of a.tracka2 doesn't matter.

When you have built the main shed you will need to make an entirely separate roof mesh, exported as roof.im.

If you prefer you can click on this link to download a zip file containing all of the files you will need (except for config.txt and Tutorial.gs which you should copy and paste from these pages) The gmax source is also included, although it's not my best model (NOTE: You will only see the roof when opened in GMAX, you must "unhide all" to see the entire model). Unzip the download to a folder on your hard drive, add your .txt and .gs files and drop the folder into CMP. For Blender users, you can find the equivalent file here

Here is the config.txt with one or two comments added (which cmp will not allow you to retain).

kuid                          <ADD YOUR OWN KUID>
username                      "Engine Shed Tutorial"
kind                          buildable
trainz-build                  2.5

light                         1

category-region               "00"
category-era                  "2000s"
category-class                "TF"

script                        Tutorial
class                         Tutorial

mesh-table {
   default {
      mesh                    "shed.im"
      anim                    "shed.kin"
      auto-create             1
      effects {
         corona {
            kind              corona
            att               "a.corona"
            texture-kuid      <kuid:-3:10110>
            frequency         2
            directional       0
            object-size       0.15
         }
         name {
            kind              name
            fontsize          0.5 
            fontcolor         160,160,160
            att               "a.name"
            name
         }
         masonry {
            kind             "texture-replacement"
            texture          "brick.texture"
         }
      }
   }
   roof {
      mesh                    "roof.im"
      auto-create             0
   }
}

// There are two attached track segments, the front segment will auto-update when you attach
// your track to it, the second will remain invisible and is only present because trains under
// a drive command will not go to the end of a track. You may need to adjust the position of
// a.tracka3 to get the train to stop in the right place 

attached-track {
  track1 {
     track                   <kuid:-10:137>
     useadjoiningtracktype   1
     vertices {
        0                    "a.tracka1"
        1                    "a.tracka2"
     }
  }
  track2 {
     track                   <kuid:-10:137>
     useadjoiningtracktype   0
     vertices {
        0                    "a.tracka2"
        1                    "a.tracka3"
     }
  }
}

// This is the trigger which detects the approaching train. Since it has a track assignment
// it will ignore trains on any parallel tracks in the same object.  You will find this a useful 
// device if you decide to turn this into a multi track shed.

attached-trigger {
   trigger1 {
      att                     "a.tracka1"
      radius                  25
      track                   "track1"
   }
}

Once you have all of this done create a script file called Tutorial.gs and add the following code. This does nothing other than turning off the corona. We will be adding to it in steps.

include "Buildable.gs"

class Tutorial isclass Buildable {

   public void Init(void) {
     inherited();
     SetFXCoronaTexture("corona",null);
   }

};

Now you should load the asset via CMP, commit it and make sure that it is error free and shows up correctly in the game. Don't worry that the roof is invisible, this is by design.

When working with script it's usually advisable to add short sections of code and test them out thoroughly. Even though it will mean frequently restarting Trainz, it will be quicker to do this in the end. There are very few facilities for debugging and you can be reasonably sure that any errors or problems that you find will be connected to the last change you made. Don't forget to check the compilation before restarting Trainz, this will save you a lot of aggravation.

Previous Tutorial: Saving_and_Restoring_Data,_Using_Properties. Next Tutorial: Automating Animations.

Personal tools