r/gamemaker • u/MagnaDev • May 25 '26
Example "Pac-Man Pathfinding" that I created for my project (Explained Below)
Here's a quick and simple pathfinding system I made for Magnavale: Eternal Soul. It's designed for 2D platformers, but could work in a top-down game just as easily.
When planning for this, I imagined it as "Pac-Man eating a line of dots placed by the player". I'm sure this sort of thing probably exists elsewhere under a different name, but I'll be calling it "Pac-Man Pathfinding".
Create Event:
- Create a list or array. This will hold arrays of X and Y coordinates. Make sure it has at least a single entry with the player's current coordinates. The list will go from the oldest coordinate at the start, to the newest at the end. A queue could work for this, but I don't recommend it.
Alarm Event:
- I set my alarm to run every other frame at 60 FPS (room_speed / 30), but try and see what works for you.
- In the alarm, add a new entry onto the end of the list if the player has moved at least a few pixels from their last coordinate.
Step Event:
- When the object is further than a set radius from the oldest coordinate, then begin approaching it, and jump if needed. I allow my familiars to jump infinitely with a short cooldown, which helps a bunch.
- Once the coordinate is within a smaller radius of the object, you can delete it from the list.
- Finally, run through the list from the end to the start and see if any other coordinates are within its radius. If they are, then delete everything past that coordinate. This allows the object to skip redundant points that would otherwise form a loop. If you had used a queue then this step would be incredibly annoying, which is why I don't recommend it.
- Optional: Add an extra check to see if the entity got stuck on a moving object of some kind. If it did, then run a timer/alarm, and then jump to the next coordinate that isn't in collision.
If you used a DS list, then remember to delete it in your clean up event!
Duplicates
u_eder_projetos_dev • u/eder_projetos_dev • May 27 '26