In my game, levels are split up into ‘rooms’. When the player enters a room, a number of enemies spawn using PackedScene.instantiate() and are added to a node with y_sort enabled. These are all CharacterBody2Ds.
Unfortunately, in one particular room there is a significant delay (1.5s frame time) on the frame after the spawn code instantiates the enemies and adds them to the scene. This delay happens BEFORE the enemies appear on the screen.
I’ve been struggling to find the underlying cause. The profiler says that most of the time (~1.45s) is spent in ‘Integrating Forces’. I really have no idea what ‘Integrating Forces’ means, but there are no RigidBody2Ds in the scene. The code that instantiates the enemies does its thing and returns almost immediately.
What I find really baffling, is that I have other scenes with more spawns that don’t have this problem. But the main differences I see are:
The level has a somewhat larger tile map.
The tile set has two physics layers instead of just one.
The camera is being controlled with the limit_* properties, where in other scenes it’s global_position is being tweened.
My main questions are:
What do the “Integrate Forces” lines in the profiler actually measure?
What does “Integrate Forces” do, particularly for CharacterBody2Ds?
Well, it looks like the tile map has a separate collision shape for each tile, which was probably what was bogging things down.
I’m still not sure what this has to do with ‘Integrate Forces’ since there aren’t any rigid bodies and the static bodies are already set up when the new enemies were instantiated. The physics simulation runs fine after they are added to the scene.
But, anyway. For anybody else running into the same issue, it seems I was bumping up against this issue:
I had to write a script to merge and ‘bake’ the tile map collision shapes into StaticBody2D nodes based on their physics layer.