• 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 mp_grid checking if path is blocked

asollazzo

Member
Hi, im pretty new to the forums and GMS2 in general and am building my first game which is a hybrid tower defense / top down shooter. In the game the enemies are using a mp_grid to path through the level towards a goal and can react to walls being placed dynamically and adjust their path.

My problem comes with the path being blocked by a placed wall, i can make it detect when a wall has been placed that blocks the path and have it be deleted fine, but ideally i would when a potential placed wall will block the path that it will display as red before even placing it.

I cant figure out how to do it or even if it is possible.

Ive attached a pic of the game (all graphics are temporary) that shows a green square on the cursor indicating a potential wall placement in a position that will block the path for reference.

tower.jpg
 

YellowAfterlife

ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Moderator
mp_grid_path returns whether it was possible to find a path from x1y1 to x2y2, so you can use that
 

asollazzo

Member
mp_grid_path returns whether it was possible to find a path from x1y1 to x2y2, so you can use that
Yup im using that at the moment to check when placing a wall if it has actually blocked the path and to remove it. Im just unsure of how to check it before ive actually placed it and to show the square as red to show that you cannot place a wall there.
 

rIKmAN

Member
You could iterate through the points in the path and see if any of them pass through the position / cell you want to place your wall.
 

asollazzo

Member
You could iterate through the points in the path and see if any of them pass through the position / cell you want to place your wall.
Sounds like a good idea but how do i determine if the point in the path meeting my cursor is one that will block the path or not and not merely being on the path ?
 

rIKmAN

Member
Sounds like a good idea but how do i determine if the point in the path meeting my cursor is one that will block the path or not and not merely being on the path ?
Ah I misunderstood what you meant.

In that case you could mark the cell your cursor was over as blocked, then generate a path with this block data included (only do this once when the cursor moves to a new block), and then if a path is still able to be generated allow the block to be placed, or if the path cannot be generated then do not allow the placing of the block.

Remember to clean up after yourself and delete the extra path, free the blocked cell if the path was invalid etc.
 

YellowAfterlife

ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Moderator
Yup im using that at the moment to check when placing a wall if it has actually blocked the path and to remove it. Im just unsure of how to check it before ive actually placed it and to show the square as red to show that you cannot place a wall there.
You can mp_grid_add_cell/mp_grid_add_rectangle to add the wall to grid (and get rid of it via mp_grid_clear afterwards) without placing the instance
 

asollazzo

Member
Ah I misunderstood what you meant.

In that case you could mark the cell your cursor was over as blocked, then generate a path with this block data included (only do this once when the cursor moves to a new block), and then if a path is still able to be generated allow the block to be placed, or if the path cannot be generated then do not allow the placing of the block.

Remember to clean up after yourself and delete the extra path, free the blocked cell if the path was invalid etc.
Will give that a try :)

You can mp_grid_add_cell/mp_grid_add_rectangle to add the wall to grid (and get rid of it via mp_grid_clear afterwards) without placing the instance
Wouldnt that essentially be adding the wall/block just without the graphic and end up blocking the path aslong as my cursor is there or am i misunderstanding how it works ?
 

asollazzo

Member
Thanks for the help guys, but i think this might be beyond my abilities, no matter how i try to do the block testing it ends up actually blocking the enemies path and causing problems. The only way i havent tried is having a second mp_grid just for testing blockages without effecting the enemies movement but im not sure thats ideal or viable.
 
Top