[SOLVED] How to check for a global variable every frame

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

So I try to make an enemy attack if the player crosses an Area2D:

func _on_Area2D_body_entered(body):
	if "Player" in body.name:
		BOSS_GLOBAL.start = true

Boss_global.start is set to false by default but after crossing the Area2D it becomes true and then the enemy should check every frame if the variable is true so he knows when to attack.His attack is on a $Timer and it goes like this:

if BOSS_GLOBAL.start == true:

I don’t know where to put this code so it’s being checked every frame.I tried putting it in func _ready() but if I don’t start with BOSS_GLOBAL.start = true by default it won’t start the timer, I believe it is because ready func is only read once before the player can even reach the Area2D
I tried putting the code in func _process() or in func _physics_process and it still didn’t work, no errors or anything, it just didn’t start the timer. If anything I’d also appreciate if someone here knows a way to start an action when the player gets into a specific zone other than what i’m trying, I’m open to anything as long as it works.Is it because Area2D doesn’t even change the global var into a true value or is it because _process() or _physics_process() don’t work properly

Executing every frame would be inside _process().
But make sure to immetdiately reset the boolean or you would (re-)start the timer every frame so he probably never finishes.

if BOSS_GLOBAL.start == true:
        BOSS_GLOBAL.start = false   # <====

wombatstampede | 2019-05-09 15:45

Thank you very much, but after I posted this question I tried putting the Area2D inside the Enemy object and connecting it via signals just like someone suggested below, but it’s still useful to know this since I was really confused about what didn’t work properly.Will also use this method in the future!

WelpHelp | 2019-05-09 18:45

:bust_in_silhouette: Reply From: nosklo

I suggest you use signals for that, instead of checking every frame. You could connect the Area2D body entered signal directly to the function with $Timer.start()