#Error with Delta Time Recording (Elapsed Time)

10 messages · Page 1 of 1 (latest)

random zinc
#

Hey there! I was creating a lens with a text which shows how much time you survived, but there is some glitch going on with the script that I've made. In starting, the time count works fine but soon after it starts to count randomly like 10*time. (I wasn't able to explain it well, have a look on the video)

livid bronze
#

I don't know how to do this in script, but here is a visual example of how I tried to execute this timer

random zinc
crude laurel
#

How many times are you calling startTimer()? Each time you call it, I believe you'll create an additional updateEvent so the timer will be increasing faster and faster for each time you start it.

random zinc
random zinc
vast heath
#

@crude laurel said it correct, here in LS you can have multiple updateEvent handler. Since you have created the updateEvent handler in startTimer() function, every time you call startTimer a new event handler gets created and uses your updateTimer() function. So suppose 5 times you call startTimer(), 5 handlers updating time and hence 5X the effects. You just need to create your updateEvent handler outside of function call to fix this.

#

// -----JS CODE-----
//@input Component.Text[] timerTexts

var time = 0;
var interval = 1;
//var timerEvent;

global.behaviorSystem.addCustomTriggerResponse("START_TIMER", startTimer);

function startTimer() {
time = 0;
displayTime();
}

function updateTimer(eventData) {
time += (eventData.getDeltaTime() / 1);
displayTime();
}

const timerEvent = script.createEvent("UpdateEvent");
timerEvent.bind(updateTimer);

function formatTime() {
return time.toFixed(2);
}

function displayTime() {
const formattedTime = formatTime();
for (var i = 0; i < script.timerTexts.length; i++) {
script.timerTexts[i].text = formattedTime;
}
}

startTimer();

random zinc