• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

SOLVED While vs Do?

G

Gudiwa

Guest
Cant tell when to use "while" and "do-until". Also why should i use one of these two when i could just use "if"
 

FrostyCat

Redemption Seeker
Like your other question, this is another one that a quick glance at the Manual would have answered. Learn to use its Index tab to look up unfamiliar terms.
  • while is for repeating a block of code if the condition is true, and do so until the condition becomes false. No other outside code runs as long as the loop is running.
  • do-until is for repeating a block of code at least once, and do so until the condition becomes true. No other outside code runs as long as the loop is running.
  • if is for running a block of code just once if the condition is true, and skip past it if the condition is false.
 

woods

Member
while.. this will repeatedly check for a non-collision until one is found, and move there.. and then run the rest of the code

GML:
{
while (!place_free(x, y))
   {
   x = random(room_width);
   y = random(room_height);
   }
}

if.. this will check for a non-collision once and move there if its a free space, ..if not it wont move there... and then run the rest of the code

Code:
{
if (!place_free(x, y))
   {
   x = random(room_width);
   y = random(room_height);
   }
}

right?
 
Top