+1 vote

Context:
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 onSearchingAreabodyentered(body):
if body.isingroup("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.

in Engine by (16 points)

1 Answer

+2 votes
Best answer

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.

by (862 points)
selected by

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!

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.