While loops crashing the game

Hi all!
So I'm a first time developer making my first game, and I've been having some issues with the while loops. In my game, you can undock the crank to invert the game (docking it to invert again), allowing some extra features but decreasing your health. Whenever i try to implement a while loop in the players draw event for the decreasing health thing, the game crashes, and i cant figure out why. I'm also looking to create a check making the player dock the crank on game over using a while loop, but that just crashes the game again.
what am i doing wrong?

You're creating a loop inside the draw event. The game will crash since while does everything in a single frame and draw calls that loop in every frame. Try changing it to an if statement and see if that helps.

I tried- but the thing is, I need it to start when the crank is undocked and stop when its docked, and it needs to be a recurring event (the players life decreasing every 3 sec) and I'm not sure how to do it on an if statement. but what you suggested does make some sense for another thing I'm doing so thank you :slight_smile:

You're welcome,
And one more thing, you can create a timer inside a loop by creating an event that calls itself until the value of a variable changes—something like the code below. There are better ways of doing this, but this should work.

on undock do
 undocked = 1
 call "dockeffect"

on dock do
 undocked = 0

on dockeffect do
 wait 3 then
  //decrease the player's life
   if undocked == 1 then
    call "dockeffect"

Thank you so much! Didn't think of this :))

1 Like