####So i want to spawn enemies on a specific grid, this is my work around. The problem is that, often enough, it will instance too few enemies. Any advice? I gave as much as I thought was related to the problem.
var allowed_enemy_locations = [
Vector2(350, 50), Vector2(350, 150), Vector2(350, 250),
Vector2(450, 50), Vector2(450, 150), Vector2(450, 250),
Vector2(550, 50), Vector2(550, 150), Vector2(550, 250)
]
var number_of_enemies: int = rand_range(1,4)
var enemy_locations = []
func _ready():
if len(enemy_locations) < number_of_enemies:
add_enemy()
func add_enemy():
for location in range(number_of_enemies - len(enemy_locations)):
#Sets a temporary location of to-be-instanced enemy
var temp_location = allowed_enemy_locations[rand_range(0,8)]
# if temp location is already being used by another enemy re-roll location
if temp_location in enemy_locations:
temp_location = allowed_enemy_locations[rand_range(0,8)]
else:
#append temp_location to enemy_locations
enemy_locations.append(temp_location)
#instance enemy
var enemy_instance = EnemyScene.instance()
add_child(enemy_instance)
enemy_instance.position = Vector2(temp_location)
enemy_instance.characterName = str(location)
# connect death thingy shown below
enemy_instance.connect("death",self,"enemy_death",
[enemy_instance.characterName])
func enemy_death(enemy):
number_of_enemies -= 1
print(number_of_enemies)
if number_of_enemies < 1:
get_tree().reload_current_scene()
##Enemy Script
extends RigidBody2D
signal death
export(String) var characterName
export(int) var health = 100
func damage(damage):
health -= damage
if health <= 0:
emit_signal("death")
queue_free()
###Example of printing expected amount, but not instancing enough
Probably should mention that the Player is always on the left... just realized that made it hard to differentiate
