1. Trouble with the game?
    Try the troubleshooter!

    Dismiss Notice
  2. Issues with the game?
    Check the Known Issues list before reporting!

    Dismiss Notice
  3. Before reporting issues or bugs, please check the up-to-date Bug Reporting Thread for the current version.
    0.36 Bug Reporting thread
    Solutions and more information may already be available.

Working out a timer, looking for best option in the game that understands pausing physics

Discussion in 'Troubleshooting: Bugs, Questions and Support' started by krallopian, Mar 12, 2019.

  1. krallopian

    krallopian
    Expand Collapse
    NC114-85EKLS
    BeamNG Team

    Joined:
    Dec 5, 2013
    Messages:
    984
    Long story short, I've been working on a timer and quickly realized using the onUpdate(dt) isn't the best idea seeing it counts 3 real world seconds, not 3 seconds in game. So if you are in slow motion the timer doesn't slow down, and same with pausing the physics etc..

    Curious what options I have?

    I remember at some point someone suggested, "If you want at timer just use. . . . . . . . " and that's where my memory stops! Haha, it had something to do with, "it's good for about a quarter of a second accuracy but if you want more it won't be so good."

    Hoping someone can help =)

    Currently this is what I'm getting using onUpdate(dt) but again no pausing, and no slomo allowed :(

    upload_2019-3-11_19-34-44.png
    --- Post updated ---
    I think the person was talking about, updateGFX(dt)

    I've just tried it and time isn't calculated, at least not for me. This code works with onUpdate(dt) but if I wrap it in "updateGfx(dt)" it doesn't work.
     
  2. krallopian

    krallopian
    Expand Collapse
    NC114-85EKLS
    BeamNG Team

    Joined:
    Dec 5, 2013
    Messages:
    984
    (this is the sixth time Google has brought me here!!! I keep thinking, "YES! This is exactly what I'm looking for!!")
    --- Post updated ---
    I was coming here to post, "I got a response from thomatoes50 on how to do it, but I can't make it work, he said:

    "
    For the timer update the function like so
    Code:
    local function onUpdate(dtReal, dtSim, dtRaw)
    and you dtRaw now"

    So I tried, "onUpdate(dtReal)" I tried, onUpdate(dtSim), and tried, onUpdate(dtRaw) there was no difference.

    As soon as I went to paste that in here I realized, "Wait I didn't just try, local function onUpdate(dtReal, dtSim, dtRaw)"

    So I did.

    It works.

    IT WORKS!!!!

    Now I have a timer that counts time correctly if in slowmotion or not so that a slowmo drag race is no longer 45 seconds and instead the correct 8-15 it should be! @thomatoes50 you're getting a big'ol Christmas gift this year =D
    --- Post updated ---
    So I put together what I believe should work, if anyone knows any better please chime in but I hated that I couldn't find anything like this to even get started with while searching. I added a ton of comments to help the noobiest of noobs, but again please let me know where I went wrong if I did:

    First, without the comments so you can see the actual code here better:
    Code:
    local M = {}
    
    local myTimerVar = 0
    local startShowingTimerFunction = false
    
    local function onBeamNGTrigger(data)
        if data.event == "enter" and data.triggerName == "triggersName" then
            startShowingTimerFunction = true;
        else
            startShowingTimerFunction = false;
            
        end
    end
        
                    
    local function onUpdate(dtReal, dtSim, dtRaw)
        if startShowingTimerFunction == false then
            myTimerVar = 0
            return     
        end
    
        if startShowingTimerFunction == true then
        myTimerVar = myTimerVar+dtSim 
           print("You should see the time shown here: "..myTimerVar);
        end
    end
    
    M.onBeamNGTrigger = onBeamNGTrigger
    M.onUpdate = onUpdate
    return M 

    Then with the comments, to help see what each line actually does:

    Code:
    local M = {}  -- always at the very top of your mainLevel.lua file
    
    local myTimerVar = 0 -- intialize a local variable called "myTimerVar" it could be anything you want, as long as it is always the same.  This starts at 0 representing "0 seconds" in this case
    local startShowingTimerFunction = false -- this is a var that will change from false to true and back when you enter or exit a specified trigger
    
    -- This starts the "actual" code.  A function is declared that says "when anything in the game happens to "onBeamNGTrigger" check this block of code.  In this case, all triggers in game default to "onBeamNGTrigger" so when you enter or exit one it will look here unless you've specified otherwise
    
    local function onBeamNGTrigger(data) -- this declares the actual function, currently the name is "onBeamNGTrigger" and it has a data spot named, "data" and that's where things like, "enter" or "exit" will be brought in
        if data.event == "enter" and data.triggerName == "triggersName" then -- this is the actual trigger.  When you "enter" it AND it's name is "triggersName" it will execute it's code
            startShowingTimerFunction = true; -- Simply changing the value to TRUE when you enter
        else -- says "otherwise"
            startShowingTimerFunction = false; -- change the value to false, which in this case represents, "you haven't entered the trigger"
           
        end
    end
       
                   
    local function onUpdate(dtReal, dtSim, dtRaw) -- the fun part!  The actual timer function.  onUpdate dtReal/Sim/Raw means, "delta time between each update of Real time, Sim time, Raw time" in this case we want SIM time.  dtSim will react to simulation speed such as slow motion, or pausing. Useful for making a timer based on in-game speed rather than real-world speed
        if startShowingTimerFunction == false then  -- the above line is checked EVERY SINGLE moment so it will see this line and ignore the rest of the code if it equates to correct: "startShowingTimerFunction is false, so don't do anything else"  but if it ISN'T false, then it will continue below
            myTimerVar = 0 -- as a result, reset the time to 0 so that the next time you enter, it doesn't keep adding to the timer
            return     -- this is saying, "since the above lines equate positive, return away from this function and don't bother reading the rest of the lines"
        end
    
        if startShowingTimerFunction == true then -- once you've hit your actual trigger mentioned above, it will see that this has changed to true and start executing the line(s) below
        myTimerVar = myTimerVar+dtSim  -- the beauty of timers in programming, you are taking the initial myTimeVar which is "0" and saying it is now going to be 0 + dtSim which is time being counted
           print("You should see the time shown here: "..myTimerVar); -- the "print" function is great for debugging and is shown in the console (by pressing the ~ key on your keyboard) this would read "You should see the time being shown here: 0.01" and it would keep counting upward as long as you are within the trigger.
        end
    end
    
    M.onBeamNGTrigger = onBeamNGTrigger -- I still don't understand the purpose of these lines but.. I include them anyway because they seem important =D
    M.onUpdate = onUpdate -- I still don't understand the purpose of these lines but.. I include them anyway because they seem important =D
    return M -- this represents the end of the code
     
    • 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