Jump to content

possible to do long running algorithms?


vornay
 Share

Recommended Posts

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.
Link to comment
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?

 

 

Link to comment
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.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...