Is there any way to optimize pathfinding?

I would really like to use pathfinding for zombies and have waves of them spawn, but anything over about 10 zombies on mobile performs very poorly. The performance is even worse if I am near obstacles for the zombies. According to the profiler it is this code specifically causing the issues, but I am not sure what I can do to help it. This is all with a high cell size.

For each event and collision checking inside can be very heavy on resources because you are forcing the program to check collision on all enemies each frame 30-60 times a second. So you definitely need to try an other way.

You can check collision between the hitbox and the enemy in a standard event only because it is going to be true for each enemy anyway and you can also apply a timer to stop checking collision 30-60 times a second every frame and check collision only 5 times a second maybe.

If timer > 0.2 seconds check collision : reset timer

An other thing you can also try to reduce the number of collision checks is to pick a random enemy few times a second and check collision for that random enemy only.

If timer > 0.2 seconds Pick a random enemy check collision : reset timer

Because you pick a random enemy 5 times a second, there is a chance some enemies may never going to be picked and cause a “bug” so you might need to decrease the time to check more often as you have more enemies in the scene but it should be still better than checking collision for all enemies and hitboxes each frame 30-60 times a second.

In general, always best to try to solve things without using the For each object event because it can hit performance really badly. Also collision checking. The less collision checking you do the better for performance in general.

Using a timer before the loop seems to help alleviate the lag. I’m not sure how I would get away from using the for each loop though because the linked objects make only one enemy move at a time if they aren’t in the loop.

Hello, I am having trouble with this. The pathfinding is making my game so lagged. If there are more enemies there in my game, more is the lag (and I’m not talking about 50 enemies, I’m talking about 3 or 4). Even the distance between the main character and the enemies worsens the lag (I think because the distance makes it difficult to find paths).
I tried to activate pathfinding once per second and found this:
1- The time between second and second the game works perfectly (at least so far).
2- When it comes time to reload the path (the stopwatch reaches 1 second and restarts) the game takes terribly, like 0.5 seconds, maybe more … it depends on the number of enemies and the distance with them.
I need if someone can help me with this problem!!! And sorry for my bad english :grimacing: hahaha

Im having the same problem: when a pathfinding object gets close it starts to lag

Instead of resurecting an old post, could you start a new one? You can link this one in it as a reference. The GDevelop community can then have a go at answering it.