#Error with Delta Time Recording (Elapsed Time)
10 messages · Page 1 of 1 (latest)
I don't know how to do this in script, but here is a visual example of how I tried to execute this timer
Thanks @livid bronze ! I'll surely give it a try and update you with it
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.
The event is being called each time when there’s a restart😃
@livid bronze Having some errors going on (I'm not good with nodes stuff, would like to have a file directly)
@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();
Thank you @vast heath , works perfectly now!