The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

I have an array of KinematicBody2D nodes that I use as bullets in my game. Whenever a bullet expires or collides, it is returned to the cache roughly like this:

playfield.remove_child(bullet_instance)
bullet_array.push_back(bullet_instance)

Bullets are spawned again roughly like this:

new_bullet = bullet_array.pop_back()
new_bullet.position = spawn_position
new_bullet.initialize()
playfield.add_child(new_bullet)

Whenever I spawn a once used bullet, it sometimes gets killed by the same PhysicsBody2D that killed it originally - even though it has a completely new position. It's as if the physics engine still considers it to be in the same position it died previously.

I've tried printing the position of the bullet when it dies and it is nowhere near the PhysicsBody2D that supposedly collided with it.

Am I missing some best practise that should be followed when caching physics objects?

Godot version 3.2.2 stable
in Engine by (38 points)
edited by

Do you have any pending signal or connection that may need to be reset when adding again to scene?

I believe the technique is actually called object pooling try taking a look to this previous answer: https://godotengine.org/qa/26814/what-is-the-best-way-to-do-object-pooling

Thank you, using the correct terminology helped in finding much more information about it.

Apparently pooling is generally not recommended with Godot, but coming to that conclusion is not easy since the information is scattered about in many sources and not even mentioned in the official documentation.

My issue did not have any signals involved, but it does seem like I don't even know half of the things to consider when removing nodes from the tree, resetting their state and putting them back.

Maybe I'll just use simple instancing instead and see how far that gets in terms of performance.

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.