Solved Trouble with lua throttle

Discussion in 'Mod Support' started by JeffTesta, Sep 30, 2019.

  1. JeffTesta

    JeffTesta
    Expand Collapse

    Joined:
    Dec 6, 2017
    Messages:
    75
    Please forgive my lack of lua understanding as this is my first mod with it.

    What I'm trying to do is automatically set the throttle input to 'x' if the RPM drops below 'y'. It is for a simulated idle on an electricMotor with a manual transmission.

    Added to the motor jbeam for the vehicle:
    Code:
        "controller": [
            ["fileName"],
            ["e_idle"],
        ],
    
    My Lua:
    Code:
    local M = {}
    
    M.type = "auxilliary"
    M.relevantDevice = nil
    M.defaultOrder = 1000
    M.throttle = 0
    M.rpm = 0
    
    
    local function init()
        local engine = v.data.engine
           
        -- if (M.rpm < 800) then
        --     M.throttle = 0.02
        -- else
            M.throttle = 0.02   
    end
    
    -- public interface
    M.onReset   = onReset
    M.reset = reset
    M.updateGFX = updateGFX
    -------------------------------
    return M
    
    I'm pretty lost with this. I believe this should be very straightforward for somebody with lua experience. Thanks!
     
  2. aljowen

    aljowen
    Expand Collapse

    Joined:
    Oct 21, 2012
    Messages:
    1,677
    Hi,
    There are some issues with your code
    • The "init()" (init stands for initialise) function that you have only runs once when the vehicle is loaded. You should have that code inside your vehicle update function. ("updateGFX" as listed below)
    • The "init()" function won't do anything, since it isn't listed on your public interface. As such, I don't think the game will call it. The code below is typical for my mods. You can either change your function names to match this interface, or change this interface to fit your functions. But only change the value on the right of the '='.
      Code:
      -- public interface
      M.onInit    = onInit
      M.onReset   = onReset
      M.updateGFX = updateGFX
    • You need to access the rpm from a stream, and then you need to set your throttle input back to the throttle stream.
    • You don't need an "else" clause for your statement. Since you only need to apply a throttle value IF it goes below a certain RPM value, otherwise there is no need to interfere.
      Code:
      local function updateGFX(dt)
      
          local rpm = math.floor( electrics.values.rpm + 0.5 or 0)
          local throttle = 0
      
          if rpm < 800 then
             throttle = 0.05
             electrics.values.throttle = throttle
          end
      end
      
      
    I wrote this wiki page, it may be useful to you: https://wiki.beamng.com/Vehicle_Lua

    Hope some of that helps :)
     
    • Like Like x 1
    • Informative Informative x 1
  3. JeffTesta

    JeffTesta
    Expand Collapse

    Joined:
    Dec 6, 2017
    Messages:
    75
    Thank you for the help. I'll work with that and see how it goes. The wiki page is very helpful also. :)
     
    • Like Like x 1
  4. JeffTesta

    JeffTesta
    Expand Collapse

    Joined:
    Dec 6, 2017
    Messages:
    75
    Ok, my lua reflects what you wrote above but I'm having trouble getting it to initialize. Error is as follows:

    It should be in the "\lua\controller" folder, correct? Jbeam call it out as a controller too?
     
  5. NOCARGO

    NOCARGO
    Expand Collapse

    Joined:
    Apr 1, 2019
    Messages:
    1,514
    Please allow me to note that @aljowen made a typo in his example and also didn't provided the whole text
    because he is assuming that you are aware of the header :)
    The curved bracket in local rpm = math.floor( electrics.values.rpm + 0.5 or 0) goes on space to the right
    like this ---> local rpm = math.floor(electrics.values.rpm + 0.5 or 0) (at least I think so)
    And the whole file should look something like this :
    Code:
    local M = {}
    M.type = "auxilliary"
    
    local function updateGFX(dt)
      local rpm = math.floor(electrics.values.rpm + 0.5 or 0)
      local throttle = 0
      if rpm < 800 then throttle = 0.05
      electrics.values.throttle = throttle
      end
    end
    -- public interface
    M.onInit = onInit
    M.onReset = onReset
    M.updateGFX = updateGFX
    
    return M
    I'm also not sure if electrics.values.throttle = throttle goes inside the IF statement or rather after the 'END' of
    the IF statement and before the 'END' of the function itself. Maybe that doesn't matter, maybe it does, I'm not
    sure. Finally I'm also not sure if you need to state your table as M.relevantDevice = nil at the beginning of the file.
    Anyway, you can add or leave those things out by trial and error imo. :)
     
  6. JeffTesta

    JeffTesta
    Expand Collapse

    Joined:
    Dec 6, 2017
    Messages:
    75
    Thanks for the info! No luck with those changes, either. I tried the options you listed below but the console continues to show the same information as before. I'll attach the relevant portion of this mod here just in case anyone wants to see more detail.
     

    Attached Files:

  7. meywue

    meywue
    Expand Collapse
    Administrator
    BeamNG Team

    Joined:
    Nov 19, 2015
    Messages:
    339
    Here you go. You have to have an init function in your controller code. Fixed it and attached the file.
     

    Attached Files:

    • Like Like x 2
  8. JeffTesta

    JeffTesta
    Expand Collapse

    Joined:
    Dec 6, 2017
    Messages:
    75
    Thanks! This worked perfectly.
     
    • Like Like x 1
  9. SebastianJDM

    SebastianJDM
    Expand Collapse

    Joined:
    Apr 9, 2017
    Messages:
    856
    also, please release whatever you're working on to the repo, i'm curious to drive it. it could also be used for a "creep" mode in an automatic transmission (like tesla does)
     
  10. JeffTesta

    JeffTesta
    Expand Collapse

    Joined:
    Dec 6, 2017
    Messages:
    75
    It's pretty simple, so I could release it as an experimental mod in the future.

    I imagined a scenario where gasoline/diesel vehicles were outlawed for general public use. Vintage car enthusiasts could cope with this by using an electric motor to emulate the behavior of an internal combustion engine. The amp/volt/current sent to the motor could be programmed to work in conjunction with the regen ability of the car to simulate the power delivery of whichever ICE the driver desired.

    A user interface in the vehicle would allow the choice of multiple engine profiles. A simulated tachometer would be nice too. Multi speed transmissions could be equipped or simulated also.

    Some ICE equipped vehicles are already playing simulated sound effects through the audio system. This would build on that concept and have optional driver selected engine sounds that would correlate with RPM & load.

    Just an idea I've been playing with and it works surprisingly well in BeamNG now that the idle is sorted.
     
    • Like Like x 3
  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