vornay Posted July 26, 2018 Share Posted July 26, 2018 I know the Phaser engine has a high frame-rate, is there a preferred way of performing long-running tasks? Is there something already built in to Phaser 2/3 to do this? Link to comment Share on other sites More sharing options...
stupot Posted July 26, 2018 Share Posted July 26, 2018 you need to look into 'Web Workers' samme 1 Link to comment Share on other sites More sharing options...
vornay Posted July 28, 2018 Author Share Posted July 28, 2018 So the answer is "look into web workers" ? Link to comment Share on other sites More sharing options...
Tom Atom Posted July 28, 2018 Share Posted July 28, 2018 I was just solving this recently - AI thinking / computer generating in 2 games. for computer generating I had long time running loop. I changed it, that I record some variables, that allow me to re-enter method and continue where it ended last time. Then in my update() method I am calling it as long as it is generating. Something like bellow. Every frame I do as many steps as time "lastDelta * 0.9" allows me. Making only one step per frame would be bad - if you needed 10000 steps then it would take 166 seconds with 60 FPS: private _computerIsGenerating: boolean; // ------------------------------------------------------------------------- public update(): void { if (this._computerIsGenerating) { if (this.generate()) { this._computerIsGenerating = false; } } } // ------------------------------------------------------------------------- private generate(): boolean { let enterTime = this.time.time; let lastDelta = this.time.elapsed; do { // step generator let ready = this._generator.generateStep(); // found new puzzle if (ready) { return true; } } while (Date.now() - enterTime < lastDelta * 0.9); return false; } for AI thinking in another game, AI was doing lot of different things like board analysis, looking for some possible moves, etc. in one big loop. I broke it into states and whenever state is changed, I return and next frame I continue again with last state. vornay 1 Link to comment Share on other sites More sharing options...
vornay Posted July 29, 2018 Author Share Posted July 29, 2018 11 hours ago, Tom Atom said: I was just solving this recently - AI thinking / computer generating in 2 games. for computer generating I had long time running loop. I changed it, that I record some variables, that allow me to re-enter method and continue where it ended last time. Then in my update() method I am calling it as long as it is generating. Something like bellow. Every frame I do as many steps as time "lastDelta * 0.9" allows me. Making only one step per frame would be bad - if you needed 10000 steps then it would take 166 seconds with 60 FPS: private _computerIsGenerating: boolean; // ------------------------------------------------------------------------- public update(): void { if (this._computerIsGenerating) { if (this.generate()) { this._computerIsGenerating = false; } } } // ------------------------------------------------------------------------- private generate(): boolean { let enterTime = this.time.time; let lastDelta = this.time.elapsed; do { // step generator let ready = this._generator.generateStep(); // found new puzzle if (ready) { return true; } } while (Date.now() - enterTime < lastDelta * 0.9); return false; } for AI thinking in another game, AI was doing lot of different things like board analysis, looking for some possible moves, etc. in one big loop. I broke it into states and whenever state is changed, I return and next frame I continue again with last state. Cool, this is what I was thinking. Basically, do my AI calculations in between the 60FPS frames. Is there ever a time where you could do some mass calculations without updating the screen, except maybe with a spinning wheel, or progress bar? For example, if your program generated a random world for the player to explore, you might put that code in to an initialization section. Or is it just better to do all your calculations between the FPS? Link to comment Share on other sites More sharing options...
Tom Atom Posted July 29, 2018 Share Posted July 29, 2018 No, there is no such time - your game is running in single thread and you are in risk, that browser will say your game is not responsive. If you do not want to split your long running task into smaller steps, then you have to use Web Workers as @stupot suggested - it will start separate thread. Look at here: https://www.w3schools.com/htmL/html5_webworkers.asp Unfortunatelly, Web Workers are not supported in old browsers (not a big problem), you have to have its task in separate file and you have to take care of communication between your game and Worker - postMessage / onmessage. vornay 1 Link to comment Share on other sites More sharing options...
Recommended Posts