Any way to make speedtrap with lua?

Discussion in 'World Editor' started by DeWorD, Mar 22, 2024.

  1. DeWorD

    DeWorD
    Expand Collapse

    Joined:
    Jan 20, 2017
    Messages:
    152
    like in forza and probably in NFS games when u drive through a certain checkpoint it shows your current speed somewhere like in chat or i don't know wherever it would be easy to do xD

    Making a vehicle testing map and now got an idea of cornering as making a roundabout and a 1 checkpoint in there so could test how fast some cars are able to do cornering and other things too.

    Another thing came to mind that possible to have a timer what would start counting when drive through checkppoint-A and stops when drive through checkpoint-B?

    in scenarios this probably me could do, but i wanna it in freeroam lol

    id like to have speedtraps on those checkpoint spots if any way easily possible :)
    1098868-37fcd870e0f1d7c7c84b83b5752d52ad.png

    Thanks alot :)
     
  2. Barbent Servo GT

    Barbent Servo GT
    Expand Collapse

    Joined:
    Jun 4, 2023
    Messages:
    716
    Hi DeWorD :)
    I experimented with this code a bit. It works well but reacts sometimes (not allways) too slow. Means if you drive 200km/h through you will sometimes not see the speedtrap flash. Hope this could be the real thing... ;) Seems to be some kind of internal game engine rythm in witch the car jumps.
    In other games the flash light often is a visual overlay effect. I had no other idea to simulate that by a strong bright light lighting up the environment in a radius of 10 meters around the speedtrap. If you found a better solution to simulate that flash could you please let me know? Happy progamming and have a nice day :cool:

    Code:
    local M = {}
    
    local function onBeamNGTrigger(data)
        if data.triggerName == 'BeamNGTrigger_SpeedTrap01' then        -- Trigger Object
            if data.event == 'enter' then                            -- entered?
                local vehObj = be:getObjectByID(data.subjectID)        -- Subject
                local airspeed = vehObj:getVelocity():length()        -- get airspeed
                if (airspeed * 3.6) > 88 then                        -- exceeded [km/h]
                    -- Do some things like flashing a light
                end
            end
        end
    end
    
    M.onBeamNGTrigger = onBeamNGTrigger
    return M
    
     
    • Like Like x 1
  3. DeWorD

    DeWorD
    Expand Collapse

    Joined:
    Jan 20, 2017
    Messages:
    152
    Thank you very much for fast reply and that's lot's of information.

    Sadly i don't know how am i able to use this information like you do as i don't have much coding background lol

    I understand a bit but too less to get it working any way.
    If you have time to write i would like to know more about those. It could help other people too :)

    Trigger is a trigger what "launches" the code i guess?
    Is the trigger the lua box itself or do i need to make another box trigger for it as it has the name BeamNGTrigger_SpeedTrap01 in there?

    data event is just code for lua to work i guess? not much to know about it?

    local vehObj and local airspeed comes from (data.subjectID) and vehObj:getVelocity(), but what the ? ():length() ? means?
    With these codes code knows what information to look when something goes through trigger i guess?

    if (airspeed * 3.6) > 88 then `? so the airspeed of trigger vehicle needs to be higher than 88km/h for the flash to work?
    For it to show the "current airspeed"and record it for two seconds it would be it needs some kind of screen or chatbox or something?
     
  4. Barbent Servo GT

    Barbent Servo GT
    Expand Collapse

    Joined:
    Jun 4, 2023
    Messages:
    716
    Hey,
    it was randomly that I read you question.I had the same issue/idea some time ago so maybe I can help a bit more... I am not shure if I'm completely right with all I say, cause I reverse engeneered those things - and BeamNG is alpha so things can change... Please be shure to learn LUA basics and how variables work. In the code above "data.event" means "event" is a sub element of "data" and is given to the function "onBeamNGTrigger" by BeamNG. You need to know all the syntax usage!

    I try to go a bit more from scratch to explane. Please read all and try to understand more :cool:. This is not a step by step tutorial even if it looks like...

    1. There are objects on most maps like e.g. buildings. There are many more invisible objects available in the world editor. Browse them in several categories (picture). The "Trigger" that I mean is a LuaTrigger Object in the form of a cuboid. Place one in you map. Size it for your needs. Every object can have a name. Set the name of the trigger object to your mood. I named it "BeamNGTrigger_SpeedTrap01" above (not in the pic).

    2. In the top level dirctory of your map [BNGUSERFOLDER]/levels/[YOURMAP] create a file named mainLevel.lua. I strongly recomend a good editor like notepad++. Edit this file and place the code above inside. Restart BeamNG on every edit of this file!!! All LUA code for your map is placed here. Give good names to the objects if you plan more code.

    3. If any vehicle enters any LUA Trigger object BNG tries to call a function which must have the name "onBeamNGTrigger". Here you distinguish which trigger object it was. Here you distinguish if the vehicle entered or did exit, too. You identify the vehicle by "vehObj = be:getObjectByID(data.subjectID)" and finaly read out it's speed in the next line. It's measured in meters per second. So multiplying with 3.6 to km/h and compare (greater as) with a number. Set any speed you like.

    4. I did't gave you code for what happens if everything works fine now - so nothing works at all... You could play a sound or switch some lights and so on. If you like to learn these things I can recomend downloading the (beautiful!:)) map I made: "Barbent's Gambling Zone". Open the ZIP and look for the mainLevel.lua file. Here I use some of the functionality with triggers and do several actions with them. It's commented a bit (only for me till now). Feel free to copy and paste and experiment with own attempts.

    I'm sorry, but I don't think it makes much sense to give a complete step list what to do. If you understand some dependencies by reading some code and experimenting it can give you much more as I could ever write here...;)

    I hope this helps and if you have some further spcific questions you're welcome.

    The basic situation:
    2024-03-2317_43_16-BeamNGdrive-0313016018-RELEASE-Direct3D11-background.jpg
     
  5. DeWorD

    DeWorD
    Expand Collapse

    Joined:
    Jan 20, 2017
    Messages:
    152
    Yeaa im gonna need some time to learn i don't understand shit. Thanks for helping if i understand anything about it il put message :D
     
  6. Barbent Servo GT

    Barbent Servo GT
    Expand Collapse

    Joined:
    Jun 4, 2023
    Messages:
    716
  7. LegThePeg

    LegThePeg
    Expand Collapse

    Joined:
    Jun 4, 2022
    Messages:
    3,925
    Maybe you could ask Car_Killer for help as he has made speedtraps for Polish Roads, Fairhaven, and C1 Shuto.
     
    • Agree Agree x 3
  8. DeWorD

    DeWorD
    Expand Collapse

    Joined:
    Jan 20, 2017
    Messages:
    152
    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