Login | Register

The fallthrough method

July 20, 2006, 18:20 by Calle
[loading]
-->
The fall through method is just what you guess; it a code that tests certain condition and depending on the results pass it on to the next step (or stop). This could be explained by this simple code:

Player got 100 hp and 50 ammo, but he has just fired and the variable can_fire is set to false. The programmer uses the following code to check out what is the case.

<div style="clear: both;">if (hp>0) {
//Player is alive
if (ammo>0) {
//Player is alive and got ammo
if (can_fire=true) {
//Player can shoot (because he got hp and ammo)
}
}
}
</div>

But is most typically using the switch statement:
switch (<expression>;)
{
case <expression1>: <statement1>; ... ; break;
case <expression2>: <statement2>; ... ; break;
...
default: <statement>; ...
}


The typical thing is that the code ends when a condition is met, or as this condition might be denying; is not met and therefore, check the next thing. Take for an example if you wanted to know what is the best combination of cards that the player has… As you only want to know the best hand; first check for the best hand, if found, break, if not found, continue look for the seconds best hand, and so on.

Look out for further articles on this subject, or a revised one since there are a lot of other things to write about it.

Comments

Loading comments... [loading]
.
Users logged in:

game maker articles, game maker examples, game maker tutorials, gmtutorials, game maker questions and answers, game maker crash course, how to create games