I'm creating a train using the Rail Pallette. I've created a train agent and several different types of Rail Car agents. I've gone through the Hump Yard Tutorial; this details how to create a randomly generated train with several types of rail cars.
I need to create a train with several different rail cars in a deterministic order. I imagine a createUniform()
function that takes the classes and creates the cars in the order given until the train length is reached.
I've tried several different avenues. The closest is creating my own createUniform()
function, but I am not able to return the different car types as their classes don't match. I've attempted to have it return Agent
but this still results in an error: cannot cast OpenCar to Agent
.
Is there a way to create a parent class for all my cars so that they can be returned from one function?
If you are using the TrainSource block, under the New rail car section you can switch to code instead of the agent picker. They provide you with a carindex
parameters, which you can use to define the car. This can be used with the ternary operator if you don't want to write an special function.
For example:
(carindex < 2)?
new Locomotive() :
(carindex < 10) :
new HopperCar() :
new ContainerCar()
In this case, the first two cars (carindex
0 and 1) will be Locomotives, then carindex
2 to 9 will be HopperCars, and 10 onwards will be ContainerCars.