How do signals' use of the observer pattern affect performance at lower levels of code?

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

I’m writing a pathfinding algorithm (based on Navigation2D) that allows enemies chasing the player to find a staircase to go up if the player is at a different depth than them (I have a pseudo-3D depths system in my top-down game that behaves basically identically to how it does in Zelda Link to the Past). I have no troubles with my code, moreso curiosity for how one of my scripts will impact performance at lower-levels of code. I have a large Area2D that is instantiated for each enemy that, when the enemy detects that the player is at a different depth level than them, activates, and then gives the enemies’ pathfinding a new target: the first staircase that is picked up by their Area2D.

Actual Problem:
How would the following code impact performance?
func _on_SearchingArea_body_entered(body):
if body.is_in_group(“stairs”):
#rest of code
Would it try to scan every collidable object in the level (that’s roughly the size of the Area2D), or would it skip checking things that aren’t stairs, thus not lagging the game on lower-end machines?
I really haven’t learned much of Godot’s efficiency patterns and what goes on behind-the-scenes, so possibly a quick explanation of how Godot utilises the observer pattern would be what I need.

:bust_in_silhouette: Reply From: JimArtificer

Since the engine is open source, you can review how it is implemented in detail by reviewing the code, but the only way to know how something will impact the performance of your game is to measure it.

It’s likely the area collision uses a technique like an octree or something to speed up the calculations, but even if it didn’t I think your concern is misplaced. Commonly, the pathfinding algorithm is going to be the most expensive operation in the situation you described.

Try to avoid premature optimization and remember that the machine you are working with is insanely fast compared to the one inside a Super Nintendo.

Huh, ok. Based on what I found and how my code behaves in the debugger, the collision detection takes up like no processing power. Thanks for the answer!

Shimimple | 2020-08-14 16:36