Why does my code throw the error "E 0:00:04:0444 Area2D.gd:21 @ _on_coin_bottom_body_entered(): Can't change this state while flushing queries. Use call_deferred() or set_deferred() to change monitoring state instead."? Everything works perfectly, and i've tried adding call_deferred("") to everywhere i can think of but i just broke more lmao. Anyone know what it means?

Coin Block code:

extends StaticBody2D

var Coinscene = preload("res://Scenes/Tiles/coin.tscn").instantiate()

var empty = false
# Called when the node enters the scene tree for the first time.
func _ready():
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	pass



func _on_coin_bottom_body_entered(body):
	if body.get_name() == "Player" and not empty:
		empty = true
		$AnimationPlayer.play("Bump")
		add_child(Coinscene)
		Coinscene.coin_collect()
		await $AnimationPlayer.animation_finished
		$AnimatedSprite2D.play("Bumped")


func _on_coin_top_body_entered(body):
	if body.get_name() == "Player" and not empty:
		if body.state == 2:
			empty = true
			$AnimationPlayer.play("BumpLower")
			await $AnimationPlayer.animation_finished
			$AnimatedSprite2D.play("Bumped")

Coin code:


extends Area2D

var collected = false

# Called when the node enters the scene tree for the first time.
func _ready():
	$AnimatedSprite2D.play("Idle")

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	pass

func _on_body_entered(body):
	if body.get_name() == "Player" and collected == false:
		coin_collect()

func coin_collect():
	collected = true
	$AnimatedSprite2D.play("Spin")
	$AnimationPlayer.play("Collect")
	$CoinSound.play()
	Coins.coins = Coins.coins + 1
	await $CoinSound.finished
	queue_free()

Probably is complaining about the physics object being destroyed on a callback, meaning it will be out of date with the physics update in the current physics loop.

This is likely happening on the coin when you call queue_free. I’d try calling queue_free with a deferred call and that should resolve the error. Keep in mind you will want a Boolean to tell if the coin has already been collected though to prevent double coin collection. Just set the Boolean to true before calling queue_free in the deferred call and then, in the code that detects coin collisions, check if the Boolean is true and just do nothing (ignore the collision) if it is.

    Does the issue still occur if you disable/remove queue_free entirely? Obviously this wouldn’t be a solution, but it might narrow down where the issue is. I would have thought it was the queue_free call, but it seems this may not be the case. But it might and so removing it temporarily would help rule that out.

    Never mind, I think the issue is in the block script on second read.

    SuperMatCat24 Btw, it also happens inside a mushroom block scene i made, which doesnt have a coin in it

    Okay, looking at this again, this was my bad. The issue seems to be in the block, not the coin. My guess is calling Coinscene.coin_collect() right after spawning the node may be the problem. Have you tried calling this in a deferred function call?

    What code is on line 16 on the coin block? That should help tell you where the issue is. I think if you double click it, the editor should try to go to the line of code, but I do not remember 100% right off.

    it says the error is on "add_child(Coinscene)" but adding call_defferred causes a crash

    That’s strange. Is the error for the crash? If so, did you make sure it is like call_deferred(“add_child”, coinScene)?

    I’m not sure right off what the issue is then. It seems very strange since the error, from what I can tell, shouldn’t cause a crash. Are you also calling Coinscene.coin_collect() in a deferred call? Coinscene.call_deferred(“coin_collect”)

    Ah. Turns out i dont know how call_defered works lmao. Thanks!

    a year later

    in func _ready function set the monitoring to false

    func _ready():
    monitoring=false