Can't change this state while flushing queries. (Enabling/Disabling Collision)

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

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.

body_set_shape_disabled: Can’t change this state while flushing queries. Use call_deferred() or set_deferred() to change monitoring state instead. I am wondering how call_deferred() 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 == 'Lydia':
:bust_in_silhouette: Reply From: godot_dev_

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

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

SnapCracklins | 2022-08-19 20:17