About "VisibleOnScreenNotifier2D"

Godot Version

4.6.2

Question

I’m brand new here to Godot Engine. Below I just want to remove one instance when it is out of view range, but I found this doesn’t always work.

To be specified, this way works ONLY in the case the instance is in the view when it is instantiated. So is there any way instead of comparing its position with limits?

func _on_visible_on_screen_notifier_2d_screen_exited() -> void:
	queue_free() # Replace with function body.

You can use
VisibleOnScreenNotifier2D::is_on_screen() method in the _ready() to validate that and remove immediately.
But you might need to wait a frame before checking due to this disclaimer:

Note: It takes one frame for the VisibleOnScreenNotifier2D’s visibility to be determined once added to the scene tree, so this method will always return false right after it is instantiated, before the draw pass.

1 Like

It also depends on how often or when you want to check that. As @wchc said, you can check in the _ready()function, or when an instance is actually added to the tree via _enter_tree()or even each frame during _process().

Comparing positions is also very easy. Have you taken a look at the documentation’s tutorial? You can see an example how to compare variables right there. position is a regular variable that can be checked against any other variable of the same type or processed further.

Thanks. I will take a look. In fact using screen size limits is easy and suitable enough for my needs, I just wonder the usage of VisibleOnScreenNotifier2D.