Why not use on_screen_notifier?

Hey, for the enemy destruction I simply added a VisibleOnScreenNotifier2D.

I then add a boolean to the enemy script that is set to false called “enteredScreen”

Then:

func _on_visible_on_screen_notifier_2d_screen_entered():
	enteredScreen = true
	

func _on_visible_on_screen_notifier_2d_screen_exited():
	if enteredScreen:
		queue_free()

Works exactly as intended. Is there a reason to create a “death zone”?

3 Likes

Well, as Kaan mentions, there’s frequently more than one way to do something. If it works, it works, and well done coming up with another idea!

Is there a reason to create a deathzone? Maybe, depending on if/how you extend the project later. If you have enemies that enter the screen, leave the screen, and then legitimately enter it again (could be a path enemy with a crazy path, or a regular enemy that zig-zags or something), then your idea will need tweaking at the least, and the deathzone starts to look a bit more favourable.

2 Likes

This is what I did as soon as I made the enemy move. You did not need a entered function to make it work. I used code that I had used before to reload the scene as a placeholder until he gets to a part for scoring.

image

ETA: He does mention this method near the end of the video, and I have used the deathzone method a few weeks ago on a game jam. If I was going to add scoring to the die method, and the deathzone calls die, then it would score whenever it got to the deathzone.

There’s always more than one way to something when coding, but some ways can optimize things while others are a little more messy.
I believe in what was noted above me here ‘if it works, it works’.

With that said sometimes implementing functions in certain ways can break the game in unexpected ways. For example, I created a function to use queue_free prior to this episode. Because we learned how to do it when we learned how to release the rocket objects, so I simply took that snippet of code, and use it to release my rocket.
So mine looks like this:

extends Area2D


#create global variables and connect nodes
@export var speed = 450
@onready var visible_notifier = $VisibleNotifier
@onready var area_enter = $"."


#create bullet movement 
func _physics_process(delta):
	global_position.x += speed*delta

#connect signals in the ready function
func _ready():
	visible_notifier.screen_exited.connect(_on_screen_exited)
	

#kill the process
func _on_screen_exited():
	queue_free()

#delete the rocket when the area is entered 
func _on_area_entered(area):
	queue_free()

I also added a second enemy, and because of the method I used to create the enemy when they ran into one another they would delete each other. This was fixed using the layer/mask as at the end of Lecture 12 for Alien Attack.

I hope this provided some insight as I’m now realizing that you wrote this post a while ago and have likely moved on to greater coding!

Privacy & Terms