Using while in update method

I'm having a problem when trying to use " while " in the update method. Everytime I use while, the game goes not responding but it works just fine when trying to use " if ". The problem with " if " is that I can't hold the key I set for input.

Here's the script:

Spoiler
class Goodish < Scene_MenuBase  

  def start

    super

    hoardyer

  end

  

  def update

    super

    while Input.trigger?🙂LEFT)

      @dotty.x = @dotty.x - 5

    end

  end

  

  

  def hoardyer

    @background = Sprite.new

    @dotty = Sprite.new

    @background.bitmap = Cache.picture("Stany")

    @dotty.bitmap = Cache.picture("Dot")

    @dotty.x = 265

    @dotty.y = 270

    @background.opacity = 0

    @dotty.opacity = 0

    Graphics.wait(15)

    Graphics.fadeout(2)

    @background.opacity = 255

    @dotty.opacity = 255

    Graphics.fadein(2)

    RPG::BGM.new("Battle4", 100, 100).play

  end

end
 

I would appreciate if someone could help me. Thanks in advance! 🙂
Yes, in general, you shouldn't use loops in the update method if they rely on user input to end.


Update gets run once every frame, so just use if.
Yes, in general, you shouldn't use loops in the update method if they rely on user input to end.

Update gets run once every frame, so just use if.
I see. Thanks! But is there a way to loop the input so I will be able to hold that key instead of pressing it over and over again in order to move?
Of course you can loop - it just depends on where you place the commands.


The entire game is processed in a loop that operates every frame - that is why the update is called every frame.


If you program a new, local loop that contains nothing but your check for key press, then that loop will block the rest of the game loop, freezing the game.


If you want something to be processed and checked every frame, you need to place that something into the main game loop and let the engine handle it as part of the entire structure.


Placing that in the update with an if IS placing it inside a loop - you just need to change the logic into a two-step-check, instead of operating the action on the keypress, you have to use a switch to store if the key press is continuously or not, and operate the action on that switch as often as you want it inside the game loop/frame.


However, not everything in the game loop is updated every frame - a lot of the update functions are called only if something changed (similiar as to only calling the action if the switch was changed).


You'll have to check every part of the main game loop to see when it gets updated and then decide where in it to place which part of your functions.


That is why a lot of users prefer the parallel process events instead of scripted functions if there is to be a continuous check - the parallel process events already have been placed in the correct parts of the game loop by default.
Thanks for all the help guys! I managed to fix it by using Input.press? instead of Input.trigger? . Hope you the best! 🙂

Original thread: https://forums.rpgmakerweb.com/threads/using-while-in-update-method.53639/