Broken Lua-triggered Sound Emitter

Discussion in 'Utilities and programming' started by Techeyy, Feb 2, 2024.

  1. Techeyy

    Techeyy
    Expand Collapse

    Joined:
    May 5, 2022
    Messages:
    32
    I'm attempting to use a Lua trigger to change the properties of a sound emitter based on the car's speed as it passes through. The general logic is working fine, but the volume and pitch changes are only being applied while the world editor is open.


    Code:
    local emitter = scenetree.findObjectById(scenetree.SoundGroup.obj:idAt(0))                                     -- # of corresponding sound emitter
    local function emitSound(data)
    
        if data.event == 'exit' then-- mute if exit                                                                                                  
            emitter.volume = 0
        end
    
        if data.event == 'enter' then --play sound when enter trigger
            local veh = scenetree.findObject(data.subjectName) -- get triggering vehicle
            local speed = vec3(veh:getDirectionVector()):dot(vec3(veh:getVelocity(veh:getRefNodeId()))) -- get car velocity
            emitter.pitch = (speed / 11.11) -- scale pitch based on velocity
            emitter.volume = (0.0288 * speed) - 0.2 + 1.2 -- scale volume based on velocity
    
        end
    end
    
    return emitSound
     

    Attached Files:

    • DesktopScreenshot20240201-23370835.png
  2. daniel-w

    daniel-w
    Expand Collapse
    BeamNG Team

    Joined:
    Jan 28, 2017
    Messages:
    282
    If it's an SFXEmitter, you'll have to delete it and create it again. You can't change the volume after it has been created sadly. For my flood mod, I do this:
    Code:
    local function setRainVolume(vol)
        local soundObj = findObject("rain_sound")
        if soundObj then
            soundObj:delete()
        end
    
        soundObj = createObject("SFXEmitter")
        soundObj.scale = Point3F(100, 100, 100)
        soundObj.fileName = String('/art/sound/environment/amb_rain_medium.ogg')
        soundObj.playOnAdd = true
        soundObj.isLooping = true
        soundObj.volume = vol
        soundObj.isStreaming = true
        soundObj.is3D = false
        soundObj:registerObject('rain_sound')
    end
    
    I'm hoping they make it so you can change the sound after creation. Maybe one day..
     
  3. Techeyy

    Techeyy
    Expand Collapse

    Joined:
    May 5, 2022
    Messages:
    32
    I found a fix, turns out that you can!
    Just needed to add
    Code:
    emitter:postApply()
    in this case:

    Code:
    local emitter = scenetree.findObjectById(scenetree.SoundGroup.obj:idAt(0))
    local function emitSound(data)
    
            local veh = scenetree.findObject(data.subjectName)
            local speed = vec3(veh:getDirectionVector()):dot(vec3(veh:getVelocity(veh:getRefNodeId())))
            emitter.pitch = math.max(speed * .09, 0.1)                                                                                                 
            emitter.volume = math.max(0.0288 * speed - 0.2, 0)
            emitter:postApply()
    
        if data.event == 'exit' then                                                                                       
            emitter.volume = 0
            emitter:postApply()
        end
    end
    
    return emitSound
     
    • Like Like x 2
  4. daniel-w

    daniel-w
    Expand Collapse
    BeamNG Team

    Joined:
    Jan 28, 2017
    Messages:
    282
    Ah, nice find! I'll have to try it out
     
  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