Consider using Callable.call_deferred()
instead of Object.call_deferred()
This way you get to avoid a string reference
Say you have a signal: died, and inside on_died, godot gives you an error/warning and suggests you to on the add_child() line and suggests to use call_deferred
func _on_died():
if randf() > drop_chance: return
var vial_instance = vial_scene.instantiate() as Node2D
owner.get_parent().add_child(vial_instance) #need to call_deferred
vial_instance.global_position = owner.global_position
Instead of directly calling the add_child() method deferred,
Navigate to where the the signal is being emitted and use the Callable.call_deferred()
Extract method
Construct Callable
<aside> ⚠️ Make sure you do not call the extracted method, simply pass the reference
</aside>
call call_deferred() on the Callable
func take_damage(damage: float):
current_health = max(current_health - damage, 0)
Callable(check_death).call_deferred()
func check_death():
if current_health == 0:
died.emit()
owner.queue_free()
This way, you tackle the root of the problem and any other instances where you connect to the died
signal wont have to call_deferred() individually every single time .
You can actually just call call_deferred() directly on a method without having to construct a callable.
check_death.call_deferred()
(func():
if _current_health == 0:
owner.queue_free()
died.emit()
).call_deferred()
From: https://www.udemy.com/course/create-a-complete-2d-arena-survival-roguelike-game-in-godot-4/