[SOLVED] How to make Wait / delay?

how would add wait to my game??

Hi @Fus_ion,

If you mean like a pause menu where the main gameplay state isn’t updating but you are able to go through menu options, then you can set the following property to zero:

this.app.timeScale = 0.0;

To resume execution just set it back to 1.0. While it’s set to 0.0 all update and postUpdate script callbacks will not get executed.

2 Likes

no, I mean like the script waiting 3 seconds then going back to running the script

Here is a simple script that implements a simple pause timer. Set this this.pause = true; at any point and your update method will stop executing.

MyScript.prototype.initialize = function(){

    this.pause = false;
    this.pauseTime = 3;
    this.timer = 0;
};

MyScript.prototype.update = function(dt){

    if( this.pause === true ){

        this.timer += dt;
        
        if( this.timer >= this.pauseTime ){
            this.pause = false;
            this.timer = 0.0;
        }else{
            return;
        }
    }

    // things to do while unpaused
};

You can use set Timeout function. Here is its syntax:

setTimeout(() => {
        this.ischeck= true;
    }, 1000);

here this.check will get true after 1000 milliseconds i.e. 1 second. You can change the time as you want.

2 Likes

setTimeout is good though be careful when using it as part of your game logic, since it runs even when the browser tab is minimized or with no focus.

1 Like

whats the difference between settimeout and setinterval?

set timeout is a simple block of code in which the code is enters after a particular time and set interval is a function which is called again and again at the particular time.

Example of setInterval :

coinInterval = setInterval(function() {
                
                        if (number <= target){
                            clearInterval(coinInterval);
                      
                        } 
                        else
                            number = number -1;
                }
            }, 1000);

Here this function will be called after 1 second and if you want this to stop then you can simply call simple clearInterval of the variable.

One more way to have something happen after a certain delay is to use a “null tween”. Of course, you have to have the tween library loaded for this to work.

         this.app.tween({}).to({}, 1, pc.Linear).delay(0)
            .on('complete', function() {
            // do whatever here
        }, this).start();

You are essentially tweening nothing, and using the .on(‘complete’,function() to trigger some change after the combined interval created by the tween duration (“1” in this case) and the delay (“0” in this case).

7 Likes

I know there has been a 2-year gap but do any of yall know if there is a way I could do this where It plays an animation(legacy) and then delays and then plays another animation and delays and then loops to a final third animation? I know that’s oddly specific but I am trying to make a cinematic-like opener for a boss in my game but idk how to delay the animations between each other. The boss drops from the sky in the first animation and in the second animation the boss does a taunt and then for the final animation the boss just runs at you because well that’s how I coded him.:sweat_smile: The code that I have made doesn’t seen to work…
Here is my script for the Intro boss animation; any help would greatly be appreciated…

var Bosshandler = pc.createScript('bosshandler');

Bosshandler.attributes.add('animations', {
    type: 'json',
    schema: [{
        name: 'spawn',
        type: 'string',
    }, {
        name: 'stand',
        type: 'string',
    }, {
        name: 'scream',
        type: 'string',
    }, {
        name: 'run',
        type: 'string',
    }]
});
Bosshandler.prototype.initialize = function() {
    this.timer = 0;
this.rootentity = this.app.root.findByName('Boss');

this.direction = this.animations.spawn;
this.setDirection(this.animations.spawn);

};
Bosshandler.prototype.update = function(dt) {
if(this.loop == true){this.Introver();}
if(this.start == true){this.timer +=0.01;}
};

Bosshandler.prototype.setDirection = function (direction) {
    this.direction = direction;
    this.entity.animation.play(direction, this.blendTime);  
};

//where intro starts 
Bosshandler.prototype.INTRO = function() {
this.start = true;
this.setDirection(this.animations.spawn);
if(this.timer < 1.73) return;//delay
this.setDirection(this.animations.scream);
if(this.timer < 5.36) return;//delay
this.loop = true;
};
Bosshandler.prototype.Introver = function() {
this.timer = 0;
this.setDirection(this.animations.run);
this.rootentity.script.enemy.enabled = true;
};

Hey, legacy animation doesn’t throw any event when animation is completed so you will have to manage it manually.
At the start of each animation, you can get its duration and can set the timer accordingly using setTimeout or in the update function (given by Leonidas above), and then when it’s completed, you can simply add the delay before going to the next animation.

2 Likes

:+1: thank you I’ll try that.