ChatGpt lua code for beamng

Discussion in 'Utilities and programming' started by FFIVGUY, Jun 22, 2024.

  1. FFIVGUY

    FFIVGUY
    Expand Collapse

    Joined:
    Feb 21, 2018
    Messages:
    603
    I was curious to see if chatgpt could make a lua code for beamng, so I inputted this, but it didn't seem to work in game. I don't know if it was something with chatgpt, beamng, or I'm just not knowledgeable with this stuff yet. I don't know.
    Here’s what I entered:
    in beamng.drive would it be possible to have a pitch lfo for engine sounds based on rpm and be able to set a certain amount of pitches in cents? For example, 0, -5, 0, -5, -5, 0, -5, 0, -5 to get a crossplane sound from a flatplane engine. And if so, where would I reference it here?

    { "etk_engine_i4_2.0": { "information":{ "authors":"BeamNG", "name":"2.0L I4 Engine", "value":4200, }, "slotType": "etk_engine", "slots": [ ["type", "default", "description"], ["etk_enginemounts","etk_enginemounts", "Engine Mounts", {"coreSlot"


    }], ["etk_oilpan_i4","etk_oilpan_i4", "Oil Pan", {"coreSlot"

    }], ["etk_intake_i4_2.0_petrol","etk_intake_i4_2.0_petrol_turbo", "Intake", {"coreSlot"

    }], ["etk_exhaust_i4_2.0_petrol","etk_exhaust_i4_2.0_petrol", "Exhaust"], ["etk_engine_i4_2.0_ecu","etk_engine_i4_2.0_ecu_150", "Engine Management", {"coreSlot"

    }], ["n2o_system","", "Nitrous Oxide System"], ["etk_engine_i4_2.0_internals","etk_engine_i4_2.0_internals", "Engine Long Block", {"coreSlot"

    }], ["etk_transmission","etk_transmission_6M", "Transmission"], ], "powertrain": [ ["type", "name", "inputName", "inputIndex"], ["combustionEngine", "mainEngine", "dummy", 0], ], "mainEngine": { "torque":[ ["rpm", "torque"], [0, 0], [500, 135], [1000, 177], [2000, 214], [2500, 230], [3000, 233], [4000, 238], [4500, 235], [5000, 228], [6000, 212], [7000, 163], [7500, 150], ], "idleRPM":650, //max capable "maxRPM":7000, "inertia":0.10, "friction":14, "dynamicFriction":0.0239, "engineBrakeTorque":36, //"burnEfficiency":0.42, "burnEfficiency":[ [0, 0.12], [0.05, 0.28], [0.4, 0.51], [0.7, 0.48], [1, 0.34], ], //fuel system "energyStorage": "mainTank", "requiredEnergyType":"gasoline",


    json
    //exhaust
    "instantAfterFireSound": "event:>Vehicle>Afterfire>i4_01>single",
    "sustainedAfterFireSound": "event:>Vehicle>Afterfire>i4_01>multi",
    "shiftAfterFireSound": "event:>Vehicle>Afterfire>i4_01>shift",
    "particulates":0.015,
    "instantAfterFireCoef": 0.25,
    "sustainedAfterFireCoef": 0.1,
    "afterFireAudibleThresholdShift":150000,
    "instantAfterFireVolumeCoef": 0.6,
    "sustainedAfterFireVolumeCoef": 0.3,
    "shiftAfterFireVolumeCoef": 0.5,

    //cooling and oil system,
    "thermalsEnabled":true,
    "engineBlockMaterial":"aluminum",
    "engineBlockAirCoolingEfficiency":28,

    //engine durability
    "cylinderWallTemperatureDamageThreshold":150,
    "headGasketDamageThreshold":1500000,
    "pistonRingDamageThreshold":1500000,
    "connectingRodDamageThreshold":2000000,
    "maxTorqueRating": 430,
    "maxOverTorqueDamage": 400,

    //node beam interface
    "torqueReactionNodes:":["e3r","e4r","e2l"],
    "waterDamage": {"[engineGroup]:":["engine_intake"]},
    "radiator": {"[engineGroup]:":["radiator"]},
    "engineBlock": {"[engineGroup]:":["engine_block"]},
    "breakTriggerBeam":"engine",
    "uiName":"Engine",

    "soundConfig": "soundConfig",
    "soundConfigExhaust": "soundConfigExhaust",

    //starter motor
    "starterSample":"event:>Engine>Starter>i4_2009_eng",
    "starterSampleExhaust":"event:>Engine>Starter>i4_2009_exh",
    "shutOffSampleEngine":"event:>Engine>Shutoff>i4_2009_eng",
    "shutOffSampleExhaust":"event:>Engine>Shutoff>i4_2009_exh",
    "starterVolume":0.76,
    "starterVolumeExhaust":0.76,
    "shutOffVolumeEngine":0.76,
    "shutOffVolumeExhaust":0.76,
    "idleRPMStartRate":1.25,
    "idleRPMStartCoef":1.2,

    //engine deform groups
    "deformGroups":["mainEngine", "mainEngine_intake", "mainEngine_accessories"]
    },
    "soundConfig": {
    "sampleName": "I4_2_engine",
    "intakeMuffling": 1,

    "mainGain": -12.5,
    "onLoadGain":1,
    "offLoadGain":0.57,

    "maxLoadMix": 0.73,
    "minLoadMix": 0,

    "lowShelfGain":3,
    "lowShelfFreq":150,

    "highShelfGain":4,
    "highShelfFreq":7500,

    "eqLowGain": 0,
    "eqLowFreq": 120,
    "eqLowWidth": 0.1,

    "eqHighGain": 0,
    "eqHighFreq": 500,
    "eqHighWidth": 0.1,

    "fundamentalFrequencyCylinderCount":4,
    "eqFundamentalGain":0,
    },
    "soundConfigExhaust": {
    "sampleName": "I4_2_exhaust",
    "sampleSmoothingFactor": 0.3

    "mainGain": 2.5,
    "onLoadGain":1,
    "offLoadGain":0.55

    "maxLoadMix": 0.73,
    "minLoadMix": 0,

    "lowShelfGain":-12,
    "lowShelfFreq":100,

    "highShelfGain":7,
    "highShelfFreq":4000,

    "eqLowGain": 0,
    "eqLowFreq": 120,
    "eqLowWidth": 0.2,

    "eqHighGain": 0,
    "eqHighFreq": 4000,
    "eqHighWidth": 0.1,

    "fundamentalFrequencyCylinderCount":4,
    "eqFundamentalGain": -2
    },
    "vehicleController": {
    "clutchLaunchStartRPM":1500,
    "clutchLaunchTargetRPM":2000,
    //**highShiftDown can be overwritten by automatic transmissions**
    "highShiftDownRPM":[0,0,0,2800,3800,4300,4700,4700],
    //**highShiftUp can be overwritten by intake modifications**
    "highShiftUpRPM":6800,
    },

    3/8
    ChatGPT
    To implement a pitch LFO (Low-Frequency Oscillator) for engine sounds in BeamNG.drive based on RPM and allowing for pitch modulation in cents, you'll need to:

    1. Create a Lua script that handles the pitch modulation.
    2. Modify your vehicle's JBeam file to reference the Lua script and ensure it integrates correctly with the engine sound system.
    Step 1: Create the Lua Script
    Here's a Lua script that adjusts engine sound pitch based on RPM and uses a sequence of pitch values in cents:

    lua
    -- File: pitch_lfo_cents.lua
    -- This script modifies engine sound pitch in BeamNG.drive using an LFO and a sequence of pitch values in cents.

    local M = {}

    -- Function to convert cents to frequency ratio
    local function centsToRatio(cents)
    return 2^(cents / 1200)
    end

    -- Example: Define a sequence of pitch modulation values in cents
    local pitchSequenceInCents = {0, -5, 0, -5, -5, 0, -5, 0, -5}
    local sequenceLength = #pitchSequenceInCents

    -- Calculate the corresponding frequency ratios for the pitch sequence
    local pitchSequenceRatios = {}
    for i, cents in ipairs(pitchSequenceInCents) do
    table.insert(pitchSequenceRatios, centsToRatio(cents))
    end

    -- Function to update engine pitch based on RPM and LFO
    local function updateEnginePitch(engine)
    if engine then
    local rpm = engine.rpm
    local normalizedRpm = rpm / engine.maxRPM -- Normalize RPM to a 0-1 range

    -- Calculate LFO value (oscillating between -1 and 1)
    local lfoValue = math.sin(2 * math.pi * normalizedRpm)

    -- Determine pitch index based on LFO value
    local index = math.floor((lfoValue + 1) * (sequenceLength - 1) / 2) + 1
    index = math.max(1, math.min(sequenceLength, index)) -- Ensure index is within bounds

    -- Get the pitch ratio from the sequence
    local pitchRatio = pitchSequenceRatios[index]

    -- Set engine pitch to modified frequency
    engine.sounds.mainSample:setPitch(pitchRatio)
    end
    end

    -- Function called when the script initializes
    local function onInit()
    -- Initialize any necessary variables or resources here
    print("Pitch LFO for engine sounds script initialized")
    end

    -- Function called on each simulation update
    local function onUpdate()
    local vehicle = be:getPlayerVehicle(0)
    if vehicle then
    local engine = vehicle.engine
    updateEnginePitch(engine)
    end
    end

    -- Hook functions to the vehicle's lifecycle events
    M.onInit = onInit
    M.onUpdate = onUpdate

    return M

    Step 2: Modify the JBeam File
    Integrate the Lua script with your vehicle’s JBeam file. Here’s how you can modify your engine.jbeam file:

    json
    {
    "etk_engine_i4_2.0": {
    "information": {
    "authors": "BeamNG",
    "name": "2.0L I4 Engine",
    "value": 4200
    },
    "slotType": "etk_engine",
    "slots": [
    ["type", "default", "description"],
    ["etk_enginemounts", "etk_enginemounts", "Engine Mounts", {"coreSlot": true}],
    ["etk_oilpan_i4", "etk_oilpan_i4", "Oil Pan", {"coreSlot": true}],
    ["etk_intake_i4_2.0_petrol", "etk_intake_i4_2.0_petrol_turbo", "Intake", {"coreSlot": true}],
    ["etk_exhaust_i4_2.0_petrol", "etk_exhaust_i4_2.0_petrol", "Exhaust"],
    ["etk_engine_i4_2.0_ecu", "etk_engine_i4_2.0_ecu_150", "Engine Management", {"coreSlot": true}],
    ["n2o_system", "", "Nitrous Oxide System"],
    ["etk_engine_i4_2.0_internals", "etk_engine_i4_2.0_internals", "Engine Long Block", {"coreSlot": true}],
    ["etk_transmission", "etk_transmission_6M", "Transmission"]
    ],
    "powertrain": [
    ["type", "name", "inputName", "inputIndex"],
    ["combustionEngine", "mainEngine", "dummy", 0]
    ],
    "mainEngine": {
    "torque": [
    ["rpm", "torque"],
    [0, 0],
    [500, 135],
    [1000, 177],
    [2000, 214],
    [2500, 230],
    [3000, 233],
    [4000, 238],
    [4500, 235],
    [5000, 228],
    [6000, 212],
    [7000, 163],
    [7500, 150]
    ],
    "idleRPM": 650,
    "maxRPM": 7000,
    "inertia": 0.10,
    "friction": 14,
    "dynamicFriction": 0.0239,
    "engineBrakeTorque": 36,
    "burnEfficiency": [
    [0, 0.12],
    [0.05, 0.28],
    [0.4, 0.51],
    [0.7, 0.48],
    [1, 0.34]
    ],
    "energyStorage": "mainTank",
    "requiredEnergyType": "gasoline",
    "instantAfterFireSound": "event:>Vehicle>Afterfire>i4_01>single",
    "sustainedAfterFireSound": "event:>Vehicle>Afterfire>i4_01>multi",
    "shiftAfterFireSound": "event:>Vehicle>Afterfire>i4_01>shift",
    "particulates": 0.015,
    "instantAfterFireCoef": 0.25,
    "sustainedAfterFireCoef": 0.1,
    "afterFireAudibleThresholdShift": 150000,
    "instantAfterFireVolumeCoef": 0.6,
    "sustainedAfterFireVolumeCoef": 0.3,
    "shiftAfterFireVolumeCoef": 0.5,
    "thermalsEnabled": true,
    "engineBlockMaterial": "aluminum",
    "engineBlockAirCoolingEfficiency": 28,
    "cylinderWallTemperatureDamageThreshold": 150,
    "headGasketDamageThreshold": 1500000,
    "pistonRingDamageThreshold": 1500000,
    "connectingRodDamageThreshold": 2000000,
    "maxTorqueRating": 430,
    "maxOverTorqueDamage": 400,
    "torqueReactionNodes:": ["e3r", "e4r", "e2l"],
    "waterDamage": {"[engineGroup]:": ["engine_intake"]},
    "radiator": {"[engineGroup]:": ["radiator"]},
    "engineBlock": {"[engineGroup]:": ["engine_block"]},
    "breakTriggerBeam": "engine",
    "uiName": "Engine",
    "soundConfig": "soundConfig",
    "soundConfigExhaust": "soundConfigExhaust",
    "starterSample": "event:>Engine>Starter>i4_2009_eng",
    "starterSampleExhaust": "event:>Engine>Starter>i4_2009_exh",
    "shutOffSampleEngine": "event:>Engine>Shutoff>i4_2009_eng",
    "shutOffSampleExhaust": "event:>Engine>Shutoff>i4_2009_exh",
    "starterVolume": 0.76,
    "starterVolumeExhaust": 0.76,
    "shutOffVolumeEngine": 0.76,
    "shutOffVolumeExhaust": 0.76,
    "idleRPMStartRate": 1.25,
    "idleRPMStartCoef": 1.2,
    "deformGroups": ["mainEngine", "mainEngine_intake", "mainEngine_accessories"]
    },
    "soundConfig": {
    "sampleName": "I4_2_engine",
    "intakeMuffling": 1,
    "mainGain": -12.5,
    "onLoadGain": 1,
    "offLoadGain": 0.57,
    "maxLoadMix": 0.73,
    "minLoadMix": 0,
    "lowShelfGain": 3,
    "lowShelfFreq": 150,
    "highShelfGain": 4,
    "highShelfFreq": 7500,
    "eqLowGain": 0,
    "eqLowFreq": 120,
    "eqLowWidth": 0.1,
    "eqHighGain": 0,
    "eqHighFreq": 500,
    "eqHighWidth": 0.1,
    "fundamentalFrequencyCylinderCount": 4,
    "eqFundamentalGain": 0
    },
    "soundConfigExhaust": {
    "sampleName": "I4_2_exhaust",
    "sampleSmoothingFactor": 0.3,
    "mainGain": 2.5,
    "onLoadGain": 1,
    "offLoadGain": 0.55,
    "maxLoadMix": 0.73,
    "minLoadMix": 0,
    "lowShelfGain": -12,
    "lowShelfFreq": 100,
    "highShelfGain": 7,
    "highShelfFreq": 4000,
    "eqLowGain": 0,
    "eqLowFreq": 120,
    "eqLowWidth": 0.2,
    "eqHighGain": 0,
    "eqHighFreq": 4000,
    "eqHighWidth": 0.1,
    "fundamentalFrequencyCylinderCount": 4,
    "eqFundamentalGain": -2
    },
    "vehicleController": {
    "clutchLaunchStartRPM": 1500,
    "clutchLaunchTargetRPM": 2000,
    "highShiftDownRPM": [0, 0, 0, 2800, 3800, 4300, 4700, 4700],
    "highShiftUpRPM": 6800
    },
    "lua": {
    "controller": "vehicles/your_vehicle/lua/pitch_lfo_cents.lua"
    }
    }
    }

    Explanation
    • Lua Script: Save the Lua script as pitch_lfo_cents.lua in the appropriate directory (e.g., vehicles/your_vehicle/lua/).
    • JBeam Modification: Add the "lua": { "controller": "vehicles/your_vehicle/lua/pitch_lfo_cents.lua" } line to your engine configuration in the JBeam file. This line references the Lua script to ensure it is executed for the engine sounds.
    Testing
    1. Save all changes: Ensure your Lua script and JBeam file are correctly saved in the respective directories.
    2. Load the vehicle: Start BeamNG.drive, select your vehicle, and listen for the pitch changes as the RPM varies.
    This setup allows for dynamic pitch modulation based on RPM, using a sequence of pitch values in cents to achieve a desired engine sound effect. Adjust the pitchSequenceInCents array in the Lua script to fine-tune the sound to your liking.
     
    #1 FFIVGUY, Jun 22, 2024
    Last edited: Jun 22, 2024
    • Like Like x 1
  2. Agent_Y

    Agent_Y
    Expand Collapse
    Jbeam/QA support
    BeamNG Team

    Joined:
    Jul 10, 2020
    Messages:
    10,434
    This actually makes more sense than expected, it used to just output complete nonsense but this looks like something that would actually work. Probably uses non-existent lua functions though, a typical problem it has with lua.
     
    • Like Like x 1
  3. FFIVGUY

    FFIVGUY
    Expand Collapse

    Joined:
    Feb 21, 2018
    Messages:
    603
    Would something like this actually be possible in-game?
     
    #3 FFIVGUY, Jun 22, 2024
    Last edited: Jun 24, 2024
  4. FFIVGUY

    FFIVGUY
    Expand Collapse

    Joined:
    Feb 21, 2018
    Messages:
    603
    Because it would definitely give more variety to the engine sounds
     
  5. Agent_Y

    Agent_Y
    Expand Collapse
    Jbeam/QA support
    BeamNG Team

    Joined:
    Jul 10, 2020
    Messages:
    10,434
    If you manage to code in the non-existent lua functions it references, maybe? But idk if lua can access fmod.
     
    • Like Like x 1
  6. FFIVGUY

    FFIVGUY
    Expand Collapse

    Joined:
    Feb 21, 2018
    Messages:
    603
    Who would know if lua can access fmod?
     
    #6 FFIVGUY, Jun 25, 2024
    Last edited: Jun 26, 2024
  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