Skip to content

Commit 729e0b7

Browse files
committed
Changes to reduce the risk of null reference exceptions in edge cases
1 parent f19642e commit 729e0b7

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

Source/RunActivity/Viewer3D/Lights.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ bool UpdateState()
287287
// Should prevent some unneeded computation, but is a little messy. May revise in the future
288288

289289
// Headlight
290-
int newTrainHeadlight = !Car.Lights.IgnoredConditions[0] ? (Car.Train != null ? Car.Train.TrainType != Train.TRAINTYPE.STATIC ? leadLocomotiveCar.Headlight : 0 : 0) : 0;
290+
int newTrainHeadlight = !Car.Lights.IgnoredConditions[0] ? (Car.Train?.TrainType != Train.TRAINTYPE.STATIC ? (leadLocomotiveCar != null ? leadLocomotiveCar.Headlight : 2) : 0) : 0;
291291
// Unit
292292
bool locomotiveFlipped = leadLocomotiveCar != null && leadLocomotiveCar.Flipped;
293293
bool locomotiveReverseCab = leadLocomotive != null && leadLocomotive.UsingRearCab;
@@ -320,11 +320,11 @@ bool UpdateState()
320320
// Friction brakes, activation force is arbitrary
321321
bool newBrakeOn = !Car.Lights.IgnoredConditions[9] && Car.BrakeForceN > 250.0f;
322322
// Reverser: -1: reverse, 0: within 10% of neutral, 1: forwards. Automatically swaps if this car is reversed
323-
int newReverserState = !Car.Lights.IgnoredConditions[10] ? ((Car.Train.MUDirection == Direction.N || Math.Abs(Car.Train.MUReverserPercent) < 10.0f) ? 0 :
323+
int newReverserState = (!Car.Lights.IgnoredConditions[10] && Car.Train != null) ? ((Car.Train.MUDirection == Direction.N || Math.Abs(Car.Train.MUReverserPercent) < 10.0f) ? 0 :
324324
Car.Train.MUDirection == Direction.Forward ? 1 : -1) * (Car.Flipped ? -1 : 1) : 0;
325325
// Passenger doors
326-
bool newLeftDoorOpen = !Car.Lights.IgnoredConditions[11] && Car.Train.DoorState(DoorSide.Left) != DoorState.Closed;
327-
bool newRightDoorOpen = !Car.Lights.IgnoredConditions[11] && Car.Train.DoorState(DoorSide.Right) != DoorState.Closed;
326+
bool newLeftDoorOpen = !Car.Lights.IgnoredConditions[11] && Car.Train?.DoorState(DoorSide.Left) != DoorState.Closed;
327+
bool newRightDoorOpen = !Car.Lights.IgnoredConditions[11] && Car.Train?.DoorState(DoorSide.Right) != DoorState.Closed;
328328
// Horn and bell (for flashing ditch lights)
329329
bool newHornOn = !Car.Lights.IgnoredConditions[12] && leadLocomotive != null && leadLocomotive.HornRecent;
330330
bool newBellOn = !Car.Lights.IgnoredConditions[13] && leadLocomotive != null && leadLocomotive.BellRecent;

0 commit comments

Comments
 (0)