+1 vote

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".

in Engine by (690 points)

1 Answer

+10 votes
Best answer

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")
by (3,916 points)
selected by

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.