How to pause and reset enemies off screen

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

I am making a game in the style of older Zelda games, like Link’s Awakening. I want to have the enemies by inactive when the player is in a different room, and to be reset to default status when they leave and reenter.

I know to use an Area2D to trigger the pause and reset, but I don’t know how to code this in.

Or should I just make spawner nodes that spawns the enemies in when the play enters and removes them when they leave?

:bust_in_silhouette: Reply From: Inces

There are many ways to approach this problem, but I would like to promote one sollution I learned about not so long ago, and it seems perfect for your situation:

There is a node called VisibilityNotifier. You can child it to your enemies, and it will emit signal, when they go off screen. You can connect something to this signal to pause them, and unpause when they will be back on screen.

Alright, but how do you pause an individual node? Everything I have read is pausing the entire scene and making some exempt from that.

DJ_Fail87 | 2022-03-20 22:41

You have put all this work to code some behavior in enemies, and all it takes to pause is to NOT let that code go :). It can be easily done by tactically placing RETURN in most important functions. So I would create some boolean var “paused” and use it in conditions like these :

func physcics_process()
       if paused == true:
              return
       movementcode
       animationcode
       AIcode
       whatevercode

this first condition stops all the code below it if paused is true

Inces | 2022-03-21 15:48