Im trying to make a countdown in lens studio that ends on a speficic time, but when i try to edit the timer it only goes from 58:45 and 57:00 minutes. How do i fix it, here is the script.
Iv`e tried many things but it wont work. I tried to correct the startTimer function to properly calculate the time remaining until the target timestamp.
Iv`e also tried to make sure to use the correct variable for the countdown duration.
This is the timer.js:
// ---- Start Timer Object Script ----
//@input Asset.Texture[] timerTextures
//@input Component.SpriteVisual[] columns
var maxTime = 0;
var timerType = 'countdown';
var time = 0;
var minutes = 0;
var seconds = 0;
var minutesTens = 0;
var minutesOnes = 0;
var secondsTens = 0;
var secondsOnes = 0;
global.timerSeconds = 0;
global.timerActive = false;
global.startTimer = function(targetTimestamp, type) {
if (!global.timerActive) {
if (type === 'stopwatch' || type === 'countdown') {
timerType = type;
print(timerType + ' is starting');
global.timerActive = true;
// Calculate the initial time difference
const currentDate = new Date().getTime();
const initialTimeDifference = targetTimestamp - currentDate;
// For countdown, set the countdown duration in seconds
time = type === 'countdown' ? Math.max(initialTimeDifference / 1000, 0) : 0; // Ensure time is non-negative
global.timerSeconds = time;
updateTime.reset(0);
} else {
print('type invalid');
}
} else {
print('timer already started');
}
}
global.stopTimer = function(callback) {
if (global.timerActive) {
global.timerActive = false;
print(timerType + ' finished');
if (callback) {
callback(arguments[1], arguments[2], arguments[3], arguments[4], arguments[5]);
}
} else {
print('No stopwatch/countdown initialized');
}
}
var updateTime = script.createEvent("DelayedCallbackEvent");
updateTime.bind(function(eventData, callback, cb) {
minutes = Math.floor(time / 60)
seconds = time - (minutes * 60)
if (minutes > 9) {
minutesTens = Number(String(minutes).charAt(0));
minutesOnes = Number(String(minutes).charAt(1));
} else {
minutesTens = 0;
minutesOnes = Number(String(minutes).charAt(0));
}
if (seconds > 9) {
secondsTens = Number(String(seconds).charAt(0));
secondsOnes = Number(String(seconds).charAt(1));
} else {
secondsTens = 0;
secondsOnes = Number(String(seconds).charAt(0));
}
updateTimerSprites();
// start change time
if (global.timerActive) {
if (timerType === 'countdown') {
time--;
global.timerSeconds--;
if (time >= 0) {
updateTime.reset(1);
} else {
global.stopTimer();
}
} else {
time++;
global.timerSeconds++;
if (time <= maxTime) {
updateTime.reset(1);
} else {
global.stopTimer();
}
}
}
});
function updateTimerSprites() {
script.columns[0].mainPass.baseTex = script.timerTextures[secondsOnes];
script.columns[1].mainPass.baseTex = script.timerTextures[secondsTens];
script.columns[2].mainPass.baseTex = script.timerTextures[minutesOnes];
script.columns[3].mainPass.baseTex = script.timerTextures[minutesTens];
}
And this is Script.js:
const targetTimestamp = new Date("2024-01-01T00:00:00Z").getTime();
global.startTimer(targetTimestamp, 'countdown');