How to break a function?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By JulioYagami

I want to break a function for example:

func _ready():
    if some_condition:
        print("true")
    else:
        # break
    print("test")

So I want it to be broke and doesn’t print “test”, only “true”.

:bust_in_silhouette: Reply From: SIsilicon

The break keyword in most programs, including Godot, stops the execution of an if statement or a for, while or any iterative loop. To ‘break’ out of a function, just use the return keyword.

func _ready():
    if some_condition:
        print("true")
    else:
        return # breaks execution of the entire function
    print("test")

Please note that according to your code fragment, this would either print out both true and test, or neither at all.

SIsilicon | 2019-02-24 14:16