call_deferred()
From the collection Godot

godot call_deferred
godot signals
tips

QR Code
call_deferred()

You can show this QR Code to a friend or ask them to scan directly on your screen!

Thanks for sharing! 🫶

The url for this was also copied to your clipboard!

image.png

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 Python Copy 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 Make sure you do not call the extracted method, simply pass the reference call call_deferred() on the Callable Python Copy 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 DONT ACTUALLY NEED TO WRAP IN CALLABLE

You can actually just call call_deferred() directly on a method without having to construct a callable. check_death.call_deferred()

You can take this a step further to avoid creating a seperate function by using a lambda

Python Copy ( 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/ ALT

See more from renews