Elvas Tower: Crash on switching to autopilot - Elvas Tower

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Crash on switching to autopilot Rate Topic: -----

#1 User is offline   Csantucci 

  • Member, Board of Directors
  • Group: Status: Elite Member
  • Posts: 7,013
  • Joined: 31-December 11
  • Gender:Male
  • Country:

Posted 20 February 2022 - 07:23 AM

See http://www.elvastowe...post__p__282337 . This applies to the actual Testing version.
Following is the result of my analysis.
I generated a PR adding following line to MSTSLocomotive.InitializeMoving()
   EngineBrakeController?.SetPercent(0);


This caused no problems.
I was asked to correct this to
   EngineBrakeController?.InitializeMoving();


This causes a crash when in the player train there is a non-leading locomotive with no controllers set in the .eng file, and when a switch to autopilot is performed.
The crash starts from following line in AITrain.SwitchToAutopilotControl()
                    if (loco.EngineBrakeController != null) loco.SetEngineBrakePercent(0);

The crash occurs in MSTSNotchController.SetPercent(percent), because EngineBrakeController.CurrentNotch was set to 0 by EngineBrakeController.InitializeMoving(), and so within these lines of MSTSNotchControllerSetPercent(percent)
if (CurrentNotch >= 0)
            {
                if (Notches[Notches.Count - 1].Type == ControllerState.Emergency)

Notches[Notches.Count - 1] fails because there are 0 notches.

In the original solution instead CurrentNotch remains to -1 and so the second "if" is not executed.

A fix within MSTSBrakeController.InitializeMoving() is being introduced that checks for the number of notches before setting CurrentNotch to zero.

BTW OR seems to generate controllers with zero notches also for locomotives with no controllers defined in the .eng file.

#2 User is offline   gpz 

  • Superintendant
  • Group: Status: Elite Member
  • Posts: 1,772
  • Joined: 27-October 12
  • Gender:Male
  • Location:Budapest
  • Simulator:OpenRails
  • Country:

Posted 20 February 2022 - 07:54 AM

View PostCsantucci, on 20 February 2022 - 07:23 AM, said:

if (CurrentNotch >= 0)
            {
                if (Notches[Notches.Count - 1].Type == ControllerState.Emergency)


Not looking into this part of the code, just solving puzzles as a relaxation:
if (CurrentNotch >= 0)
    {
        if (Notches.Count > 0 && Notches[Notches.Count - 1].Type == ControllerState.Emergency)

Is there a prize? :)

#3 User is offline   Serana 

  • Conductor
  • Group: Status: Contributing Member
  • Posts: 489
  • Joined: 21-February 13
  • Gender:Male
  • Location:St Cyr l'Ecole (France)
  • Simulator:Open Rails
  • Country:

Posted 21 February 2022 - 12:34 AM

Or you could use LastOrDefault.

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users