call_deffered help

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

I am following along with Heartbeast’s Action RPG tutorial and while I wait for new videos I have been attempting to add new features to a duplicate project. Currently, I am working on adding coin and gem spawning from cut bushes (Link). The code seems to work when I play the game except I get this error.

E 0:00:03.125 area_set_shape_disabled: Can’t change this state while flushing queries. Use call_deferred() or set_deferred() to change monitoring state instead.
<C++ Error> Condition “area->get_space() && flushing_queries” is true.
<C++ Source> servers/physics_2d/physics_2d_server_sw.cpp:409 @ area_set_shape_disabled()
Grass.gd:28 @ spawn()
Grass.gd:15 @ create_grass_effect()
Grass.gd:18 @ _on_HurtBox_area_entered()

From what I have looked up it seems that I am having problems with overlapping areas, and I should use call_deffered() or set_deffered(). Where is my question? I can not seem to make the error stop.

extends Node2D

var rng = RandomNumberGenerator.new()
var press = 0

func _ready():
	rng.randomize()

func create_grass_effect():
	var GrassEffect = load("res://Effects/GrassEffect.tscn")
	var grassEffect = GrassEffect.instance()
	var world = get_tree().current_scene	
	world.add_child(grassEffect)
	grassEffect.global_position = global_position
	spawn()

func _on_HurtBox_area_entered(_area: Area2D) -> void:
	create_grass_effect()
	queue_free()
	
func spawn():
	rng = (rng.randi_range(16, 1) * press) - press 
	print(rng)
	if rng == 6 || rng == 8 || rng == 10 || rng == 21 || rng == 24 || rng == 30 || rng == 36|| rng == 39 || rng == 42:
		print(str("coin")+' ', press)
		var CoinSpawn = load("res://Coin/Coin.tscn")
		var coinSpawn = CoinSpawn.instance()
		get_tree().get_root().get_node("World").find_node("YSort").add_child(coinSpawn)
		coinSpawn.global_position = global_position
	
	elif rng == 0 || rng == 3:
		print(str("gem")+' ', press)
		var GemSpawn = load("res://Coin/Rupee.tscn")
		var gemSpawn = GemSpawn.instance()
		get_tree().get_root().get_node("World").find_node("YSort").add_child(gemSpawn)
		gemSpawn.global_position = global_position
		
	else:
		print("you get nothing")

func _input(event: InputEvent) -> void:
	if event is InputEventKey and event.pressed and not event.is_echo():
		press += 1
		if press == 4:
			press = 1

Thank you for checking out the post.

:bust_in_silhouette: Reply From: Aireek

I’m following his tutorial too. He is awesome! I’m very new as well (and i’m having this same error but with a reparenting function).

Until someone with more knowledge answers your question… my guess would be your spawn function. How i’ve seen call_deferred used is like this:

func spawn():
    call_deferred("_deferred_spawn")

func _deferred_spawn():
    #Your actual spawn function goes here

and then you’d just call your spawn() where you needed it. Not sure if that would fix it… but worth a shot :stuck_out_tongue:

That worked, thank you so much

Mentiras | 2020-04-13 15:54

I’m glad it worked! Have fun with your game!

Aireek | 2020-04-13 16:59