Solved Trouble with LUA

Discussion in 'Mod Support' started by bluerider598, Jan 30, 2018.

  1. bluerider598

    bluerider598
    Expand Collapse

    Joined:
    Aug 13, 2016
    Messages:
    197
    I'm working on a lua mod (code at bottom of post) that makes the horn have chance of getting stuck 'on' and blaring until you reset the car during a crash, simulating the twisting and bending of the metal frame grounding out the horn wires like what sometimes happens in real crashes. But I'm not sure how to code it so that it regenerates a random number without having resetting the whole vehicle lua. In other words, when I press "R" to reset the car, I want to generate a different random number without having to use "Ctrl+R" to re-run the whole code.

    Also, right now, if the horn gets triggered in the crash, if you press the horn button, it will shut the horn off. How do I make it so that the horn stays on until you reset the car, no matter if you use the horn button? Perhaps some code that detects when the horn is off? I've tried
    Code:
    if electrics.horn == false then electrics.horn(true)
    but that didn't work. I've also tried some while loops, but those made the game hang.. Either that, or if there's a way to "ignore" the horn button input until reset, that would work too. Any help is much appreciated.

    I started with the postCrashBrake file and modified it to what I need. Right now it has a 1 in 3 chance of setting off the horn in a frontal crash, eventually I will change it to 1/20 chance when everything is working right:

    Code:
    local M = {}
    M.type = "auxilliary"
    M.relevantDevice = nil
    
    local max = math.max
    local state = "idle"
    local hornThreshold = 0
    local chance = math.random(3)
    
    local function updateGFX(dt)
    
      if state == "idle" then
        if (max(sensors.gy) > hornThreshold) and chance == 1 then
          electrics.horn(true)
          state = "honking"
         
        end
       
       
      end
    end
    
    local function init(data)
      --if the horn is still active from before reset, deactivate it
      if state == "honking" then
        electrics.horn(false)
      end
    
      state = "idle"
      hornThreshold = 30
     
      end
    
    M.init = init
    M.updateGFX = updateGFX
    
    return M
     
    #1 bluerider598, Jan 30, 2018
    Last edited: Jan 30, 2018
  2. bluerider598

    bluerider598
    Expand Collapse

    Joined:
    Aug 13, 2016
    Messages:
    197
    I tried that, doesn't seem to work, no matter where I put the code. Unless I'm doing something wrong?:
    Code:
    local M = {}
    M.type = "auxilliary"
    M.relevantDevice = nil
    
    local max = math.max
    local state = "idle"
    local hornThreshold = 0
    local chance = math.random(3)
    
    local function onReset()
     math.random(3)
      end
    
    local function updateGFX(dt)
    
      if state == "idle" then
        if (max(sensors.gy) > hornThreshold) and chance == 1 then
          electrics.horn(true)
          state = "honking"
             
        end
     
      end
    end
    
    local function init(data)
      --if the horn is still active from before reset, deactivate it
      if state == "honking" then
        electrics.horn(false)
      end
    
      state = "idle"
      hornThreshold = 30
     
      end
     
    M.init = init
    M.updateGFX = updateGFX
    M.onReset  = onReset
    
    return M
     
  3. bluerider598

    bluerider598
    Expand Collapse

    Joined:
    Aug 13, 2016
    Messages:
    197
    Thank you! That should solve half my problem. And which lua folder exactly are you putting it in? Because before, I would just replace the original postCrashBrake file with my file just to test it.
     
  4. fufsgfen

    fufsgfen
    Expand Collapse

    Joined:
    Jan 10, 2017
    Messages:
    6,781
    Maybe you could add custom control for the horn and disable horn input? I don't know if it is possible to override default bindings though.
     
  5. bluerider598

    bluerider598
    Expand Collapse

    Joined:
    Aug 13, 2016
    Messages:
    197
    It works great, just tested it out. And unfortunately putting it in the common folder doesn't work, so using it for all cars is another thing I have to figure out. Worst case scenario, I'll just have to put that 1 file in every vehicle folder.
    Yeah if I could have lua override the horn control after the crash that would work, just not sure how I would do that
     
  6. fufsgfen

    fufsgfen
    Expand Collapse

    Joined:
    Jan 10, 2017
    Messages:
    6,781
    Cutscenes and slowmotion in campaign disable some of the keys, maybe there could be possible to find a way.

    Or maybe it is possible to define binding for the horn which would then overwrite default binding, but that might make mod invalid for the repo.
     
  7. bluerider598

    bluerider598
    Expand Collapse

    Joined:
    Aug 13, 2016
    Messages:
    197
    Interesting.. I just looked through all the campaign lua files plus some others, and couldn't find anything relating to disabling key inputs, but I'll keep looking.. And yeah, overwriting a default binding probably wouldn't be allowed for the repo
     
  8. thomatoes50

    thomatoes50
    Expand Collapse
    BeamNG Team

    Joined:
    Jan 31, 2013
    Messages:
    722
    to load a lua module for any vehicle, you can use controllers
    JBEAM:
    Code:
        "controller": [
            ["fileName"],
            ["moduleName", {"arg1": "test, "arg2": 42}],
        ],
    put your lua file in /lua/vehicle/controller/moduleName.lua

    Code:
    --you need to define some value to describe the module
    M.type = "auxilliary"
    M.relevantDevice = nil



    To disable some action in a scenario define this value in the .json
    Code:
        "blackListActions": [
            "accelerate", "brake", "accelerate_brake", "parkingbrake", "parkingbrake_temporary",
            "parkingbrake_toggle", "shiftUp","shiftDown","gearR","gear1","gear2","gear3",
            "gear4","gear5","gear6","gear7","toggleShifterMode", "default_blacklist_scenario"
        ],
    But it will not work if you are in freeroam and you would need to change every scenario.
    I think you should manually set to true every frame when you meet the condition. and it should be set to false when you reset the car
     
  9. thomatoes50

    thomatoes50
    Expand Collapse
    BeamNG Team

    Joined:
    Jan 31, 2013
    Messages:
    722
    Yes, you cannot force it to be on.
    the other solution is to monitor electrics.values.horn
     
  10. bluerider598

    bluerider598
    Expand Collapse

    Joined:
    Aug 13, 2016
    Messages:
    197
    Thanks @thomatoes50. I got it to work by monitoring electrics.values.horn. There is a small blip in the horn if you press the horn control, but it's probably as good as it's going to get. And to use a controller for all the cars, I would need to make a jbeam addon for every car correct? What's the advantage of making a jbeam addon for every car vs a lua addon for every car?
    Code:
    local M = {}
    
    local hornThreshold = 30
    local chance = math.random(3)
    local state = "idle"
    
    local function updateGFX(dt)
    if state == "idle" then
       if sensors.gy > hornThreshold and chance == 1 then
       electrics.horn(true)
       state = "honking"  
       end
     
       elseif state == "honking" and electrics.values.horn == 0 then
       electrics.horn(true)
       end
     
    end
    
    local function onInit()
        state = "idle"
        electrics.horn(false)
        chance = math.random(3)
        hornThreshold = 30
    end
    
    local function onReset()
        state = "idle"
        electrics.horn(false)
        chance = math.random(3)
    end
    
    M.onInit = onInit
    M.onReset  = onReset
    M.updateGFX = updateGFX
    
    return M
     
  11. thomatoes50

    thomatoes50
    Expand Collapse
    BeamNG Team

    Joined:
    Jan 31, 2013
    Messages:
    722
    hum i was thinking you would define a part for every vehicle but i guess you want to apply this to every car automatically.
    Please avoid copy the file over and over, you can do a really small module for every vehicles that will load your 'main module'
     
  12. bluerider598

    bluerider598
    Expand Collapse

    Joined:
    Aug 13, 2016
    Messages:
    197
    I'm having some trouble making the module as I've never dealt with them before (not as fluent in lua as I should be).. my code is probably totally wrong, or I'm missing something, not sure what. Below is the module I attempted to create and I'll also attach the error I got.
    Code:
    postCrashHorn = require("postCrashHorn")
    postCrashHorn.updateGFX(dt)
    postCrashHorn.onInit()
    postCrashHorn.onReset()
    lua.png
     
  13. thomatoes50

    thomatoes50
    Expand Collapse
    BeamNG Team

    Joined:
    Jan 31, 2013
    Messages:
    722
    almost :)

    put postCrashHorn.lua in lua/vehicles

    now all your cars module should look like this

    Code:
    M = {}
    postCrashHorn = require("postCrashHorn")
    function updateGFX(dt)
        postCrashHorn.updateGFX(dt)
    end
    function onInit()
        postCrashHorn.onInit()
    end
    function onReset()
       postCrashHorn.onReset()
    end
    
    M.updateGFX = updateGFX
    M.onInit = onInit
    M.onReset = onReset
    
    This way the logic is only in one file
     
  14. bluerider598

    bluerider598
    Expand Collapse

    Joined:
    Aug 13, 2016
    Messages:
    197
    It works now, thanks!
     
  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