VisibleOnScreenNotifier2D delayed

Godot Version

4.6.2

Question

I have an NPC that has a schedule and must teleport location to location, without the player seeing. It has one notifier to see if it is currently on screen, and one that it move to see if the target destination is on screen.

func move():
    second_notifier.position = target
    await get_tree().process_frame
    if not second_notifier.is_on_screen():
        teleport()

The await is necessary or else it does not correctly detect if on screen, I tried call_deffered, and it did not function either.

The documentation says that the detector does not function the first frame it exists, but it is reused every time

Is there a way to get the notifier to work properly without the await?

What’s your scene tree look like?

Nvm, I looked at the docs, and I think this would work better if you used the signals.

var off_screen: bool = true


func _ready() -> void:
	second_notifier.screen_entered(func(): off_screen = false)
	second_notifier.screen_exited(func(): off_screen = true)


func move():
    second_notifier.position = target
    if off_screen:
        teleport()

Does the screen exited emit the frame it is move? I feel like it would have the same problem, since it is still getting moved the same frame, I can’t test it right now?.

If you can simplify to a single point check, you could use Camera3D::is_position_in_frustum().
You can also make a custom box/frustum check but it’d require constructing a custom frustum collider.

Would that be possible in 2d?

It’d be much simpler in 2d.
Although if you can live with that one frame delay, just do what you’re currently doing.