• 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!

Legacy GM Pathfinding - Find closest point before collision

MeBoingus

Member
Hi all,

I'm busy working on a basic (9-verb / Monkey Island-style) point-and-click adventure game, and I'm struggling to set up the walking / path-finding system.

Each room in the game will be surrounded by a collision mask, of sorts. My goal is to be able to click somewhere on the collision mask, and have the character object path-find to the point closest to the mouse position.


Illustrated below:
Ex.png

Where green represents the player, pink represents the path, and the red X represents the position that was clicked.

The way MP Grids work at the moment would result in no path being returned.


Any ideas?
 

OnLashoc

Member
I don't know how to achieve by way of grid as you explained but as they say "There's more than one way to skin a banana!" or something like that.

I would create multiple objects in the room or points that you need the character to move to along the way and in your character object I would put in the step event something like:

Code:
if instance_exsists(object or point you need character to walk to) && mouse_button_checked_pressed(mb_left or whatever you want it to be) && position_meeting(object / area you want character to move to){
character object.x ++1; (or whatever speed / direction you want it to go)
character object.y ++1; (or whatever speed / direction you want it to go)
}
Or something like that. I've been learning self taught the past year+ so I'm pretty new myself, but maybe this will help some how. And yes I'm sure there is a mostest betterer bestest'er way to do this & I'm a newb and maybe this might help.

Onnie
 

OnLashoc

Member
And to add, once the character object reaches the first object, I would have another line of code in the character step event like this:

Code:
if position_meeting( whatever the x & y of the position object) {
 oCharacter.x ++1;
oCharacter.y ++1;
}
So basically it will move from the starting point then when you click on the area you are pointing to click to move to, it will hit each object like train stations and cause the character to continue to move in the direction you want it to go until it reaches the desired end point. Again probably a cleaner better way to do this, but it will give you the desired effect with a little tinkering.
 

MeBoingus

Member
And to add, once the character object reaches the first object, I would have another line of code in the character step event like this:

Code:
if position_meeting( whatever the x & y of the position object) {
 oCharacter.x ++1;
oCharacter.y ++1;
}
So basically it will move from the starting point then when you click on the area you are pointing to click to move to, it will hit each object like train stations and cause the character to continue to move in the direction you want it to go until it reaches the desired end point. Again probably a cleaner better way to do this, but it will give you the desired effect with a little tinkering.
Hi Onnie :),

I actually just ended up solving this issue a short while ago, but thanks a bunch for your comment!

For anyone interested in how I achieved this:

It's a very hack-y way of getting it done, but because I'll only need this to work vertically, and I'm using a 16x16 MP Grid... I just do a loop (in increments of 16) up / down from the mouse_y, looking for a potential path.

Here's the code I'm using:
Code:
        var pathCheck = 0;
        while (pathCheck < room_height / 16) && (mp_grid_path(moveGrid, movePath, x, y, mouse_x, mouse_y + (pathCheck * 16), true) == false && mp_grid_path(moveGrid, movePath, x, y, mouse_x, mouse_y - (pathCheck * 16), true) == false)
        {
            pathCheck ++;
        }
For Onnie:

Just so you know, there's a few functions for moving objects towards specific points on the screen :) I.E. move_towards_point, distance_to_object, etc. So if you ever need to achieve this in the future, now 'ya know ;)!
 

OnLashoc

Member
Yep and I've used them, I just happened to be browsing while at work and seen your question. I knew my answer may not be THE answer, but maybe something in there would trigger your thought process to find the answer lol.

I find a lot of times when Im trying to figure something out and I've tried and tried on my own, I start looking around and usually I can't find the EXACT answer, but something puts me in the right direction and before you know it I've solved it on my own :)
 

NightFrost

Member
I've read a little bit about those systems, so for the interested parties: old point&click adventures used a series of rectangles to define their walkable areas and did some simple pathfinding maths to move characters to clicked positions (or edges of rectangles, if click was outside). Some later ones have defined their walkable area as a polygon, possibly with secondary polygons to poke holes (unwalkable areas) into that base polygon. They essentially use A* to find the way around convex corners. Then there's also a bit of maths involved when figuring out whether a mouse click was inside or outside a polygon. There were a few good resources on this around but I can't find them right now (should have bookmarked them...)
 
Top