Sphere
Log in Join
☆ 4

godot 4.2 scene drops to 20fps after adding more enemies

Started a small top-down shooter in Godot 4.2.1, everything was fine at 60fps with about 30 enemies. Added a wave of 150 and it tanks to 20fps on my laptop (GTX 1650, i5-9300H). Profiler says most time is in Scripting and Physics 2D. Each enemy is a CharacterBody2D with an AnimatedSprite2D, a CollisionShape2D, and a script that calls get_tree().get_nodes_in_group("player") every _physics_process to find the player. I know that lookup is dumb but is it really the whole problem? Also noticing the animation seems heavy. Should I be pooling these instead of queue_free and reinstantiate? Getting a bit lost on where to start.

⚑ Report

4 replies

☆ 0
R rust72 ·2026-11-04#1

That get_nodes_in_group in _physics_process is doing a linear scan 150 times a frame, and groups aren't free. Cache a reference to the player once in _ready or use a singleton autoload. Also check you're not creating a new Vector2 every frame - Godot 4 builds fine but the allocations add up. What does the profiler show for Physics 2D vs Scripting exactly, in ms?

⚑ Report
☆ 0
G ghost26 ·2026-11-08#2

Scripting is about 22ms, Physics 2D is 9ms. I changed the group lookup to a cached @onready var player and it dropped to 17ms scripting, so that helped more than I expected. Still not 60fps though. The enemies also each have their own Timer node for shooting - could 150 timers be part of it?

⚑ Report
☆ 0
P plastic2 ·2026-11-12#3

Timers are cheap-ish but 150 of them all firing is still callback overhead. Move the shooting cooldown into a float counter in _physics_process instead, no Timer nodes. Also AnimatedSprite2D with a big SpriteFrames resource shared is fine, but if each enemy has its own duplicate that's memory churn. Check the SpriteFrames is shared, not duplicated per instance.

⚑ Report
☆ 0
C cardboard ·2026-11-13#4

Was following this because I hit the same wall. Two things got me from 25 to 55 fps: the cached player reference, and switching collision shapes from circles to rectangles. Circle-vs-circle in Godot 2D is pricier than people think. Still some work but it's playable. If you know a cleaner way I'm all ears.

⚑ Report
Log in to reply.