Getting Started in TrainzScript
- In Trainz SP3, script support was added to allow the creation of scripted scenarios (interactive activities.) While existing scenarios are still usable in TS2010, creation of these assets has not been supported by N3V since TRS2006 and future versions of Trainz will drop support for this asset kind entirely.
- Scripts are used to make objects perform operations in reaction to events. For example a level crossing (grade crossing) can be made to operate as a train approaches it.
- Rules are written as scripts, and these rules can be included in Driver sessions, effectively as the session's program elements.
- Driver commands are written as scripts, and these can also be included in Driver sessions so as to give train drivers a series of operations to perform.
Creating and Editing Scripts, Tools to Use
- Trainz scripts need to be included in your asset folder as plain text files with a *.gs file extension.
- Compile-time errors in the script, including spelling or syntax errors, cause the parent asset to be flagged as erroneous by Content Manager.
- Runtime errors won't be flagged up until you load a map which uses your asset or when you first place your asset on a map.
- Although you can use Notepad to create and edit files and commit them directly to the game, it will quickly become frustrating having to run TRS to find that you have missed out a semicolon or closing quote.
- Trainz also includes command-line error checking support through TrainzUtil for situations where the Content Manager GUI is inappropriate.
- Editors which will help you to ensure that your scripts are correctly formatted are freely available, one such is Context and details of how to obtain it and set it up for Trainz are included at this page: Using Context with Trainzscript. Another such program is TextPad (http://www.textpad.com).
The following are links to various tutorial pages available for TrainzScript, these have been updated for TS2012:
A set of tutorials by Andi Smith which will take you through the steps involved in setting up a simple script on a scenery asset, starting with the traditional 'Hello World' and ending with the use of a Property Interface to deal with basic effects, including animation, sounds and texture replacement. The tutorials largely avoid object-oriented terms such as encapulation, inheritance and polymorphism, and instead try to concentrate on getting your scripts working as quickly and painlessly as possible.
- Hello World, Setting Up An Asset Script.
- Getting Information From the Game, Writing Your Own Methods.
- Talking to Yourself, Sending Messages.
- Saving and Restoring Data, Using Properties.
- Setting Up the Asset.
- Automating Animations.
- Handling Corona Effects.
- Playing Sounds.
- Hiding Meshes.
- Handling Name Effects.
- Texture Replacement.
Script Compilation Errors
The follow is a list of compilation errors and possible causes:
Error: Unable to link compiled script class.
Possible cause: The class name in the config.txt class tag is not found in the script identified in the script tag. Generally this is a misspelling.
Error: ...parse error...in line xxx
Possible cause: Perhaps the most common error found during compilation. This means that the compiler was unable to continue because of format errors. Usually the error will be on Trainzscript lines preceding the line number in the error. Look for missing closing curly brackets ( } ), or invalid constructs. The compiler always stops at the first error and errors later in the source file will not be identified. This is why it is always best to add small amounts of code and then check for errors.