Why doesn't this work in 'while' method?

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

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

where do you call this function… _ready or _physics_process

Vikrant | 2021-06-06 03:53

from _ready():

Dumuz | 2021-06-06 03:56

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

Wakatta | 2021-06-06 05:47

:bust_in_silhouette: Reply From: wyattb

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

_yep

So change the line to if y >= 100:

Wakatta | 2021-06-06 05:37

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.

Dumuz | 2021-06-06 07:00

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

Wakatta | 2021-06-06 09:53