Why my enemy Spawner does not work?

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

Hi i am working at a side scrolling space shooter and i made a ememy generator inspired by this guy and everything worked until i need to add one more enemy to the spawner (this video). I do every thing that he does but the first time it spawned enemy but by lasers did not collide with them and second time didn’t spawn enemies at all. I tried to reverted to what it was but now that did not work as well. I’m confused what can i do? i did not show me any errors.

Please provide the code for the project, or even better, the entire project folder.

Shaqpower3 | 2021-03-28 20:59

hi this is my code:

Player.gd:

 extends AnimatedSprite

export (int) var speed = 150
var velocity = Vector2()
var timer = Timer.new()
var player_alive = true
var can_shoot = true
var fire_rate = 0.25
var health = 4

const screen_width = 320
const screen_height = 180

var laser = preload("res://Scenes/VFX/Laser.tscn")
var explosion = preload("res://Scenes/VFX/Explosion.tscn")

signal create_laser(laser,location)
signal instance_node(node, location)

func _ready():
	add_to_group("Player")
	self.play()
	Global.player = self
	timer.set_one_shot(true)
	timer.set_wait_time(fire_rate)
	timer.connect("timeout",self,"on_shoot_timeout")
	add_child(timer)

func _exit_tree():
	Global.player = null

func _process(delta):
#screen restrain
	position.x = clamp(position.x,8,screen_width-8)
	position.y = clamp(position.y,8,screen_height-8)
# shoot 
	if Input.is_action_pressed("Shoot") and can_shoot:
		Global.play_sound("Shoot")
		emit_signal("create_laser",laser,global_position)
		can_shoot = false
		timer.start()
	#movement
	velocity = Vector2()

	if Input.is_action_pressed("ui_right"):
		velocity.x +=1
	if Input.is_action_pressed("ui_left"):
		velocity.x -=1
	if Input.is_action_pressed("ui_down"):
		velocity.y +=1
	if Input.is_action_pressed("ui_up"):
		velocity.y -=1
	position += velocity.normalized() * speed * delta

	#check if player he still has health
	if health <= 0:
		Global.play_sound("Explosion")
		emit_signal("instance_node",explosion,global_position)
		queue_free()  
		Global.reset = false
		
#emit explosion signal and queue free the player
func _on_Hit_box_area_entered(area):
	if area.is_in_group("Enemy"):
		health -= area.get_parent().damage
		Global.play_sound("Hit")
		emit_signal("instance_node",explosion,area.global_position)
		area.get_parent().queue_free()

func on_shoot_timeout():
	can_shoot = true

Asteroid.gd:

    extends AnimatedSprite

export var speed = 75
export var health = 1
export var points = 5
var check_laser = 0

var explosion = preload("res://Scenes/VFX/Explosion.tscn")

signal instance_node(node,location)

func _ready():
	$Hit_box.add_to_group("Enemy")
	play()
	if Global.world != null:
		connect("instance_node",Global.world,"instance_node")


func _process(delta):
	global_position.x -= speed * delta
	
	if global_position.x < -10:
		queue_free()


func _on_Hit_box_area_entered(area):
	if area.is_in_group("Laser"):
		check_laser +=1
		health -= area.get_parent().damage
		if check_laser == 1:
			if health <= 0:
				Global.play_sound("Explosion")
				Global.score += points
				emit_signal("instance_node",explosion,global_position)
				area.get_parent().queue_free()
				queue_free()

Enemy_generator.gd:

    extends Position2D

var asteroid = preload("res://Scenes/Enemies/Asteroid.tscn")
signal instance_node(node,location)

func _on_Timer_timeout(): 
	randomize()
	emit_signal("instance_node",asteroid,Vector2(328,rand_range(0,180)))

Shortanel | 2021-03-29 08:14

Global.gd (Singleton):

 extends Node

var world = null
var asteroid = null
var player = null
var sound_controller = null

var reset = true

var score = 0

func play_sound(sound):
	if sound_controller != null:
		if sound_controller.has_node(sound):
			sound_controller.get_node(sound).play()

Laser.gd:

extends AnimatedSprite

export (int)var speed = 150
export (int)var damage = 1

func _ready():
	self.play()
	$Hit_box.add_to_group("Laser")

func _process(delta):
	global_position.x += speed *delta
	
	if global_position.x > 330:
		queue_free()

Shortanel | 2021-03-29 08:18

Game_UI:

extends CanvasLayer

var array =["Big","Small"]
var speed = 50

func _process(delta):
	for item in array:
		match item:
			"Big":
				for i in range(1,9):
					get_node("ParallaxBackground/big_stars/big_star_" + str(i)).play()
				$ParallaxBackground/big_stars.motion_offset.x -= speed * delta
			"Small":
				for i in range(1,16):
					get_node("ParallaxBackground/small_stars/small_star_" + str(i)).play()
				$ParallaxBackground/small_stars.motion_offset.x -= speed * delta
	for x in range(1,4):
		get_node("Heart"+str(x)).play() 
		if Global.player != null:
			var health = Global.player.health
			match health:
				1:
					$Heart1.animation = "death"
				2:
					$Heart2.animation = "death"
				3:
					$Heart3.animation = "death"
				4:
					continue

	$Score.text = "Score: " + str(Global.score)

Explosion.gd :

extends AnimatedSprite

func _ready():
	play()

func _on_Timer_timeout():
	queue_free()
	if Global.reset == false:
		get_tree().reload_current_scene()
		Global.reset = true

Shortanel | 2021-03-29 08:24

World.gd:

extends Node2D

func _ready():
	Global.world = self

func _exit_tree():
	Global.world = null

func _on_Player_create_laser(laser, location):
	for i in range(1,4):
		var laser_instance_i = laser.instance()
		add_child(laser_instance_i)
		match i:
			1:
				laser_instance_i.global_position = location + Vector2(11,-4)
			2:
				laser_instance_i.global_position = location + Vector2(13,0)
			3:
				laser_instance_i.global_position = location + Vector2(11,4)

func instance_node(node, location):
	var node_instance = node.instance()
	add_child(node_instance)
	node_instance.global_position = location

In my oppinion i think that the signal is not received by instance_node function but i really don’t know why?

Shortanel | 2021-03-29 08:28

have you checked your collision layers and collision masks?

bloodsign | 2021-03-30 18:59

I never use them them but I think I’ll try to implement them.

Shortanel | 2021-03-31 05:56