0 votes

I have some code with a monster and am trying to make an event where he "awakens" to chase the player. My code disables his hitbox so a player can run past him but whenever I try to apply and event (with him as child) and change the state, I get this error.

bodysetshapedisabled: Can't change this state while flushing queries. Use calldeferred() or setdeferred() to change monitoring state instead. I am wondering how calldeferred() could be utilized here to solve this.

Here is the code, all my methods work fine until the physics settings get changed.

 extends Area2D

func _awakening():
    $Patches._isAsleep = false
    $Patches._collision.disabled = false

func _on_ScareEvent_body_entered(body):
    if body.name == 'Lydia':
Godot version Godot 3.5 stable
in Engine by (548 points)

1 Answer

+1 vote
Best answer

Here is a thread with a similar problem. The issue is that you are not using call_deferred() when changing collision properties (like disabling hitboxes or destroying them) inside the collision handling method _on_ScareEvent_body_entered The error is occurring because the physics engine is processing collissions, and you can't mess with the collision-related nodes that are part of that collision detection.

To solve it, make sure to destroy objects that have Area2D's actively being checked for collisions outside the event handler (e.g. call_deferred("queue_free") and when disabling collision shapes/areas, I think you have to deferr the call as well.

How I handle this is I have a collision handling method that just appends all the collisions to a list, then on the physics step (inside _physics_process) you can process the collisions, disable hitboxes, create hitboxes, etc. without having to use call_deferred

by (570 points)
selected by

Thanks for that. I don't like using call_deferred much. Moving it to the physics process solved it. Thank you!

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 [email protected] with your username.