From c8c2a10c89b068ad6f1037ce51dadfa2bc165607 Mon Sep 17 00:00:00 2001 From: Muffin Date: Sat, 2 Nov 2024 11:49:09 -0500 Subject: [PATCH] XeroName/Deltatime: don't report delta time of 0 (#1741) If projects do things like "5 / delta time", they'll break horribly when delta time is 0, so let's not return that. --- extensions/XeroName/Deltatime.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/extensions/XeroName/Deltatime.js b/extensions/XeroName/Deltatime.js index bb99cc01dc..07c6b3eb3d 100644 --- a/extensions/XeroName/Deltatime.js +++ b/extensions/XeroName/Deltatime.js @@ -21,7 +21,15 @@ vm.runtime.on("BEFORE_EXECUTE", () => { const now = performance.now(); - deltaTime = previousTime === 0 ? 0 : (now - previousTime) / 1000; + + if (previousTime === 0) { + // First frame. We used to always return 0 here, but that can break projects that + // expect delta time to always be non-zero. Instead we'll make our best guess. + deltaTime = 1 / vm.runtime.frameLoop.framerate; + } else { + deltaTime = (now - previousTime) / 1000; + } + previousTime = now; });