Solved Disable engine with lua

Discussion in 'Mod Support' started by chris_lucas, Apr 12, 2017.

  1. chris_lucas

    chris_lucas
    Expand Collapse

    Joined:
    Dec 12, 2012
    Messages:
    1,444
    How would I shutdown the engine with lua and does work with electric engines too?
     
  2. torsion

    torsion
    Expand Collapse

    Joined:
    May 31, 2015
    Messages:
    1,600
    Do you want to "disable" the engine or "shutdown" the engine (eg simply "turn it off")?

    On the default game settings if the engine is simply "turned off" then the vehicle controller will attempt to restart it using the starter motor. So you can't really leave it shut down and expect to start it back up whenever you want to... the game will try to crank it as soon as it's off.

    This code should kill the engine if run in Vehicle Lua, although I don't know if it's the best way:
    Code:
    powertrain.getDevice("mainEngine"):lockUp()
    This should stall the engine, but as I said it's only for a brief time: the game will automatically attempt to restart the engine. As before, this should be run from Vehicle Lua.
    Code:
    powertrain.getDevice("mainEngine").isStalled = true
     
  3. bluerider598

    bluerider598
    Expand Collapse

    Joined:
    Aug 13, 2016
    Messages:
    196
    Isn't there a way to just turn off the ignition with a lua code?
     
  4. torsion

    torsion
    Expand Collapse

    Joined:
    May 31, 2015
    Messages:
    1,600
    I feel like I addressed this above. If the engine is off then the vehicle controller will attempt to turn the engine back on.

    EDIT:
    In other words, the game does not currently have support for "the engine is just off until the player says to crank it up" when settings are on defaults. I think one of the "realistic" gameplay settings probably changes that but...
     
  5. bluerider598

    bluerider598
    Expand Collapse

    Joined:
    Aug 13, 2016
    Messages:
    196
    I just tried that line of code, and it didn't stall my vehicle.. I was referring to being able to run code that would mimic the user turning off their vehicle with the ignition control.
     
  6. torsion

    torsion
    Expand Collapse

    Joined:
    May 31, 2015
    Messages:
    1,600
    I edited my post above to clarify a bit.

    In response to your latest post: Did you run it in Vehicle Lua? Make sure you have your volume turned up enough to hear the engine stop/start.
     

    Attached Files:

    • upload_2017-4-12_19-51-51.png
  7. bluerider598

    bluerider598
    Expand Collapse

    Joined:
    Aug 13, 2016
    Messages:
    196
    Yes, my mistake, I tried it again and I did notice the message in the upper left hand corner very briefly gives the engine is off message when using your line of code for stalling the engine. However, I am able to press a button I have mapped to the ignition control, and when in realistic shifting mode, I can turn off the engine and it will stay off until I press and hold the ignition button, and it will start up for me, when I want it to. I am just wondering if you can use a line of lua code to mimic that control, and simply switch off the ignition like using the button would.
     
  8. torsion

    torsion
    Expand Collapse

    Joined:
    May 31, 2015
    Messages:
    1,600
    I'm not familiar with the input mapping you refer to as "ignition control", can you tell me the exact name I'd look for?
     

    Attached Files:

    • upload_2017-4-12_20-15-17.png
  9. bluerider598

    bluerider598
    Expand Collapse

    Joined:
    Aug 13, 2016
    Messages:
    196
    You have to tick the "show advanced controls" box
     

    Attached Files:

    • starter.png
  10. torsion

    torsion
    Expand Collapse

    Joined:
    May 31, 2015
    Messages:
    1,600
    That simply activates the engines starter motor. Operating a switch for a starter motor is not the same as operating an ignition switch.

    Please try this exercise for me. Start up the game and load a level and vehicle with your gearbox mode set to realistic. From the vehicle lua console, run this code:
    Code:
    print(powertrain.getDevice("mainEngine").isStalled)
    The game will print to the console whether the engine is stalled, it should be "false". Now toggle out of the console, press a button you have mapped to "Activate the engine starter motor" in order to get the motor turned off. Now run the above lua command again. It should now read "true".

    See what I'm getting at? IMO there's no good way to do what you guys want right now.[/code]
     
  11. bluerider598

    bluerider598
    Expand Collapse

    Joined:
    Aug 13, 2016
    Messages:
    196
    I understand that. But when you say to press the button to shut the engine off, that's what I'm referring to when I say to use lua code to mimic turning the ignition off. If you're able to press a button to shut the motor off and keep it shut off until I want to restart it, it should be possible to use a lua code to do the same thing.
     
  12. torsion

    torsion
    Expand Collapse

    Joined:
    May 31, 2015
    Messages:
    1,600
    This could all be broken later, but here is code that will "turn the engine off"... given all the above caveats (it's not an ignition switch, etc etc):
    Code:
    powertrain.getDevice("mainEngine").outputAV1 = 0
    And of course this will crank the engine back up:
    Code:
    powertrain.getDevice("mainEngine"):activateStarter()
    I would not be at all surprised if all of this was broken in an update or two.

    EDIT: doesn't seem very reliable either. I'm sure there's a better way, but I doubt that there's a good way right now. If you are really determined you can use a counter or timer to run the first code block over a few frames. That should get it to work consistently.
    --- Post updated ---
    Actually try using this one to shut the engine off, this seems effective and reliable. All of the above warnings still apply of course. ;-)

    Code:
    powertrain.getDevice("mainEngine").starterThrottleKillCoef = 0; 
     
    • Like Like x 1
  13. bluerider598

    bluerider598
    Expand Collapse

    Joined:
    Aug 13, 2016
    Messages:
    196
    Works great, thanks.
     
    • Like Like x 1
  14. Diamondback

    Diamondback
    Expand Collapse
    Vehicle Systems Lead
    BeamNG Team

    Joined:
    Apr 8, 2014
    Messages:
    1,957
    I actually implemented this properly for 0.9.
    Here's what you need to do:
    Code:
    controller.getController("vehicleController").setEngineIgnition(false)
    This works for anything that is controlled by a vehicleController (so any regular car).
    If you want to go more low level and talk to the powertrain directly you can also use this:
    Code:
    powertrain.getDevice("mainEngine"):setIgnition(0)
    However, the direct approach won't work correctly with the vehicleController since it will just try to restart the engine. (Also make sure to adapt the code to whatever your engine device is called)
     
    • Like Like x 2
  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