Problem when Enemies die

With the Alien Attack game in the current state there is an issue that makes the player scoring incorrect. If the aliens get past the player without being destroyed by the player or a rocket they will be destroyed by the Deathzone which calls the alien die() function. This in turn increases the player score which is not correct. I had a think about this and came up with the following solution:

In enemy.gd I modified the died signal by adding a byPlayer parameter which is set to true if the player killed the alien and false otherwise:

extends Area2D

signal died(byPlayer)

@export var speed = 200

func _physics_process(delta):
	global_position.x += -speed*delta
	
func die(byPlayer):
	emit_signal("died", byPlayer)
	queue_free()

func _on_body_entered(body):
	body.take_damage()
	die(true)

In rocket.gd pass true as the byPlayer argument

func _on_area_entered(area):
	queue_free()		
	area.die(true)

In game.gd
Modified the following function to pass false as the byPlayer argument:

func _on_deathzone_area_entered(area):
	area.die(false)

Finally, in game.gd I could then determine if the alien was killed by the player:

func _on_enemy_died(byPlayer):
	if byPlayer:
		score += 100
                
1 Like

Just started the next lesson and noticed other people had found the same issue and in particular I think
@ Paulionassus’s solution is very good just using area.queue_free() in the _on_deathzone_area_entered callback. Much better than passing an additional parameter around.

1 Like

Yep, the instructor actually addresses it as a fix in… I think it was the final lesson(?). But yes, you’re correct with it being a bug in the calculation method.

Thanks for confirming. I have actually just finished the lesson where the bug is fixed!

1 Like

image

If you follow the video, your score will increase when the enemy dies in the deathzone. So I replaced it with this code. Using .die() will increase the score.

1 Like

Privacy & Terms