GitHub | Documentation | Become a Patron

clock.setInterval deltaTime



  • Hi, I'm currently using this.clock.setInterval to compute some time based positions.
    For that task I need the deltaTime within the interval scope (not the room.setSimulationInterval scope).

    Right now I'm computing the deltaTime on my own:

    var timestamp = this.clock.currentTime;
    this.clock.setInterval(function() {
        const delta = this.clock.currentTime - timestamp;
    
        //event tough refresh rate is set to 2000, the deltaTime is between about 1983 and 2013 
        [...] //code
    
        timestamp = this.clock.currentTime;
    }, 2000);
    

    It would be nice to get the local deltaTime passed in by the setInterval closure just like in room.setSimulationInterval:

    this.clock.setInterval(function(**deltaTime**) { 
        [...] //code
    }, 2000);
    

    Are there any other suggestions how to get the deltaTime within a local setInterval?


  • administrators

    Hi @RobinFalko,

    The clock instance already have a deltaTime property which updates at every tick. See clock's public properties here: http://colyseus.io/docs/api-timing-events/#public-properties

    Hope this helps! Cheers!



  • Hi endel, thanks for your reply.
    You're right but the delta time is computed for the tick of room.setSimulationInterval, not clock.setInterval if I understood that correctly.

    e.g.

    //clock.start() is called indirectly and ticking starts:
    this.setSimulationInterval(deltaTime => {
                [...] //let's say deltaTime is about 15
            });
    
    this.clock.setInterval(function() { 
        [...] 
        //the deltaTime of this closure should be about 2000
        //but instead this.clock.deltaTime is about 15 since it's computed at the tick of setSimulationInterval
    }, 2000);
    

    So each setInterval should have its own deltaTime since it has its own refresh rate.