The two best ways I've noted to avoid using if statement are first structuring your program so it doesn't need to make choices, and the other is the switch statement. Sometimes if statements are the way to go, but in the case of several conditions, a switch will be better. But there are times where you can avoid making the program have to make a choice at all. For instance I was helping another user implement highscores. The idea was that the score would increase as the game was playing, but not while the game had ended. You can use an if statement to do that.
score_display -> Draw Actor -> Script Editor
- Code: Select all
if(game_state==0) // 0 standing for in game
{
score+=10;
}
That way when the player is destroyed, you can set game_state to 1, and that way in the highscore menu it wouldn't increase the score. But, you can also just add the score+=10 call in the player's draw event. Since the player only exists while you are playing the game, it makes sense that after the game mode ends (the player is destroyed) the score would stop increasing. This method bypasses the need for the if statement and the game_state variable. Another method would be to use EventDisable to disable the score_display's draw event while not in game mode. But the use of player's makes more sense, and it follows the object oriented approach. By using actors as objects, you are able to get rid of a lot of the decision making you normally would have to have.