While in while not working

Godot Version

v4.2.1.stable.mono.official

Question

This while in while working just 3 times

func add_regionloaders(f_radius:int) -> void:
	var x:int = 0 - f_radius
	var y:int = 0 - f_radius
	while x <= f_radius:
		while y <= f_radius:
			var region_loader_area:RegionLoaderPoint = RegionLoaderPoint.new()
			region_loader_area.position = Vector2(x * G.REGION_SIZE, y * G.REGION_SIZE)
			add_child(region_loader_area)
			y += 1
		x += 1

but this working normal

func add_regionloaders(f_radius:int) -> void:
	var x:int = 0 - f_radius
	var y:int = 0 - f_radius
	while x <= f_radius:
		second_while(x, y, f_radius)
		x += 1
func second_while(f_x, f_y, f_radius): # FIX !!!!!
	while f_y <= f_radius:
		var region_loader_area:RegionLoaderPoint = RegionLoaderPoint.new()
		region_loader_area.position = Vector2(f_x * G.REGION_SIZE, f_y * G.REGION_SIZE)
		add_child(region_loader_area)
		f_y += 1

Why?

I reckon you need to reset your y variable where you inc x.

The second solution works because y is sent by-value to the func each time and so is refreshed.

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.