Reputation: 1126
In my 2D game player has the ability to destroy crates, objects with two collision shapes. When destroyed, crate spawn items that also have collision shapes. But when the following function called many similar errors are displayed in the Godot console
Code:
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:
ERROR: Can't change this state while flushing queries. Use call_deferred() or set_deferred() to change monitoring state instead.
Upvotes: 12
Views: 11210
Reputation: 1126
The method call_deferred()
calls the method on the object during idle time. Its first parameter is method name string and other parameters are methods parameters.
Replace
$CollectibleContainer.add_child(collectible)
with
$CollectibleContainer.call_deferred("add_child", collectible)
Upvotes: 16