Is there a way to instance the same scene multiple times, without doing it manually?

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

In the game I’m developing, I randomly select a number of enemies from an array, some of which may be chosen more than once. The issue is, I can’t instance one scene multiple times, meaning only one ends up being instanced. Is there a way around this? Any help would be appreciated.

:bust_in_silhouette: Reply From: timothybrentwood

Here’s how I would do it:

var possible_enemy_scenes = [load("res://Enemy_A.tscn"), load("res://Enemy_B.tscn"), load("res://Enemy_C.tscn")]
var number_of_enemies = 20

func _ready():
	randomize()
	for enemy in number_of_enemies:
		var random_position = Vector2(rand_range(0, 720), rand_range(0, 720))
		instance_enemy_at_position(random_position)

func instance_enemy_at_position(pos : Vector2):
	var enemy_to_instance = choose_random_from_array(possible_enemy_scenes)
    var enemy_scene = enemy_to_instance.instance()
	enemy_scene.position = pos
	add_child(enemy_scene)
	
func choose_random_from_array(arr: Array):
	arr.shuffle()
	return arr.front()