NMLTutorial/Train

From TTWiki
< NMLTutorial
Revision as of 20:28, 30 August 2011 by Hirundo (talk | contribs) (spelling)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

The example used here is from the NML source. The code for this was originally written in NFO by DJNekkid for the 2cc Trainset and rewritten in NML by Hirundo. The graphics used in the example are by Purno. Code and graphics are both licensed according to the GPL v2 or later. The code has been modified for the purpose of this tutorial


Introduction

This example will show you how to make a train. This particular train is the Dutch ICM that exists as a three and four part multiple unit. In this example we'll first make a one-part train. If you want to make such a train yourself, you can stop there. Then we'll continue making this into a three-part multiple unit. This technique can also be used for steam trains with a tender. If that's all you need, you can stop here. If you want to see some more callback applications, continue where we'll make this train refittable to a four-part multiple unit. Lastly, a parameter option will be added to select if you want this train in 1cc, 2cc or real colours.

Some knowledge from the the previous examples is assumed. The most basic things will not be explained in detailed, you must apply the rule that everything must be defined before it can be referenced yourself and that you know how to link callbacks to switch blocks. If you're still uncomfortable with those things, go back and try the road vehicle and tram example (again). It's best if you at least have made one NewGRF in NML yourself (either using the examples or one of your own) before embarking on this train example. This example uses some of the more advanced features of NML, which you may not understand if you don't understand the basics yet.


Folder structure

For this example we'll assume the following folder structure. There will be one language file in the lang folder, one graphics file in the gfx folder. These two folders sit together with the main NML file in one project folder. We'll not be using custom_tags.txt this time:

tram_example
 |- lang
 |   |- english.lng
 |- gfx
 |   |- icm.png
 |- example_train.nml


Language files

Only English as default language with NewGRF name and description already defined. More things will be added to the language file as we go.

##grflangid 0x01

STR_GRF_NAME                 :NML Example NewGRF: Train
STR_GRF_DESC                 :{ORANGE}NML Example NewGRF: Train{}{BLACK}This NewGRF is intended to provide a coding example for the high-level NewGRF-coding language NML.{}Original graphics by {SILVER}Purno, {BLACK}coding by {SILVER}DJNekkid.{}{BLACK}This NewGRF defines a Dutch EMU, the ICM 'Koploper'.


NML file

Start the NML file by adding a grf block.

/* Define grf */
grf {
    grfid: "NML\00";
    /* GRF name and description strings are defined in the lang files */
    name: string(STR_GRF_NAME);
    desc: string(STR_GRF_DESC);
    /* This is the first version, start numbering at 0. */
    version: 0;
    min_compatible_version: 0;
}


When working with trains, a railtypetable is a useful means to have the train on the correct railtype, regardless of track sets loaded by the user.


NML Tutorial: Train