This is a very very smart hack.
Let’s go through it line by line.
This line seems very weird but,
for i = PhysicsFPS, Time, PhysicsFPS do --// A better visualization will be to plug in numbers directly so we see what it's for.
Time_Yielded_In_The_For_Loop_Kind_Of += Heartbeat:Wait()
So in the real world, we get
for i = .0166, 0.01, .0166 do --// or something similar.
Either way, it’s clear that this loop is actually in place to yield more than a frame. So when inputs are something like
0.2 it will yield at least a somewhat similar time.
Because of the horrible lack of comments, this is rather mystifying BUT I am pretty sure this just add how many time have been “wasted” in the previous loop (even if it didn’t yield) to the “buffer”. Over time, the buffer accumulates enough time.
local seconds_add = Time - Time_Yielded_In_The_For_Loop_Kind_Of
if seconds_add > 0 then
CurrentBuffer += seconds_add
Again this seems mystifying so we put more numbers in.
local seconds_add = 0.01 - 0 --// Or something like 0.1 - 0.08, but it's also possible for this to be HIGHER than 0 for cases where the for loop did yield more than it should.
So, a check is in place to actually yield whenever that buffer has actually waited enough time.
if CurrentBuffer > PhysicsFPS then
CurrentBuffer = 0 --// Clears the buffer.
Heartbeat:Wait() --// Yield for the minimum time possible.
So tl;dr, it just yields every once in a while. My thought (possibly wrong) on the “inaccurate” time that this seems to have yielded is more or less likely due to the fact that
os.clock is not
std::chrono::high_resolution_clock or, even that is too inaccurate.
Very smart work. I salute you.