Solved Adding Sound Effect for Final Lap in Quickrace

Discussion in 'Mod Support' started by bluerider598, Nov 28, 2017.

  1. bluerider598

    bluerider598
    Expand Collapse

    Joined:
    Aug 13, 2016
    Messages:
    196
    Is it possible to have a sound effect play when you begin your your final lap in a quickrace? Perhaps with a lua trigger? I'd like to find a way to play the sound effect when the lap number changes from 2 to 3/3 for example, think it would be a nice touch. Any help is much appreciated.
     
    #1 bluerider598, Nov 28, 2017
    Last edited: Dec 9, 2017
  2. bluerider598

    bluerider598
    Expand Collapse

    Joined:
    Aug 13, 2016
    Messages:
    196
  3. RyvyLo

    RyvyLo
    Expand Collapse

    Joined:
    May 15, 2014
    Messages:
    438
    Of course it's possible with the magic of programing !

    So, the tricky part here has been to find the variable containing the number of laps the player chosed to do. But I managed to find how to do it while looking at BeamNG.drive\lua\ge\extensions\scenario\quickRace.lua

    This code should work, I tested it on the gridmap slalom quickrace :

    Code:
    local logTag = 'quickraceSoundAtFinalLap'
    
    local finalWaypointName = 'wp_s19'                                           -- The name of the last waypoint, often the same as in the "finishLineCheckpoint" part of the json
    local lapsLeft = 0                                                           -- Variable that will contain the number of laps the player still has to do
    
    local function onRaceStart()
    
        local scenario = scenario_scenarios.getScenario()                        -- get the scenario info, especially the number of laps via the variable scenario.lapCount
        log('I', logTag,'Laps : ' .. scenario.lapCount)
      
        lapsLeft = scenario.lapCount
        log('I', logTag,'Laps left : ' .. lapsLeft)
    
    end
    
    local function onRaceWaypointReached(data)                                   -- This function gets called every time a vehicle crosses a waypoint
      
        if data.waypointName == finalWaypointName then                           -- If we crossed the final checkpoint, meaning we finished one lap
      
            lapsLeft = lapsLeft -1                                               -- We tell that we have one less lap to do
            log('I', logTag,'Laps left to do : ' .. lapsLeft)
          
            if lapsLeft == 1 then                                                -- If we have only one lap left
                Engine.Audio.playOnce('AudioGui','levels/GridMap/testSound.ogg') -- We play the sound effect
                log('I',logTag,'Sound effect played')
            end
        end
    end
    
    return {
        onRaceStart = onRaceStart,
        onRaceWaypointReached = onRaceWaypointReached,
    }
    To make it work for your quickraces, you need to save this text on a file called 'yourQuickraceName.lua' in levels\yourLevelName. Don't forget to change the finalWaypointName and the sound path on the file according to your case. You can also comment out the log lines, there are here just to see what the games does via the console.

    Only downside of this is that the custom sound effect will be played over the default checkpoint sound, so you'll hear both at the same time.

    Feel free to ask any question if there is something in the code you don't understand, or if there's something that doesn't work
     
  4. bluerider598

    bluerider598
    Expand Collapse

    Joined:
    Aug 13, 2016
    Messages:
    196
    Thank you! Though I just tried it, and even if I amplify the sound file in audacity, it's still played very softly. Any way to amplify the sound in the lua file?
     
    #4 bluerider598, Dec 10, 2017
    Last edited: Dec 10, 2017
  5. RyvyLo

    RyvyLo
    Expand Collapse

    Joined:
    May 15, 2014
    Messages:
    438
    Sadly I don't think it's possible to amplify it with lua. It's a long shot, is your audio file in the .ogg format ? Maybe it's that.
    Else, you'll have to amplify it a lot in audacity I guess
     
    • Like Like x 1
  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