VisibleOnScreenNotifier2D not working when the object returned to screen

Godot Version

v4.2.2.stable.official [15073afe3]

Question

I’m developing a 2D game. I’m trying to make items, objects and characters toggle their visibility when they are off screen, then toggle back on when they re-enter the screen.

I attached a VisibleOnScreenNotifier2D however, after the scene is toggles visibility off it never toggles back on, however, the object is still there attacking my player.

I have the following two functions linked to signals

func _on_visible_on_screen_notifier_2d_screen_entered():
	print("Screen Entered")
	show()


func _on_visible_on_screen_notifier_2d_screen_exited():
	print("Screen Exited")
	hide()

The enemy is currently visible when it spawns, and when it enters the screen it triggers the visible message, then as they leave the screen they toggle the leave message, however, when they reenter the second time while they are invisible I don’t get the message and doesn’t toggle the enemy to be visible again.

I am struggling to debug this. Any assistance would be great.

I think that if you set the visibility to false, the notifier will not respond. May I ask why you are toggling visibility for off screen?

PS I only ask as it seems a bit of a strange thing to do.

PPS I think in Godot 3 it did still signal when visibility was set to false, but in Godot 4 it does not. Personally I think this is the correct behaviour. If it is invisible, then it is not on screen.

PPPS Perhaps this is more suitable for you?
VisibleOnScreenEnabler2D

Thank you for the information.

I want to toggle visibility when the items are off-screen for performance, rather than the game drawing every single item even if the player is unable to see them. I was trying this based on https://youtu.be/lfuGLaZ3khs?si=jpF6zG6YvPXvgucC.

My game has hundreds of enemies and the potential for hundreds of items on the ground. Toggling the visibility of the root element would have been the best approach, as this ensures that all of the elements under the parent are toggled to no longer be visible. For example, some of the nodes have many sub-nodes that I want to ensure are not visible.

I tried VisibleOnScreenEnabler2D; however, that also disabled the scripts, meaning that the objects stopped functioning while off-screen. For example, enemies no longer chase while off-screen.

Any further ideas on this one?

The only time I have done this is with my Asteroids. I did it based on distance from the player calculated on the screen size plus a margin to not have the bigger asteroids suddenly appear.

I have seen on YT where people break their world into a grid, and items are only active in the player square and adjacent ones. This seemed a good approach to me but I didn’t need to do that in my case as my environment was really not that big.

The only other similar thing I have done is to use the set_process(false) on items that are not on screen. The screen notifier still worked then, but the _process (and I think the _physics_process) were not being called unnecessarily.

Anyway, I am just a beginner so I hope this helps in some way, but it probably won’t. Sorry.

Best wishes,

Paul