Sign in to follow this  
vornay

possible to do long running algorithms?

Recommended Posts

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?

 

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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?

 

 

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.