Solved Help with lua programming

Discussion in 'Mod Support' started by S.Ali.M, May 10, 2021.

  1. S.Ali.M

    S.Ali.M
    Expand Collapse

    Joined:
    Jan 12, 2021
    Messages:
    1,079
    Hello! How can I play an audio file just for 1 second? I'm working on a burglar alarm system and I want to add a blip sound to it, and I want to play that audio file when I want to turn off the alarm. I know I have to write playSFX("AUDIO") but idk how to change an end time for it, if I write it, it will play for ever as a loop. Please give me a code, I'm new at lua scripting. Thanks in advance!
     
  2. angelo234

    angelo234
    Expand Collapse
    Programmer
    BeamNG Team

    Joined:
    Aug 11, 2017
    Messages:
    540
    Something like this should work (I haven't tested the whole code out yet but I think it will work). And call "playSound" to play the sound effect for 1 second:

    Code:
    local M = {}
    
    local sfx_source = nil
    
    --1 second of playing sound
    local play_time = 1
    
    local timer = -1
    
    local function init(jbeamData)
        --Replace with your sound file
        local sound_file = "art/sound/fireloop.ogg"
       
        --Initialize sound
        sfx_source = obj:createSFXSource(sound_file, "AudioDefaultLoop3D", "", 1)
    end
    
    local function playSound()
        obj:setVolume(sfx_source, 1)
        obj:playSFX(sfx_source)
       
        timer = 0
    end
    
    local function updateGFX(dt)
        --Stop playing audio if 1 second elapsed
        if timer > play_time then
            obj:setVolume(sfx_source, 0)
            obj:stopSFX(sfx_source)
           
            --Stops timer
            timer = -1
        end
       
        --Only run timer if playing audio currently
        if timer ~= -1 then
            timer = timer + dt
        end
    end
    
    
    M.init = init
    M.playSound = playSound
    M.updateGFX = updateGFX
    
    return M
     
    • Like Like x 2
  3. S.Ali.M

    S.Ali.M
    Expand Collapse

    Joined:
    Jan 12, 2021
    Messages:
    1,079
    Thank you
     
  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