Modding Help args.dt, Techs, and Framerate Question

Discussion in 'Starbound Modding' started by PistolRcks, Apr 15, 2018.

  1. PistolRcks

    PistolRcks Void-Bound Voyager

    I'm currently making a tech mods where the tech starts draining energy after the player has been holding "F" for a period of time. Take, for example this:
    Code:
    timePressed = currentTime - pressStartingTime
    ...where both the variables `currentTime` and `pressStartingTime` were recorded with `os.clock()`.
    The `timePressed` value changes based on the framerate, when it should stay the same number. How does `args.dt` affect this? I noticed that the `args.dt` for techs stays at a constant 60 ticks per second, regardless of framerate.

    How do I rectify the issue of time changing based on framerate? Can I do that with args.dt, or do I have to do something else?
     
  2. Errors4l

    Errors4l Spaceman Spiff

    The script update rate is not fully tied to the frame rate. This means that even with 30 fps, the script should update 60 times a second: no time is 'lost'.

    In /debug you'll see that there are separate numbers for FPS and the script update rate. When the FPS dips the script update rate will remain 60. That said, the script update rate won't always be 60, especially when there's a bunch of code running in the background (try /boxes in /debug and you'll see).
    I'm not sure if the update rate will compensate for earlier dips by going over 60 for a while. If it does, that's why the update rate is over 60 in the below screenshot.
    [​IMG]

    Either way, you could measure the the time lost by comparing os.clock() every frame. Add the difference to a global each frame and you can see how far your script is ahead or behind at any given time.
    Some unoptimized code that may or may not work:

    Code:
    local timeDifference = 0
    local previousTime
    
    function update(args)
      local currentTime = os.clock()
      if not previousTime then
        previousTime = currentTime
      else
        local diff = currentTime - previousTime - args.dt
        timeDifference = timeDifference + diff
    
        local msg = string.format("Current time: %.4f | Previous time: %.4f | Difference: %.4f | Total Difference: %.4f", currentTime, previousTime, diff, timeDifference)
        sb.logInfo(msg)
    
        previousTime = currentTime
      end
    end
    PS. Yes, args.dt is a constant and it doesn't make a lot of sense that it's an argument to the update function.
     
    Last edited: Apr 15, 2018
  3. bk3k

    bk3k Oxygen Tank

    Simply incriminating with args.dt is reliable enough that you don't really need os.clock(). The only case where I've needed os.clock() was for a machine that will still function even when the chuck or world is unloaded so that I can see how much time has passed. But that's not much of a concern for tech mods.
     
  4. PistolRcks

    PistolRcks Void-Bound Voyager

    So, how did you utilize `args.dt` to measure time? I'm still (a little) confused on this.

    While I understand this solution, I think that this would use too much processing power per tick.

    EDIT: No, past self, this wouldn't take too much processing power. And it also doesn't work because, no matter what the framerate, the interval between update ticks stays the same (even though it doesn't). Framerate affects os.clock (around 4 seconds pass for every second recorded in Lua at 30 fps, while at 60 fps, around 2 seconds pass).

    Also a little sidenote, seconds recorded in the tech script actually take (about) two times the length of regular seconds, even though I'm playing at full speed., where the script update tick should equal the framerate.
     
    Last edited: Apr 15, 2018
  5. PistolRcks

    PistolRcks Void-Bound Voyager

    Nevermind, now I understand how this works.
    After looking at blinkdash.lua, I understand how args.dt is supposed to be used. I get now that every tick, you're supposed to subtract a timer by args.dt.
    It makes sense to me now. Thanks for the help.
     
  6. Errors4l

    Errors4l Spaceman Spiff

    This is why I subtracted by args.dt in the example code, but glad you figured out what you needed to know!
    Edit: Never mind, I think I understand what you mean now. You'll still run into the issue that args.dt won't be identical to (currentTime - previousTime) but it's close enough that it shouldn't matter.
     
    Last edited: Apr 16, 2018

Share This Page