Object pooling with bullets of different hitboxes

Godot Version

4.3 stable Windows 11

Question

I’m making a bullet hell game. I’m trying out object pooling on bullets to improve performance, but I have a problem: I can’t seem to figure out how to do that with bullets of different hitbox shapes. For example, i have a ball_medium bullet with a circle hitbox, and a knife bullet with a rectangular hitbox.

image
I tried something like this, and whenever I call a bullet from the pool I define its type, and I make every sprite2D node but the correct one invisible when it activates, but making nodes invisible doesn’t stop them from processing, detecting collision, and hindering performance.

I don’t think I could just make different bullet scenes for each bullet type, because then I’d have to make multiple pools and that’s not ideal.

I don’t suppose having just one bullet scene, then queue_freeing and instancing the sprite and hitbox for every activation would work either, because that would make all my object pooling efforts pointless.

What I’d like is: an object pool of instances of one single bullet scene, which when activated and used will have a different sprite and hitbox depending on the bullet type I assign to it. Is this possible? What’s an efficient way to do it? Any help is appreciated.

P.S. I don’t dare mess with physic servers yet because I don’t know how to work with them.

I see your problem. You want to optimize performance and want to prevent extra nodes in your bullet scene while also having one bullet scene to keep it centralized. Can you explain what your current performance issues are? Maybe the optimization isn’t needed as much as you think.

You can use different approaches to this problem.

  1. You already considered making the sprite invisible, but this doesn’t stop the CollisionShape2D from processing. However, you can set the CollisionShape2D’s disabled property to true so it will stop processing. Then, when you get a bullet from the pool, you can switch to the correct sprite and collisionshape by setting disabled to true on one and to false on the other.
  2. You want one pool and not create multiple pools per bullet type. However, you can have different types in one pool variable by using pool[bullettype]. This setup is used in this example: Dun-dun/game/res/scripts/managers/complex_bullet_manager.gd at main · DjHexaedro/Dun-dun · GitHub
  3. I also found an implementation using servers if you’re interested: BulletUpHell/addons/BulletUpHell/BuHSpawner.gd at Demo-V4.4 · Dark-Peace/BulletUpHell · GitHub

I’d go for option 2 if I were you. Define your pool as a dictionary, for example
var pool: Dictionary = {"roundbullet": [], "raybullet": []}
You now have two arrays for your pool variable to use.

1 Like