Array-out-of-bounds Exception

ffccnn

New member
Have you seen this error?



Looking at train.gs, line 2910, this is the statement that caused the exception:

bool HasTrainBeforeJunction(JunctionBase junction)
{
Vehicle frontVehicle = GetVehicles()[0];

This is the kind of code that you would write and cross your fingers hoping it would never fail. There is a good reason to assume that. Any standalone vehicle is by itself a train and a train’s GetVehicles() should never return an empty list because the vehicle by itself is the only one in the list (if I am wrong please someone at Auran correct me). However, in the real world and according to Murphy’s Law, it could happen as shown in the picture. I don’t know where or why in their native and secret code that may happen.

If I were sitting at Auran’s I would write a more defensive code:

bool HasTrainBeforeJunction(JunctionBase junction)
{
// @begin ffccnn fix
Vehicle[] vehicles = GetVehicles();
if(vehicles == null or vehicles.size() == 0)
{
return false; // no train in front of junction
}
//Vehicle frontVehicle = GetVehicles()[0];
Vehicle frontVehicle = vehicles[0];
// @end ffccnn fix

Sometimes game programmers don’t get that luxury because they are under tight schedules and budges. Maybe they expect us, the users, to do their QA work.

I haven’t seen that exception anymore since I put my fix in place (hopefully :eek: ).

Cheers,

FFCCNN
 
Back
Top