How do i use Godot Object Pool addon to create poolable projectiles

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

I’m making a 2D platformer where the player has various weapons that can fire projectiles at enemies. However, instancing projectiles and adding them to the scene is too expensive performance-wise and creates stuttering, especially with machine-gun-like weapons that must fire repeatedly. I’ve started reimplementing projectiles to use with the Godot Object Pool addon, but don’t now exactly how to implement them. Each projectile has a timer that will emit the “killed” signal on “timeout”. But where should i place the connect() function? If i place it in ready() then it doesn’t work at all. If i call it in an initialization function that’s called each time the projectile is added to the scene, it works, but every time i initialize a bullet that was previously initialized i get an error saying that the timeout signal is already connected (which also seems to create stuttering).

:bust_in_silhouette: Reply From: Socrates

Just asking the question pointed me in the right direction after struggling with it for hours. There is a disconnect() method that can be called on the timer every time the killed signal is emitted.

:bust_in_silhouette: Reply From: GodotNoob999

If the signal does not need to execute twice, try using the CONNECT_ONESHOT flag.

connect ( String signal, Object target, String method, Array binds=[ ] CONNECT_ONESHOT)

Godot Docs

(Use | to seperate flags)

Oh that’s perfect, thank you!

Socrates | 2018-04-24 17:22