How can I disable KinematicBody2D nodes when they aren't on the screen?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Coweh

So, in a game, I’m implementing enemies, as a lot of games do, as KinematicBody2Ds. For the most part, everything has been going well, but there’s one problem: the enemies will still do all they’d normally do, even if they’re offscreen. This is of course problematic for two reasons:

  • There’s no reason in trying to run a bunch of code when offscreen
  • For example, if the enemy is programmed to walk forward no matter what, they might fall off a ledge before the player even reaches them, and other things like that

So, how could I just make the enemies not do anything when offscreen?

:bust_in_silhouette: Reply From: kidscancode

You can use a VisibilityNotifier2D to let you know when the enemy is/isn’t on screen.

Oh, right. I forgot about that node. Thanks!

Coweh | 2020-09-30 14:55