Game over win is giving an error

Hi everyone,

I’m currently making a game in MakeCode Arcade, and I’m running into a weird issue with sprite flags. I have some logic that updates sprite flags based on certain conditions.

Here’s what I noticed:

  • When I use on game update, everything works fine—even when I trigger
mp.gameOverPlayerWin(mp.playerSelector(mp.PlayerNumber.One))

OR

game.gameOver(true)
  • But when I use a forever loop, I get this error:

    Program Error: sim error: failed cast on undefined
    
    

I made a couple of example projects:

  • Forever function example (errors when pressing A): LINK

  • On game update example (works fine): LINK

In the forever example, clicking A is supposed to run game.over(player 1 wins), but it fails with the error above. In the on-game update example, it works perfectly.

I’m wondering why using a forever loop with a pause causes this error, but using on game update every 100 ms does not.

Any ideas on how to fix this or why it’s happening?

Thanks!

1 Like

This is happening because the Game Over essentially deletes everything when it runs (not really, but that’s a good enough explanation).
When it deletes everything, it basically removes the thing that restarts forever loops, but if the loop is already running, it continues running until it is done. So you lose the game, but the pause block in the forever loop is running, then after the pause block you code tries to do something to those sprites, but those sprites don’t exist after the game over block ran, so the game errors.
This doesn’t happen with On Game Update blocks because the game over deleted the thing that restarts them, so even after the delay has passed, the code inside the On Game Update doesn’t run.

Here is a simple fix for your Forever code:

Thanks

1 Like