Check for randomized enemy instance

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

In my Top-down shooting game, I have an array of enemy like this:

var normal_enemy = preload(“res://scenes/normalEnemy.tscn”)
var runner_enemy = preload(“res://scenes/runnerEnemy.tscn”)
var flying_enemy = preload(“res://scenes/flyingEnemy.tscn”)
var grabber_enemy = preload(“res://scenes/grabberEnemy.tscn”)
var tanker_enemy = preload(“res://scenes/tankerEnemy.tscn”)

var enemies_list = [normal_enemy, runner_enemy, flying_enemy, grabber_enemy, tanker_enemy]

The problem is I want “grabber” enemy to exist on the map one at a time (for balancing reason, think it’s like a mini boss). I can check if it is still on the map but is there any idea or logic how to temporary prevent the program to randomize and spawn more “grabber” if there is one on the map (all enemies are spawned as instance node) and let the program randomly spawn a new one if the one on the map is dead.

:bust_in_silhouette: Reply From: aXu_AP

Pick a random enemy, change if it shouldn’t spawn (add in logic to check existence of grabber):

var choosen_enemy = null
while choosen_enemy == null:
	choosen_enemy = enemies_list[randi() % enemies_list.size()]
	if is_grabber_on_map and choosen_enemy == grabber_enemy:
		choosen_enemy = null
var new_enemy = choosen_enemy.instance()

Small tip: mark your code using {} button, it helps others help you :slight_smile: