Is there a fire once like method?

Is there any internal coding to fire something once within the update function playcanvas uses?

So for instance let’s say I want to set an animation with a isPressed but it fires only once instead of continuously which usually results to a freeze of animation since it keeps firing.

Not 100% sure on what you are asking here. In the case of isPressed, you have wasPressed which is only true on the frame that the button is first pressed.

Yea I know that wasPressed is a function available to me, what I’m asking is it possible to do something like this more simpler using some sort of javascript code I don’t know about. Otherwise the work arounds I have are fine for example :

var TriggerOnce = pc.createScript('triggerOnce');

TriggerOnce.prototype.initialize = function() {
    this.once = 'Play me once';
    this.keepGoing = 'Play me a lot';
    this.playState = 'none';
};

TriggerOnce.prototype.update = function(dt) {
    
    if(this.app.keyboard.isPressed(pc.KEY_1)){
        
        console.log(this.keepGoing);
        
        if(this.playState != 'played'){
            this.playOnce();  
        }
        
    } else if(this.app.keyboard.wasReleased(pc.KEY_1) && this.playState === 'played'){
        
        this.playState = 'none';
        
    }
};

TriggerOnce.prototype.playOnce = function(){
    
    console.log(this.once);
    this.playState =  'played';
    
};

Fire something once while still firing other things constantly within the ‘isPressed’ function.

Like there is a Trigger Once While True function in the game engine Construct 3 as well as Unreal Engine 4 with Do Once. Was curious if there was something like this for Playcanvas, or some system that is more optimized that I don’t know about within coding paradigms or javascript.

There is an event listener function once that registers to an event and only triggers on the first time the event fires. That’s the closest I can think of.

This is pretty amazing! This was pretty much what I was looking for, and infact it’s even better! Thanks :slight_smile:

Here is the code:

var TriggerOnce = pc.createScript('triggerOnce');

TriggerOnce.prototype.initialize = function() {
    
    this.playOnce = { };
    pc.events.attach(this.playOnce);
    
    this.playOnce.once('test', function (str) {
        console.log(str);
        this.entity.translate(0,0.2,0);
    },this);
    
};

TriggerOnce.prototype.update = function(dt) {
    
    if(this.app.keyboard.isPressed(pc.KEY_1)){
        
        this.playOnce.fire('test','Play Once');
         
    } else if(this.app.keyboard.wasReleased(pc.KEY_1)){
        
        this.playOnce.once('test', function (str) {
            console.log(str);
            this.entity.translate(0,0.2,0);
        },this);
        
    }
};