[lua] Which Side A Rail Vehicle Is Coupled To Consist

Discussion in 'General Discussion' started by Ingwar, Jan 14, 2026.

  1. Ingwar

    Ingwar Member

    Joined:
    Apr 19, 2024
    Messages:
    123
    Likes Received:
    21
    Is it possible to get the side of the coupling in a script?
    For engines this information is vital..
     
  2. Oystein

    Oystein Well-Known Member

    Joined:
    May 24, 2018
    Messages:
    375
    Likes Received:
    341
    Yes, this is possible.
    I have used this code that was shared by NZTS SavV on the TS Modding Discord-server.

    Code:
    gSomethingCoupledAtRear = 0 -- Create Variable, Coupled pipes
    gSomethingCoupledAtFront = 0
    COUPLING_MSG = 10000 -- Whatever unique ID code you want to use can go here - most people seem to use 10000
    SEND_FWD = 0 -- Set up variables _FWD and _BWD so they can be used in all SendConsistMessage Calls and for clarity on which direction the message is sending
    SEND_BWD = 1
    
    
    function CheckCouplings()
    
        gSomethingCoupledAtRear = (Call("SendConsistMessage", COUPLING_MSG, 0, SEND_BWD) == 1)  and 1 or 0
        -- Check for item connected to rear end of vehicle
        -- Sets gSomethingCoupledAtRear to 1 if Coupled at Rear, 0 if not coupled
    
        gSomethingCoupledAtFront = (Call("SendConsistMessage", COUPLING_MSG, 0, SEND_FWD) == 1) and 1 or 0
        -- Check for item connected to front end of vehicle  
        -- Sets gSomethingCoupledAtFront to 1 if Coupled at Front, 0 if not coupled
    end -- CheckCouplings
    
    gStartingLength = 0 -- Coupled pipes
    gUpdateCouplers = 0 -- Coupled pipes
    gStartingLengthPrev = 0 -- Coupled pipes
    
    function UpdateConsistLength() -- Coupled pipes
        gStartingLength = Call("*:GetConsistLength")
        if gStartingLength ~= gStartingLengthPrev then
            gUpdateCouplers = 1
        else
            gUpdateCouplers = 0
        end
        gStartingLengthPrev = gStartingLength
    end
    Then in Update you must add this code:
    Code:
    UpdateConsistLength()
    
    if gUpdateCouplers == 1 then
        CheckCouplings()
    end
    You do not want to send consist messages on every game tick, that is why it only check couplings if the consist lenght has changed.
     
    • Helpful Helpful x 1
  3. Ingwar

    Ingwar Member

    Joined:
    Apr 19, 2024
    Messages:
    123
    Likes Received:
    21
    Thank you very much! Very smart solution! I want to implement correct headlights control switch.
     
  4. Ingwar

    Ingwar Member

    Joined:
    Apr 19, 2024
    Messages:
    123
    Likes Received:
    21
    Can you please direct me to this discord server? Would like to join this community...
     
  5. Oystein

    Oystein Well-Known Member

    Joined:
    May 24, 2018
    Messages:
    375
    Likes Received:
    341
    • Helpful Helpful x 1
  6. Ingwar

    Ingwar Member

    Joined:
    Apr 19, 2024
    Messages:
    123
    Likes Received:
    21
    Thank you, joined!
     
    • Like Like x 1

Share This Page