1. Thread posting restricted: Only the BeamNG team members can post new threads in this sub-forum in order to prevent spam. Posting is available on unlocked threads.

VehicleController overhaul

Discussion in 'Microblogs' started by Diamondback, Oct 9, 2018.

  1. Diamondback

    Diamondback
    Expand Collapse
    Vehicle Systems Engineering
    BeamNG Team

    Joined:
    Apr 8, 2014
    Messages:
    1,752
    Hey everyone,

    today I'd like to introduce you to some deep changes inside a core part of the game. For the average user this is not really of much interest, modders however (especially those with more advanced vehicle lua mods) will appreciate the insight.

    TLDR: If you are the author of a vehicle mod that uses the stock vehicleController but a custom shiftLogic, keep on reading. If not, the changes do not really affect you.

    Up till now it was very difficult to support anything else than vehicles with a single combustion engine. Hybrids or electric powertrains weren't really supported out of the box. Of course you could always create your own logic from scratch and have everything working the way you like. However, that requires quite some knowledge about the game and how stuff works.
    To make things easier for the future I decided to revamp large parts of how the vehicleController works and make it more future proof by removing the dependency of having a combustion engine with a gearbox.
    In the end this means that it's much easier to keep using the vehicleController logic for custom powertrains while only supplying some supplementary logic.

    The downside of this improved compatibility is that I had to do some breaking changes that will break some mods. Please note: this issue only applies to mods that use the stock vehicleController.lua file with an additional custom "shiftLogic" module. Fortunately there are two easy options to fix that:
    1. Update your code according to what I'll describe below (preferred)
    2. Ship a copy of the current vehicleController.lua with your mod

    Option 1

    There are a bunch of small changes required to make things less focused on combustion engines. Here's what needs to be changed:

    Every shiftLogic module now has some more exposed data, giving it greater control over how certain aspects of the vehicleController work:
    Code:
    M.smoothedAvgAVInput = 0
    M.rpm = 0
    M.idleRPM = 0
    M.maxRPM = 0
    
    M.engineThrottle = 0
    M.engineLoad = 0
    M.engineTorque = 0
    M.gearboxTorque = 0
    
    M.ignition = true
    M.isEngineRunning = 0
    
    M.oilTemp = 0
    M.waterTemp = 0
    M.checkEngine = false
    
    M.energyStorages = {}
    Most of these new properties need to be updated every frame, the stock shiftLogic modules have now a dedicated method for that:
    Code:
    local function updateExposedData()
      M.rpm = engine and (engine.outputAV1 * constants.avToRPM) or 0
      M.smoothedAvgAVInput = sharedFunctions.updateAvgAVSingleDevice("gearbox")
      M.waterTemp = (engine and engine.thermals) and (engine.thermals.coolantTemperature and engine.thermals.coolantTemperature or engine.thermals.oilTemperature) or 0
      M.oilTemp = (engine and engine.thermals) and engine.thermals.oilTemperature or 0
      M.checkEngine = engine and engine.isDisabled or false
      M.ignition = engine and not engine.isDisabled or false
      M.engineThrottle = (engine and engine.isDisabled) and 0 or M.throttle
      M.engineLoad = engine and (engine.isDisabled and 0 or engine.instantEngineLoad) or 0
      M.running = engine and not engine.isDisabled or false
      M.engineTorque = engine and engine.combustionTorque or 0
      M.gearboxTorque = gearbox and gearbox.outputTorque or 0
      M.isEngineRunning = engine and ((engine.isStalled or engine.ignitionCoef <= 0) and 0 or 1) or 1
    end
    This method is then being called every frame from the various updateXYZ() methods throughout the file.

    There is also a new exposed method for every shiftLogic module:
    Code:
    local function sendTorqueData()
      if engine then
        engine:sendTorqueData()
      end
    end
    
    M.sendTorqueData = sendTorqueData
    
    Last but not least the init method got overhauled too. The parameters changed and it needs to do a bit of additional work:
    Code:
    local function init(jbeamData, sharedFunctionTable)
      sharedFunctions = sharedFunctionTable
      engine = powertrain.getDevice("mainEngine")
      gearbox = powertrain.getDevice("gearbox")
      torqueConverter = powertrain.getDevice("torqueConverter")
      [..]
    
    Code:
    M.maxRPM = engine.maxRPM
      M.idleRPM = engine.idleRPM
      M.maxGearIndex = automaticHandling.maxGearIndex
      M.minGearIndex = abs(automaticHandling.minGearIndex)
      M.energyStorages = sharedFunctions.getEnergyStorages({engine})
    I know this probably looks quite scary but I'm happy to help anyone that asks, just send me a message :)

    I've also attached a current copy of the auto shiftLogic so you can take a look at the overall file.

    Attention: the changed code obviously won't work for the current public version, this guide is just so that you can update your mods once the next update is out!

    Option 2

    The much easier, but definitely not preferred method is to include a copy of your current vehicleController.lua file in your mod. Upon loading the mod this will overwrite the new file with your old copy, keeping everything working as is. Please note: This should only be a last resort, if you do this, you miss out on future improvements of the vehicleController. (Also: this is only allowed for controllers/powertrain/energyStorage as they were designed with this in mind, never do this with any other stock lua files!)


    Feel free to ask questions or post suggestions about this here. :)
     

    Attached Files:

    #1 Diamondback, Oct 9, 2018
    Last edited: Oct 9, 2018
    • Like Like x 13
    • Informative Informative x 4
  2. Ai'Torror

    Ai'Torror
    Expand Collapse
    BeamNG Team

    Joined:
    Aug 29, 2015
    Messages:
    1,498
    Does this new change allow for multiple engines?(For example 2 separate combustion engines)?

    I feel like more flexibility with the powertrain system would be welcome. Right now (as far as I know) we anly have the MainEngine, how about adding an ability to name each engine separately using some sort of a template, the same could go for transmissions, transfercases and such.
    This would allow modders to create interesting contraptions along the lines of the twinstar eldorado.
    You could for example have a powertrain layed out like that:
    CombEngine1-torqueConverter1-differentialF-wheelaxleFL/FR
    And:
    CombEngine2-torqueConverter2-differentialR-wheelaxleRL/RR.



    Even if multiple separate powertrain systems wouldn't be possible, I'm still sure the hybrid stuff you've mentioned could come usefull if not exactly for me then for sure for others.
     
  3. Diamondback

    Diamondback
    Expand Collapse
    Vehicle Systems Engineering
    BeamNG Team

    Joined:
    Apr 8, 2014
    Messages:
    1,752
    Yes that is possible too now. Please note: possible does not mean it works out of the box, but rather that the are no hurdles in making it happen. You could make a 6x6 with 6 electric motors no problem as long as you create your own shiftLogic module that controls all 6 of these in some way.

    I obviously can't include logic for all sorts of weird and wonderful things, so that stuff needs to be implemented by modders.
     
    • Informative Informative x 7
  4. Arcanox

    Arcanox
    Expand Collapse

    Joined:
    Aug 22, 2018
    Messages:
    274
    What about a situation where I've made a custom gearbox that uses an existing shiftLogic module? My "eCVT" gearbox code is modified from the CVT gearbox, but the shiftLogic hasn't changed. I assume then that I can just copy the new version of the shiftLogic-cvtGearbox and rename it as I did before. Is there anything else that would need to change in my controller/gearbox code to make use of the new functions, or is that handled through the updateXYZ functions already called by the game and vehicleController?
     
    • Like Like x 1
  5. Diamondback

    Diamondback
    Expand Collapse
    Vehicle Systems Engineering
    BeamNG Team

    Joined:
    Apr 8, 2014
    Messages:
    1,752
    Hm I don't quite understand, you are either using an existing shiftLogic file (in which case the mod will continue to use the existing file and nothing happens) or you have your own file in the mod in which case that file is then outdated and needs to be updated. Oh, do you mean you just copied the stock cvt logic file to your mod without modifying it? If so, why?
     
  6. Arcanox

    Arcanox
    Expand Collapse

    Joined:
    Aug 22, 2018
    Messages:
    274
    I copied the stock CVT logic file because I have a custom gearbox device. It's called "ecvtGearbox", so the vehicleController was looking for a file called "shiftLogic-ecvtGearbox". I didn't see any way to tell it to simply use the stock "shiftLogic-cvtGearbox" file at all, but if there's a way to do that, that'd be optimal. All of my changes are in the gearbox device file itself, not the shift logic.
     
  7. Diamondback

    Diamondback
    Expand Collapse
    Vehicle Systems Engineering
    BeamNG Team

    Joined:
    Apr 8, 2014
    Messages:
    1,752
    He, you can indeed point it to some random file, in the vehicleController jbeam section, set the "shiftLogicName" attribute to "cvtGearbox". Ie:
    Code:
    "vehicleController": {
      [..]
      "shiftLogicName": "cvtGearbox",
      [..]
    }
    
     
  8. Arcanox

    Arcanox
    Expand Collapse

    Joined:
    Aug 22, 2018
    Messages:
    274
    Oh, that's even better. Is that something new since the latest publicly-available release? I didn't see that in the vehicleController source anywhere, but I may have glanced over it by mistake.
     
  9. Diamondback

    Diamondback
    Expand Collapse
    Vehicle Systems Engineering
    BeamNG Team

    Joined:
    Apr 8, 2014
    Messages:
    1,752
    Nope that's been there ever since these shiftLogic modules existed :)
     
  10. Arcanox

    Arcanox
    Expand Collapse

    Joined:
    Aug 22, 2018
    Messages:
    274
    I must have glanced over it then ;)

    Good to know, though! I'll update my two hybrid mods soon to remove that unneeded shiftLogic file, so hopefully my controller won't break (at least not as badly) when the new version is released.
     
  11. Diamondback

    Diamondback
    Expand Collapse
    Vehicle Systems Engineering
    BeamNG Team

    Joined:
    Apr 8, 2014
    Messages:
    1,752
    Just tested, when I do what I proposed above, the hybrid ETK works fine on my current version. :) (wouldn't work before the fix ofc)

    BTW, really nice job on that one! :)
     
  12. Arcanox

    Arcanox
    Expand Collapse

    Joined:
    Aug 22, 2018
    Messages:
    274
    Thanks!

    That should mean I can update before the game updates and have it work on both versions.
     
    • Like Like x 1
  13. ByteGuy

    ByteGuy
    Expand Collapse

    Joined:
    May 2, 2016
    Messages:
    313
    welp, this is very exciting
     
    • Agree Agree x 1
  14. YellowRusty

    YellowRusty
    Expand Collapse

    Joined:
    Nov 9, 2016
    Messages:
    1,103
    This might be a bit of a dumb question, but will logic for at least one weird and wonderful thing be included so that modders have something to examine and build off of?
     
  15. Diamondback

    Diamondback
    Expand Collapse
    Vehicle Systems Engineering
    BeamNG Team

    Joined:
    Apr 8, 2014
    Messages:
    1,752
    No, I'm not going to include stuff anymore that is not used by our own content, in the past doing this has fired back at me a few times already...
     
    • Informative Informative x 4
    • Agree Agree x 3
    • Like Like x 1
  16. SebastianJDM

    SebastianJDM
    Expand Collapse

    Joined:
    Apr 9, 2017
    Messages:
    856
    twin engine piccolina confirmed
     
    • Like Like x 2
  17. Tsutarja495

    Tsutarja495
    Expand Collapse

    Joined:
    Mar 16, 2014
    Messages:
    779
    Or maybe something generic like a proper hybrid config for the Wentward.
     
  18. Diamondback

    Diamondback
    Expand Collapse
    Vehicle Systems Engineering
    BeamNG Team

    Joined:
    Apr 8, 2014
    Messages:
    1,752
    I literally said that weird stuff won't be included and why and you guys conclude that exactly that will be part of an update...?
    :p
     
    • Agree Agree x 3
  19. YellowRusty

    YellowRusty
    Expand Collapse

    Joined:
    Nov 9, 2016
    Messages:
    1,103
    It might not be quite as weird as you think it is, but thanks for letting us know that a four-wheel drive Piccolina isn't in development at the moment.
     
    • Agree Agree x 1
  20. SebastianJDM

    SebastianJDM
    Expand Collapse

    Joined:
    Apr 9, 2017
    Messages:
    856
    you said you won't add it if it isn't official content, so it's safe to assume it definitely 100% is coming, as long as it's official content then, right?

    /s btw :p
     
  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