0 votes

If I have layered for loops. How can I break all of them in one solve?
(Simple example below, that I'd like to use in a more complex setting.)

for x in 10:
    if x = 5:
        for i in 3:
            if i > 1:
                for a in 15:
                    if a = 7:
                        break

How can I break all three loops once a = 7? Is this even possible?

Godot version 3.2
in Engine by (293 points)

2 Answers

+1 vote
Best answer

I think you should use a function, but another way would to be to have a boolean variable that is checked at the start of every loop that you would set to false when you want to break the loop. But that would require n-1 variables for n nested loops.

by (6,170 points)
selected by

Sorry, I noticed I never gave credit for this answer. Here is my long overdue thank you!

haha it's fine i answer a lot of questions

0 votes

Not sure if this is what you're looking for but you could put all loops into a function and return the function.

by (220 points)
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 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 webmaster@godotengine.org with your username.