戈多 3.2.1。刷新查询时无法更改此状态。使用 call_deferred() 或 set_deferred() 来更改监控状态

Godot 3.2.1. Can't change this state while flushing queries. Use call_deferred() or set_deferred() to change monitoring state instead

在我的 2D 游戏中,玩家可以摧毁板条箱,即具有两个碰撞形状的物体。销毁时,板条箱会生成也具有碰撞形状的物品。但是调用下面的函数时在Godot控制台显示很多类似的错误

代码:

func _on_Crate_item_dropped(collectible, pos):
    collectible.init(pos, Vector2(rand_range(30, 100), rand_range(-10, 10)))
    $CollectibleContainer.add_child(collectible)  # error occurs here

错误:

ERROR: Can't change this state while flushing queries. Use call_deferred() or set_deferred() to change monitoring state instead.

方法call_deferred()在空闲时间调用对象上的方法。它的第一个参数是方法名字符串,其他参数都是方法参数。

替换

$CollectibleContainer.add_child(collectible)

$CollectibleContainer.call_deferred("add_child", collectible)