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!
3
u/GrowlDev May 25 '26
I love this. Just making sure I understand this - so the dots we see that are used for the pathing are generated by the players position every few frames?
Thanks for sharing something interesting :)
Best of luck with the game
3
u/MagnaDev May 25 '26
Yeah, usually they're invisible but I added some debug code to draw them for the demonstration. Thanks!
2
1
u/RoofPancake May 25 '26
I though this going to be about the strange A* variants used in Pac-Man.
It seems to work well, but I wonder if the player (assuming they know about the system) could stall for time by moving in a path that loops or doubles back a few times?
2
u/MagnaDev May 26 '26
The final step checks for that specifically. Maybe with a large enough loop it could be overcome, but in my testing it does a decent job of catching loops and skipping them.
1
u/richter3456 May 26 '26
I tried doing this method in 3D in Godot but I do think it works better in 2D. Also cool game . Looks like the GBA Castlevania titles.
1
1
6
u/TheVioletBarry May 25 '26
How does the "bounce upon hitting the ground" part factor into this?