• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker Path finding to go as close as possible to object

M

Mikeatron

Guest
Context: I'm working on a state-machine AI in a game where the perspective is top-down. I'm using GameMaker's built in path finding for my AI to walk to any given location. I want the AI to walk to a tree, it won't because the tree is a collision object and it technically intercepts the path given that the destination coordinates are the same as the tree I want it to walk to.
So AI wont walk to tree because that same tree is in way of path. Is there any way for me to make the AI walk as close as possible to the tree as it can?
 
D

Danei

Guest
There are many ways to do something like that, and it's hard to know which would require the smallest changes to your current setup without knowing more about what your code is doing. When you say you're using GM's built-in pathfinding, do you mean the mp_* functions, and if so, which ones, and how are you using them?
 

TheouAegis

Member
If you are using the grid functions, you can clear the cell that the tree is on, create the path, then add the tree back into the cell.
 

TheouAegis

Member
mp_grid_clear_cell() after figuring out what cell the tree 8s in (usually trr's x div cell width and tree's y div cell height.
 

Rob

Member
To be honest if you want to ignore obstacles until you reach them, i wouldn't add them to the grid at all, unless you need to.

Then when your ai is moving you just check in front of where it's moving to and if a collision will be detected then stop the ai.

I say this because if you're adding/removing trees from the grid then if something else is expecting all trees to be obstacles then doing what the other guy suggested may mess you up.
 

TheouAegis

Member
Well if he only removes the tree when making the path and then adds the tree back after making the path, the tree will still be there for the other instances to treat as an obstacle.
 
M

Mikeatron

Guest
To be honest if you want to ignore obstacles until you reach them, i wouldn't add them to the grid at all, unless you need to.

Then when your ai is moving you just check in front of where it's moving to and if a collision will be detected then stop the ai.

I say this because if you're adding/removing trees from the grid then if something else is expecting all trees to be obstacles then doing what the other guy suggested may mess you up.
How would i implement this using grids?
 

Rob

Member
How would i implement this using grids?
If your game is grid based and the AI/Player is moving to the right, just check if there's an obstacle in the grid square to the right and if so, stop the movement. I like to use collision_rectangle but collision_point would work just as easily. Convert the grid x/y into room coordinates (times the grid x/y by your grid size) and perform the check after every square.

That's how I'd go about it anyway.
 
Top