Weird visual bug with PointLight2D

Godot Version

4.5

Question

So in my game I’m making a lantern puzzle involving PointLight2Ds on said lanterns. Because I don’t want the light to shine on top of the walls, I’ve included an occluder on them, but because I need the light to actually go past these tops of walls, I’m spawning in additional PointLights with the following code:

func spawn_in_lights(lantern_coordinates : Vector2):
	var upper_left_wall_top_cooridnates = get_used_cells_by_id(0, Vector2i(5, 2))
	var fit_coordinates : Array[Vector2] = []
	var walls = main_tile_map.get_wall_coordinates()
	
	for coordinate in upper_left_wall_top_cooridnates:
		var local_coordinate = map_to_local(coordinate)
		if local_coordinate.y + 20 == lantern_coordinates.y:
			fit_coordinates.append(local_coordinate)
			
	var i : int = 0
	for coordinate in fit_coordinates:
		if coordinate + Vector2(20 * 4, 20 * 4) in walls or coordinate == Vector2.ZERO:
			break
		
		var new_light = WALL_LIGHT.instantiate()
		add_child(new_light)
		
		if coordinate.x < lantern_coordinates.x:
			new_light.rotation_degrees = -90
			right_spawned_lights[lantern_coordinates] = new_light
			
			new_light.global_position = coordinate + Vector2(-20 * 3, 20)
			if i - 1 >= 0:
				var distance = fit_coordinates[i].distance_to(fit_coordinates[i - 1]) - 80
				new_light.scale.y *= (distance / 40) / 2
				new_light.global_position.x -= 40
			else:
				var closest_wall : Vector2
				for wall in walls:
					if coordinate.y - 20 * 2 == wall.y:
						closest_wall = wall
				if closest_wall.x == coordinate.x - 20 * 2:
					new_light.queue_free()
		else:
			new_light.rotation_degrees = 90
			spawned_lights[lantern_coordinates] = new_light
			
			new_light.global_position = coordinate + Vector2(20 * 5, 20)
		
			if i + 1 != fit_coordinates.size():
				var distance = fit_coordinates[i].distance_to(fit_coordinates[i + 1]) - 80
				new_light.scale.y *= (distance / 40) / 2
				new_light.global_position.x += 40
		
		i += 1

But there’s a visual bug that only happens if you spawn more than ten of the lanterns:

As you can see, the lights appear and disappear as you go up and down. What could be causing this?

There is a limit of max 16 lights that can light a texture. You’re probably hitting this limit.
You can find more info here:

https://www.reddit.com/r/godot/comments/11lu2qa/issue_with_light2d_point_lights_turning_on_and/

https://www.reddit.com/r/godot/comments/15tnapp/avoiding_the_limit_of_maximum_16_pointlights_2d/

1 Like