As you might have seen while running your current model, we had numerous rail cars differed by colors only. Phase 3 will be devoted to defining different types of the rail cars.
We provide you with the handy Rail Car Type creation Wizard. Simply specify the name of the new rail car type and choose the animation from the list of ready-to-use 3D objects.
Switch to the code field by clicking on the
icon which will turn into
allowing you to insert the code in the field right next to it. Type the expression as shown in the figure below.
We use conditional operator to set the railcar type for the rail cars of the train being created. We want to diversify railcars of our freight train. The first railcar should be of type Locomotive. Successive railcars can be of any other railcar type.
When TrainSource creates a new train, it successively calls the expression specified in the New rail car field for every railcar of the train. Using the local variable carindex we pass the index of the currently created railcar to the expression.
The conditional operator allows you to choose between two given alternatives depending on the value of a Boolean expression. In our example, if the carindex == 0 expression evaluates to true, then the current railcar is the first car of the train and we set its type to Locomotive. Otherwise, the expression evaluates to false and for the successive railcars we call the randomlyCreate() function and provide the classes of the railcar types to randomly choose the types for other railcars in the train.
As a result, we will have a train with a loco as the first car, followed by ten rail cars of our custom rail car types.
Reference model: Hump Yard - Phase 3