Godot 2d - Score UI Video

Have I missed something because when I run the code my score increases when ever an enemy ship is removed via the Deathzone, which I am sure is not intended. I have fixed by changing the following in the game.gd script from area.die() to area.queue_free and hoping this does not break something later on.

func _on_death_zone_area_entered(area):
	area.queue_free
5 Likes

dont think it will, nice alternative tho to not score if they pass you

I saw this behavior too - the score increased when an enemy touched the player or was removed by the deathzone, and that irritated me.

So I had just implemented a solution which takes an additional parameter into the enemy die() function, which looks now like this:

func die(cause_of_death):
	var s_value = 0
	if cause_of_death == "touched_player":
		s_value = - 2*(enemy_scoring_value) 
		# an enemy touching the player gives a penalty of double the enemy's worth (by default 100 * -2 = -200)
	elif cause_of_death == "entered_deathzone":
		s_value = - (enemy_scoring_value)
		# an enemy passed the screen and removed by the deathzone gives a penalty of the enemy's worth (by default 100 * -1 = -100)
	elif cause_of_death == "shot_by_rocket":
		s_value = + (enemy_scoring_value)
		# we shot an enemy and get the score increase for it (by default +100)
	else:
		s_value = 0
		# an unknown cause of death gives no points
	emit_signal("died",s_value) 
	# the s_value argument passes the calculated score to the _on_enemy_died(value) function in game.gd
	queue_free()

Oh, and I implemented @eric_v 's idea Attaching score value to enemy scene - Other Courses / Show - GameDev.tv - thank you :slight_smile:
and I put the scoring value of the enemy into a variable and made it editable in the inspector:
(near the top of enemy.gd, near where the speed variable is)
@export var enemy_scoring_value = 100

Of course I had to add the appropriate parameter to all places where the enemy’s die(cause_of_death) function is called - which are:

  • (also in enemy.gd), in the function _on_body_entered(body)) the die call is now die("touched_player")

  • (in game.gd) in the function _on_deathzone_area_entered(area) the die call is now area.die("entered_deathzone")

  • (in rocket.gd) in the function _on_area_entered(area) the die call is now area.die("shot_by_rocket")

In my testings this worked flawlessly.
Enjoy :slight_smile:

Edited to add clarity

7 Likes

Privacy & Terms