# How to pause the game correctly while in the game menu?

**URL:** <https://forum.babylonjs.com/t/how-to-pause-the-game-correctly-while-in-the-game-menu/15975>\
**Category:** Questions\
**Created:** [November 23, 2020, 11:56pm UTC](https://forum.babylonjs.com/t/how-to-pause-the-game-correctly-while-in-the-game-menu/15975 "2020-11-23T23:56:27Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![splash27](https://avatars.discourse-cdn.com/v4/letter/s/76d3ee/32.png) [@splash27](https://forum.babylonjs.com/u/splash27)\
**Post date:** [November 23, 2020, 11:56pm UTC](https://forum.babylonjs.com/t/how-to-pause-the-game-correctly-while-in-the-game-menu/15975/1 "2020-11-23T23:56:27Z")

</div>

Hi everyone!

I am rendering the game itself and the main menu in the separate scenes. Like this:

```auto
_runRenderLoop() {
    this.engine.runRenderLoop(function () {
        this.currentScene.render();
        this.mainMenu.mainMenuScene.render();
    }.bind(this));
}

```

I want to pause the game when the user is in the main menu. How to do this?

In my understanding, the pausing includes:

1. Stop rendering, freeze at the current frame.
2. Stop physics.
3. Stop other processes like animations, sounds etc.
4. Stop keyboard/mouse listening. Here some clarifications needed, I expect that the engine will not put these events into the event queue when paused. I DO NOT want the following situation: pause, press several keys, unpause, engine handles all the keys which were pressed during the pause.

Is there any general way to do so?

I tried it something like this:

```auto
_runRenderLoop() {
    this.engine.runRenderLoop(function () {
        if (!this.paused) {
            this.currentScene.render();
        }
        this.mainMenu.mainMenuScene.render();
    }.bind(this));
}

```

But the screen behind the menu becames white and I don’t know how correct the pausing is.

---

<div class="post-metadata">

**Author:** ![msDestiny14](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/msdestiny14/32/9572_2.png) [@msDestiny14](https://forum.babylonjs.com/u/msDestiny14)\
**Post date:** [November 24, 2020, 12:34am UTC](https://forum.babylonjs.com/t/how-to-pause-the-game-correctly-while-in-the-game-menu/15975/2 "2020-11-24T00:34:23Z")

</div>

Hello!  
Curious, do you have a full playground for this? I’d love to help you out and games are one of my passions.

On a high level summary, when the pause event is triggered, you will want to call off everything currently running (pause sound, stop animations). Next, you can still accept input when the game is “paused” but then filter it out for your pause and unpause functions. Then when you transition back to unpaused you can resume your animation and sounds in another function.

---

<div class="post-metadata">

**Author:** ![splash27](https://avatars.discourse-cdn.com/v4/letter/s/76d3ee/32.png) [@splash27](https://forum.babylonjs.com/u/splash27)\
**Post date:** [November 24, 2020, 2:12am UTC](https://forum.babylonjs.com/t/how-to-pause-the-game-correctly-while-in-the-game-menu/15975/3 "2020-11-24T02:12:15Z")

</div>

Hi,

It’s barely possible to make a playground for this. Even if I will create a simple menu and the simple scene, it may not contain all the mechanics.

Regarding mechanics. All my own proccesses in the game that depends on time (like fire rate of guns, grenade delays etc.) I attach to the gaming loop directly, using:

```auto
this.scene.onBeforeRenderObservable.add(function(e) {doSomething()});

```

I never use `setTimeout` or `setInterval`. All my processes are synchronized with the gaming loop. Internal `BABYLON.Animation` instances and physics could be also synchronized with the gaming loop (I want to believe). That means, that if I will stop rendering the scene (like I did in my previous post) this should stop all native BABYLON processes and my processes as well and if I will resume rendering the scene then everything will continue from the last point (I want to believe).

Actually it seems to work so far, but I am receiving the white screen instead of the last frame. I hope it might be easily fixed, by setting some parameter or putting the last frame in the some kind of buffer somehow (don’t know how exactly).

I didn’t check sounds yet. It will be really painful to go into every function that uses sounds and place `paused` flag there. Of course I could create a separate component to handle every sound in one place. But I want to believe that the Babylon already has it inside. And even if the sound is not paused on pausing rendering loop, it might be paused by some method like `pauseAllSounds()` (I want to believe).

Going inside every `onBeforeRenderObservable` event handler of mine and pausing it is barely possible either.

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [November 24, 2020, 4:09am UTC](https://forum.babylonjs.com/t/how-to-pause-the-game-correctly-while-in-the-game-menu/15975/4 "2020-11-24T04:09:55Z")

</div>

Assigned @msDestiny14 on November 24, 2020, 4:09am UTC



---

<div class="post-metadata">

**Author:** ![msDestiny14](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/msdestiny14/32/9572_2.png) [@msDestiny14](https://forum.babylonjs.com/u/msDestiny14)\
**Post date:** [November 25, 2020, 7:19pm UTC](https://forum.babylonjs.com/t/how-to-pause-the-game-correctly-while-in-the-game-menu/15975/5 "2020-11-25T19:19:35Z")

</div>

Hi again,

So for sound I would recommend storing them all in a common data structure so you can iterate over and pause them in a single function. I am trying to see if I can create a basic demo or pseudo code that can show this.

Please let me know if you have more questions! 🙂 Are you still stuck on a white screen when you pause?

---

<div class="post-metadata">

**Author:** ![splash27](https://avatars.discourse-cdn.com/v4/letter/s/76d3ee/32.png) [@splash27](https://forum.babylonjs.com/u/splash27)\
**Post date:** [November 28, 2020, 10:34pm UTC](https://forum.babylonjs.com/t/how-to-pause-the-game-correctly-while-in-the-game-menu/15975/6 "2020-11-28T22:34:43Z")

</div>

No, I don’t know what to do with white screen. Probably before the pause we can dump image buffer into some kind of array and print it again, but this time in the Main Menu related scene. But I am not sure how BABYLON provides such opportunity.

Initially I expected that BABYLON will keep the last frame if I will stop rendering the scene, but it clears it instead.

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [November 29, 2020, 12:34pm UTC](https://forum.babylonjs.com/t/how-to-pause-the-game-correctly-while-in-the-game-menu/15975/7 "2020-11-29T12:34:46Z")

</div>

You can do this by ensuring that when you initialize the engine you setup in the options preserveDrawingBuffers to true.

---

<div class="post-metadata">

**Author:** ![splash27](https://avatars.discourse-cdn.com/v4/letter/s/76d3ee/32.png) [@splash27](https://forum.babylonjs.com/u/splash27)\
**Post date:** [November 29, 2020, 8:09pm UTC](https://forum.babylonjs.com/t/how-to-pause-the-game-correctly-while-in-the-game-menu/15975/8 "2020-11-29T20:09:18Z")

</div>

OK. The last frame remains, but there another problem appears.

My `mainMenuScene` uses a partially transparent background for menu (alpha = 0.5). When the game is not paused and both scenes are rendering, then everything looks good, I can see the main scene through the menu background. But when the game is paused, then the background of main menu becomes not transparent.

Not paused:

 ![not paused](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/2X/d/d34e4b08e9aa84268f37c769626a59c34b906fbc.jpeg)

Paused:

 ![paused](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/2X/0/05e74c8de7c4c4836522fdadfb68333a60e567bc.jpeg)

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [November 30, 2020, 12:31am UTC](https://forum.babylonjs.com/t/how-to-pause-the-game-correctly-while-in-the-game-menu/15975/9 "2020-11-30T00:31:48Z")

</div>

In this case it is totally different as if you need to render the menu, you still need to render everything behind it every frame for the blending to work.

So either you could use the createScreenshot apis and put the result in a layer or you would need to render the screen in a RenderTargetTexture and use this in a layer.

---

<div class="post-metadata">

**Author:** ![msDestiny14](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/msdestiny14/32/9572_2.png) [@msDestiny14](https://forum.babylonjs.com/u/msDestiny14)\
**Post date:** [December 15, 2020, 3:30pm UTC](https://forum.babylonjs.com/t/how-to-pause-the-game-correctly-while-in-the-game-menu/15975/10 "2020-12-15T15:30:01Z")

</div>

Unassigned @msDestiny14 on December 15, 2020, 3:30pm UTC


