Script "out of bouncing" at random indexes every time

Godot Version

v4.2.2.stable.mono.official [15073afe3]


So I have a tiny little script, which should make black-and-white simplex noise with thresholding:

extends Node2D

@export_range(1, 1024) var size: int = 64
@export_range(0, 256) var threshold: int = 256

var noise =
var image: Image
var data: PackedByteArray

func _ready():
	noise.noise_type = FastNoiseLite.TYPE_SIMPLEX
	noise.seed = 69

func _process(delta):
	image = noise.get_image(size, size)
	data = image.get_data()
	for i in range(size):
		for j in range(size):
			var value = data[i + j * size]
			var color
			if value <= threshold:
				color = Color.BLACK
				color = Color.WHITE
			image.set_pixel(i, j, color)
	$TextureRect.texture = ImageTexture.create_from_image(image)

And it outputs something like this:

But every time I try to increment @export var size: int it rises an “Out of bounds” exception.
But the thing is: with exactly the same input values (and even noise, I suppose, because of the seed), the index it gives up on is different every time. Isn’t it suppose to brake at the same spot if everything else is not changing?

When the exception occurs tho, the debugger says that size = 65 and data.count = 4096 (???)
Like image = noise.get_image(size, size) was executed before size value updated (so size = 64data.count = 64 x 64), but everything else was executed after that change (so for i/j in range(65)). What’s up with that?

This is because you are running loop multi times, out of limit, the default is 1024, if you want to create a infinite world generation then make it with chunks, I think you can increase the default loop value in the project settings or make the size to 64 or 256

What do you mean by “running loop multiple times”? Like simultaneously? I think not, because I’m not coroutining it or paralleling it in any other way, so each for { for { ... } } loop suppose to run one after another.

Any way, for now I’m calling get_image() every frame just because I’m playing for the moment. I’m planning to make the world in chunks, and they are not gonna be that big, but there’s definitely a problem (either in my code or in Godot itself) which is driving me crazy.

Running a loop for out of 1024 times is not supported in Godot

It is not the case:

  • If nesting loops does count:
      The script starts and produces a picture normaly even if initial size value is set to something like 500.
  • If nesting does not count:
      I simply do not set size value anything near 1024. My work PC are already struggling at numbers around 500.

And anyway, if it is the case, than why or how does the loop reach 80,000th iterations? And why does it break down at, for example, 8552nd iteration, when the data.count is 8192? Like 8551, 8550, …, 8193 – was ok?

You need to do step by step, like I have a data of 10,000 players leaderboard, so I will show this step by step like it will show first 100, then when user scroll down more, it will show more 100. And running loop for 500 times is not natural, why to run 500 times? If you want create a world generation then to this step by step like first run 100 loops, after a break, again run 100…

You are missing the point. This topic is not about the correct way to generate a world map or iterate through the large amounts of data. Please stick to the main question(-s) or consider to not flood the reply section.

Ok so use loop for 64 or 128 times