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

1

11

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.
Bonaparte answered 1/8, 2020 at 14:19 Comment(0)
B
15

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)
Bonaparte answered 1/8, 2020 at 14:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.