On game update and forever

what’s the difference between “on game update” and “forever”?

2 Likes

idk

2 Likes

Good question, @Mega0star01 !

At first glance, the two containers do seem to serve similar roles. In general, you should use on update instead of forever. There are a lot of technical differences between the two containers, and the on update container is specifically designed for the MakeCode Arcade environment, whereas the forever container is used in other MakeCode environments (like micro:bit).

Code inside of a forever loop runs “in the background,” separately and independently from your game. These forever loops are great for things like background music.

Code inside of on game update loops runs frequently – usually each time the screen updates, but the MakeCode Arcade environment determines how frequently the code runs. This is where you put your code that needs to run each time “something” changes in your game.

Again, they are really similar. Unless you actually need to use a forever loop, you should use an on game update loop in MakeCode Arcade.

For reference:

4 Likes

An important thing to note is that the code in the on game update blocks needs to finish before the game will update again. Here’s an example that uses a forever loop.

Now … what would happen if you changed this project to use an on game update loop instead? Give it a try and see what happens!

So, if you need a loop to run so that it does not interfere with your game, then use a forever loop. Otherwise, you probably should use an on game update loop instead.

5 Likes

thank you @AlexK, well now it makes sense.

3 Likes