How to shift gears using LUA?

Discussion in 'Programming' started by FIR3x, Aug 20, 2023.

  1. FIR3x

    FIR3x
    Expand Collapse

    Joined:
    Aug 29, 2017
    Messages:
    3
    I'm working on a simple LUA script for smart coasting aka when you just wanna save some fuel on the highway the car will shutdown its engine and shift out of gear to maximize coasting.

    I have 90% of the script done but I just cannot figure out how to tell the gearbox to go to neutral and since I'm using a torque converter auto its even more difficult. I tried everything I could find on the old wiki and the new documentation and no luck. Can somebody please help me with this?
     
  2. murph.vienna

    murph.vienna
    Expand Collapse

    Joined:
    Apr 10, 2023
    Messages:
    635
    wow joined in 2017 and only 2 messages but anyway did you try using a system like idlo stop and start mod because that could potentially work. :)
     
  3. FIR3x

    FIR3x
    Expand Collapse

    Joined:
    Aug 29, 2017
    Messages:
    3
    Im looking at the mod right now and sadly I cannot find any traces of a code that would tell the gearbox to shift to a gear, I can mostly see just lines of code reading the current gear.
     
  4. murph.vienna

    murph.vienna
    Expand Collapse

    Joined:
    Apr 10, 2023
    Messages:
    635
    maybe you should ask the dev of it(if hes still active) and ask for persm and ask him how th system works
     
  5. DaddelZeit

    DaddelZeit
    Expand Collapse

    Joined:
    Jul 17, 2019
    Messages:
    3,486
    You can use this code to shift gears:
    Code:
    controller.mainController.shiftToGearIndex()
    shiftToGearIndex() takes one number as an argument, which is the gear. On manuals this is the literal indexes, so 0 = neutral, -1 = reverse, 1=first etc.
    Automatics don't do that, instead assigning an auto-gear to the index system:
    Code:
    hShifterModeLookup = {[-1] = "R", [0] = "N", "P", "D", "S", "2", "1", "M1"},
    Meaning that to shift to neutral shiftToGearIndex needs to be called with 0, and to shift back into drive shiftToGearIndex needs to be called with 2.
    Keep in mind that this might not work properly with the Arcade gearbox mode.
     
    • Like Like x 1
  6. MrRadYT

    MrRadYT
    Expand Collapse

    Joined:
    Jan 18, 2023
    Messages:
    241
    Will this code work for my lua script? I just want to shift into neutral if wheelspeed is under 5 and theres no throttle, but not if its in reverse
    Code:
    local M = {}
    
    local wheelspeed = 0
    local nbs = 0
    local gearbox = powertrain.getDevice("gearbox")
    
    if electrics.values.throttle > 0.1 then
        nbs = 0
      elseif gearbox.gearRatios[gearbox.gearIndex] == 0 then
          nbs = 0
          return false
      end
    
    if electrics.values.wheelspeed <= 5.0 then
        nbs = 1
    end
    
    if electrics.values.wheelspeed >= 5.0 then
        nbs = 0
    end
    
    local function updateGFX(dt)
        if nbs == 1 then
            controller.mainController.shiftToGearIndex(0)
    end
     
  7. DaddelZeit

    DaddelZeit
    Expand Collapse

    Joined:
    Jul 17, 2019
    Messages:
    3,486
    Hi, code this short should work:
    Code:
    local M = {}
    
    local function updateGFX()
        -- speed is in m/s (m/s * 3.6 = km/h)
        if electrics.values.wheelspeed < 5/3.6 and electrics.values.throttle == 0 and electrics.values.gearIndex > 0 then
            controller.mainController.shiftToGearIndex(0)
        end
    end
    
    local function init()
    end
    
    M.init = init
    M.updateGFX = updateGFX
    return M
    This is a vehicle controller, so you'll need to load it via jbeam: https://documentation.beamng.com/modding/vehicle/sections/controller/
    Alternatively, you can use this code to always run as an extension. Just plop it in a "lua" subfolder of the vehicle you're trying to mod.
    Code:
    local M = {}
    
    local function updateGFX()
        -- speed is in m/s (m/s * 3.6 = km/h)
        if electrics.values.wheelspeed < 5/3.6 and electrics.values.throttle == 0 and electrics.values.gearIndex > 0 then
            controller.mainController.shiftToGearIndex(0)
        end
    end
    
    local function onExtensionLoaded()
    end
    
    M.onExtensionLoaded = onExtensionLoaded
    M.updateGFX = updateGFX
    return M
     
    • Like Like x 1
  8. MrRadYT

    MrRadYT
    Expand Collapse

    Joined:
    Jan 18, 2023
    Messages:
    241
    So this file structure in the .zip is the right one?
    Code:
    mrradyt_wentwardtransmissionpack.zip/lua/vehicle/controller/nbsVoith.lua
     
  9. MrRadYT

    MrRadYT
    Expand Collapse

    Joined:
    Jan 18, 2023
    Messages:
    241
    Hello, unfortunately, the code doesn't work for me. I've loaded the controller in the jbeam and got the lua file in the right folder, but it doesn't do anything. No errors in the console either. Do you know why?
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice