This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

This is a test concept of something I'm trying to use on a larger scale. But even this test function freezes up my computer. I don't understand, I thought as long as I have a solve in the while function that will break it eventually, it would be fine. But it just locks up like the solve doesn't exist.

func _yep():
    var testing = false
    var y = 0
    while testing == false:
        if y == 100:
            testing = true
        for x in 101:
            y = y + 1
Godot version 3.3
in Engine by (367 points)

where do you call this function.... ready or _physicsprocess

from _ready():

Your code is pretty strange imho.
Why have double loops and checks?

The for loop gets executed in one cycle so the while loop becomes redundant
Unless the testing var is being used later it's pretty useless

func _yep():
    var y = 0
    while y < 100:
        y += 1

1 Answer

+1 vote
Best answer

Because y goes to 101. Not 100. See print values

func _yep():
    var testing = false
    var y = 0
    while testing == false:
        if y == 100:
            testing = true
        for x in 101:
            y = y + 1
            print(x, y)
        pass
by (810 points)
selected by

_yep

So change the line to if y >= 100:

I see that I messed up that script, 101 should have been 100, and then it worked.

Which shows me that the overall concept I'm trying to do can work, I probably have overlooked some script issue.

Thanks for pointing that out so I can confirm that concept of changing a variable outside of while and using it as a parameter to break while is indeed possible. I do a lot of loops within loops. I'm new to coding as GDscript is my first and only language I've been learning. So, don't know if its odd for people to use loops within loops, but it's been working up until now.

It's not odd to use loops in loops at all

for x in range(1, 10):
    for y in range(5, 8):
        for x in range(3, 12):
            var point = Vector3(x, y, z)

It's just that in your specific use case it was unnecessary to

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.