An attempt to improve my procedurally generated terrain function; loading freezes

Godot Version

4.4.1

Question

I have been able to figure out months ago how to use a noise generator to place terrain tiles on each coordinate individually. I decided to start tinkering with this function again, but to include transition tiles. I figured I can do so with a tilemap. Screenshot of what I have below:

However, when I start the game, it just loads indefinitely. This has been a consistent issue, despite multiple attempts. I have no idea why. Code below:

extends Node2D

@export var noise_height_text : NoiseTexture2D
var noise = FastNoiseLite.new()

@onready var terrain: TileMapLayer = $Terrain

var width: float = 300
var height: float = 300

var grass_threshold: float = -0.22

# Called when the node enters the scene tree for the first time.
func _ready():
  generate_world()

func generate_world():
  #Sets the seed number to a random 32 digit integer, allowing for new terrain to be generated
  noise.seed = randi()
  var cells = []
  var cells2 = []
  for x in range (-width/2, width/2):
    for y in range (-height/2, height/2):
    #Gets noise value to determine which tile to place
      var noise_value: float = noise.get_noise_2d(x,y)
      #Uses noise value to place grass tile
      if noise_value >= grass_threshold:
        cells.append(Vector2i(x, y))
        terrain.set_cells_terrain_connect(cells, 0, 0)
      #Uses noice value to place sand tile
      elif noise_value > -0.3 and noise_value < grass_threshold:
        #Determines which sand tile to use
        cells2.append(Vector2i(x, y))
        terrain.set_cells_terrain_connect(cells2, 0, 0)

func _on_generate_world_pressed():
  generate_world()

Move the terrain.set_cells_terrain_connect() outside of the loops. You are calling it every time a new cell is added to the array and it will grow exponentially.

…whoops. That did fix it, but now I’m not seeing the transition tiles. Below is the code:

extends Node2D

@export var noise_height_text : NoiseTexture2D
var noise = FastNoiseLite.new()

@onready var terrain: TileMapLayer = $Terrain

var width: float = 300
var height: float = 300

var terrain_rng = RandomNumberGenerator.new()

var sand_threshold: float = -0.3
var grass_threshold: float = sand_threshold + 0.07

# Called when the node enters the scene tree for the first time.
func _ready():
	generate_world()

func generate_world():
	#Sets the seed number to a random 32 digit integer, allowing for new terrain to be generated
	noise.seed = randi()
	var cells1 = []
	var cells2 = []
	var cells3 = []
	for x in range (-width/2, width/2):
		for y in range (-height/2, height/2):
			#Gets noise value to determine which tile to place
			var noise_value: float = noise.get_noise_2d(x,y)
			#Uses noise value to place grass tile
			if noise_value >= grass_threshold:
				cells1.append(Vector2i(x, y))
			#Uses noice value to place sand tile
			elif noise_value > sand_threshold and noise_value < grass_threshold:
				#Determines which sand tile to use
				cells2.append(Vector2i(x, y))
			elif noise_value <= sand_threshold:
				#Determines which sand tile to use
				cells3.append(Vector2i(x, y))
	terrain.set_cells_terrain_connect(cells1, 0, 0)
	terrain.set_cells_terrain_connect(cells2, 0, 1)
	terrain.set_cells_terrain_connect(cells2, 0, 2)
	cells1.clear()
	cells2.clear()
	cells3.clear()

func _on_generate_world_pressed():
	_ready()

This is the result: