WIP How do I make a scenario vehicle follow a Script AI path?

Discussion in 'Programming' started by LCS, Apr 7, 2021.

  1. LCS

    LCS
    Expand Collapse

    Joined:
    Apr 7, 2021
    Messages:
    3
    I've been getting into scenario creation, and was looking around the files for the vanilla scenarios hoping to learn more about how they work. That was then I stumbled across "East Coast Chase #1" which has a .track.json file - the same type of file that you get from saving a Script AI Path. Looking further into it, it seems that the path taken by the fleeing vehicle in the scenario is actually a recorded path. I have since then been trying to find a way to make that work with a custom scenario I'm making myself, but I have two main problems; first one is, I couldn't find the .prefab file for that scenario, so I couldn't figure out how the vehicle is setup to work. And also, looking through the .JSON didn't help much on figuring out how to set the AI vehicle to follow a .track.json file.

    Does anyone know how to make a vehicle follow a recorded path, or at least have a clue on how to set it up? Any help would be appreciated.
     
  2. angelo234

    angelo234
    Expand Collapse
    Programmer
    BeamNG Team

    Joined:
    Aug 11, 2017
    Messages:
    540
    First create a Lua file in your scenario's mod folder of the path "your_scenario_mod_name/levels/the_map_name/scenarios" folder named the same as the prefab's file name (I believe?). And paste this code into there and modify the parts to make it work:

    Code:
    local M = {}
    
    local function onRaceStart()
        --This loads in the script ai recording json file
        local file = readJsonFile("path to script ai recording json file")
     
        --Set script ai recording parameters
        file.recording.loopCount = 1
        file.recording.loopType = "firstOnlyTeleport"
     
        --This plays the recording
        scenetree.findObject("name of the vehicle in the map"):queueLuaCommand('ai.startFollowing(' .. serialize(file.recording) .. ')')
    end
    
    M.onRaceStart = onRaceStart
    
    return M
    I've only tested the code inside the onRaceStart function individually in free roam, but the rest of the surrounding code should make it work for scenarios.
     
  3. LCS

    LCS
    Expand Collapse

    Joined:
    Apr 7, 2021
    Messages:
    3
    Oooh, thank you! That helps a lot! I implemented it into my already existing .lua file, but now I've got an issue where the scenario doesn't load in properly anymore, probably due to my poor syntax skills. Code looks like this inside the file:


    Code:
    local M = {}
    
    local function onRaceStart()
        --This loads in the script ai recording json file
        local file = readJsonFile("levels\west_coast_usa\scenarios\WCH1\SunburstP1.track.json")
    
        --Sets script ai recording parameters
        file.recording.loopCount = 1
        file.recording.loopType = "firstOnlyTeleport"
    
        --Plays the recording
        scenetree.findObject("cop_sunburst_1"):queueLuaCommand('ai.startFollowing(' .. serialize(file.recording) .. ')')
    end
    
    M.onRaceStart = onRaceStart
    
    return M
    Trying to find what i did wrong here. Probably a very dumb mistake.
    EDIT: turns out, I messed something up in the scenario files, and it wasn't loading properly even without the changed code. I fixed it now, but I'll have to try and implement the path script tomorrow. For now though, is the syntax correct in the code block?
     
    #3 LCS, Apr 9, 2021
    Last edited: Apr 9, 2021
  4. angelo234

    angelo234
    Expand Collapse
    Programmer
    BeamNG Team

    Joined:
    Aug 11, 2017
    Messages:
    540
    The only thing you need to change is the back slashes to forward slashes of the json file directory. So it should just look like this instead:
    Code:
    local file = readJsonFile("levels/west_coast_usa/scenarios/WCH1/SunburstP1.track.json")
    Other than that, everything looks good to me. I also tested the code out in a test scenario I made and it works just fine so it should work for you. :)
     
  5. LCS

    LCS
    Expand Collapse

    Joined:
    Apr 7, 2021
    Messages:
    3
    It worked perfectly! Thanks for the help!
     
    • 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