[SOLVED] How to stop timer save as high score?

Hello everyone, I have just started playcanvas two days ago. I have created a simple game where the player has to reach the destination as fast as possible. Fastest time gets saved as high score. I have only created the timer so far, and I have just realised that no one in playcanvas so far has asked on how to stop a timer, so far i have only seen reset timer. I have tried the app.timescale = 0, but it only freezes the game and not the timer at all.

this is my code:

var Timer = pc.createScript('timer');


Timer.attributes.add('textElement', {type: 'entity'});
// initialize code called once per entity
var minutes=0;
var sec=0;

Timer.prototype.initialize = function() {
    this.timer = 0;
    this.textElement.element.text = minutes + ":" + sec;
};

// update code called every frame
Timer.prototype.update = function(dt) {
    
    sec = this.timer++;
    if(sec>59){
        minutes++;
        this.timer = 0;
    }
    this.textElement.element.text = minutes + ":" + sec;

};

My questions are: How to stop the time once the user reaches the destination, and how to save the time so that it can be displayed as text as the user plays that same level again.

you can use a set interval or a settimeout to control a timer without using the update. example

var counter = 0;

var timer = setInterval(function () {

    console.log("turn no. " + counter);

    if (table.game.playerWon) {
        console.log('Player won');
    }

    if (counter >= 75 || table.game.playerWon) {
        clearInterval(timer);
    }

    counter++;

}, 100);
1 Like

Thank you for replying, but I do not understand how to implement the code you just showed. I am trying to save the best time, the update function is to show the user the time as they are playing and when they reach the end of the level(by colliding on a trigger) the timer stops, and it will be displayed as the high score when the user goes back to that level. How do I share my project so that you can check out what I am trying to do?

can you pass me the link to the project or if it is a private project you should add me to the group

does this work:

https://playcanvas.com/editor/scene/1318590

I have seen your code, I would put a setinterval in the initzialize because when you start it has to start and a counter in the update is very expensive, and it is also a bit imprecise and you can’t reset the deltatime, you can interrupt it with a boolean or when you exit the map with a function. if you don’t know how to use a setinterval in javascript here you have the link that explains how to use it setInterval() - Web APIs | MDN

Can you show me on how to use the setinterval in the initialise, im trying it now, and its showing an error saying its not defined. This is how I did it:

sorry I forgot to mention that in playcanvas the set interval is inside windows, use window.setInterval and to call the timeIt use this.timeit that is window.setInterval(this.timeIt,500) remember that 500 is the thousandths of mileseconds that this action will be repeated, i.e. it will repeat this action twice per second.

Just want to say thanks for your help, the window.setInterval was good but the problem was it coult not read my text element, therefore the code here was used:

time = setInterval(this.timeIt.bind(this), 100);

which worked, however I have a new problem, I cannot seem to call a variable from another script even though I have referenced the entity as shown below:
image

image

Hi @HelloCanvas! Your script has the name nextScene and not NextScene and do you need to use hasCollided or this.hasCollided in that script?

1 Like

when you use an oncolision the function becomes anonymous i.e. you have to declare the this before i.e. let that=this and use it the same this.entity.collision.on(“collisionstart”, that.collisionstart, that) and with that you would not lose the reference to this inside the anonymous function.

Sorry, the this.hasCollided was me just testing. when I used NextScene instead of nextScene, the hasCollided becomes undefined. However, when I used nextScene, it does not even work. The hasCollided = true, is to check if my player has reached the end of the level, and if he has, the timer would stop but it just keeps going instead

As far as I can see hasCollided is a global variable, that means you can use it directly in every script.

if (hasCollided === true) {

}
1 Like

You are right and it started working haha, but another prolem emerged :frowning:
as I proceed through the levels this error is popping up:


It cannot read my text element all of a sudden and:
image
the timer is overwriting each other even though i placed this in the code:
image

You reset this.timer, but I think you also need to reset minutes.

I figured it out, for some reason as I am moving on to the next level my player gets duplicated as it goes to the next scene even though I have destroyed the previous hierachy:



this is the sample scene that i just placed at the end to give you an example. As you can see, for some reason the player get duplicated multiple times and this sometimes happen randomly throughout the levels which causes the errors to appear

This problem is not related to the topic. Can you please search for an answer on the forum or create a new topic for this problem?

1 Like

You are right, thank you to @isvaku and @Albertos for helping for my problem with the timer. I was stuck on this for the entire day, and this solved it for me. Ill create another topic for this new problem

2 Likes