# Conditionals

Conditionals let you run code only in some situations, letting you create much more interesting behavior.

# If

The most common conditional is an if statement.

// Prints "Going up" only if vertical speed is upwards
if vsp < 0 {
    print("Going up")
}

Or more generally:

if <a boolean, something true or false> {
    <code that should only run if it's true>
}

(See boolean)

if statements let you run code only if some condition is true.

if state == PS_WALK { // While walking
    walk_speed += 0.4// Keep increasing walk speed
} else { // While *not* walking
    walk_speed = 0 // Reset walk speed to very low
}

You'll often want to use boolean's logical connectors and and or to handle more complex conditions, such as if window == 3 and window_timer == 1

Immediately after the if block, you can put an optional else block, which will run any time the if condition is false.

You can check a series of conditions with else if like

if charge <= 0 {
    print('no charge')
} else if charge < 100 {
    print('charging')
} else {
    print('maximum charge!')
}

# Switch Case

Sometimes you might find you have a long if-else chain checking the value of something, like checking attack in attack_update.gml. In these cases you may use the switch-case statement to streamline the code, though it's entirely optional.

switch(<a variable>) {
	case <a value>:
        <code to run if the variable equals this value>
        break;
    case <another value>:
        <code to run if the variable equals this value>
        break;
    // More cases
}

For example:

// hit_player.gml
switch(my_hitboxID.attack) {
	case AT_JAB:
        fx_id = jab_fx;
        break;
    case AT_FAIR:
        fx_id = fair_fx;
        break;
    case AT_UAIR:
        fx_id = uair_fx
        break;
    // More cases
}
// Use fx_id

break is a special statement that ends the current block of code, here ending the switch statement.

If your switch case has less than 3 items in it, it'd be clearer to use if.