0 votes

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:
        $Timer.start()

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

in Engine by (18 points)
edited by

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   # <====
        $Timer.start()

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!

1 Answer

+1 vote

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()

by (23 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.