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

Draw a path on a pathfinding without using draw_path

Didjynn

Member
Hello everyone,

I have a grid and a pathfinding working well and I would like to draw the path depending of my action points like this :


But the actual system I'm using isn't really good and anyway doesn't work anymore since I don't use objects anymore to make my map (I was using the distance and direction from the player to the mouse and then dividing it by the size of my map tiles (16px here) before to draw rectangles and use instance_nearest to place them).

Another thing I would like to do is, after my player began to follow the path, stop on each square and check something but I don't see how to do that. path_position only give the position in % not the actual x,y position or grid cell (and I'm actually using a grid for the pathfinding, I don't see any other way. My skill in programming is... relative since I never studied this).

Thank you for your time :)
 
Last edited:

Didjynn

Member
Annnnnnd... the code because it may help

Code:
grid = mp_grid_create(0, 0, rw, rh, 16, 16)
path = path_add()

for (var i = 0; i < room_width /16; i ++) {
   for (var j = 0; j < room_height /16; j ++) {
       if terrain[i, j] = "ocean" or terrain[i, j] = "montagneinfranchissable" {
           mp_grid_add_cell(grid, i, j)
       }
   }
}

mp_grid_path(grid, path, x, y, destx, desty, 1)
 

Didjynn

Member
Really, noone have an idea ? I try to find the player next position to draw a rectangle on this position then the next, etc and stop once he doesn't have enough action points. If he has 10 action points he can move of 10 squares and make a short stop on each. If the mouse goes further than 10 squares, the path only draw on 10 and the final stop would be on the 10th.

My old system was working but as I said, it wasn't perfect the way the player was following would in some cases add more squares than he should follow to go to his destination (when the perfect path to go should be 8 squares, the player would go on 10 for the same destination just because my system wasn't perfect).

I'm really blocked here, it's a "book where you are the hero" so there is much text and few gameplay so if I can't do that it won't even be a game anymore, just a ... book. I like books but I really want to make a game. Please, help :'(
 

Didjynn

Member
I'm still looking for a way to draw my path :)

Sorry for the 4 times post but I don't understand why I can't delete my last post to make a all in one, it would be cleaner :/
 

John Andrews

Living Enigma
Ok ok buddy everyone who has seen this post surely has an idea (or even the complete solution) to what you want to do, but they didn't understand a d4mn about what you wrote:

You could maybe draw the path as squares in the draw event, how? Well it's simple:

Code:
//IN THE DRAW EVENT:
for(i=0;i<path_get_number(path);i++)//We set the loop to run for as many points the path has
{
    px=path_get_point_x(path,i);//we set the path i point x
    py=path_get_point_y(path,i);//we set the path i point y
    px=floor(px/16)*16;//Snap the x point to a 16x16 grid
    py=floor(py/16)*16;//Snap the y point to a 16x16 grid
    draw_set_color(color);//Set a color for the squares
    draw_rectangle(px,py,px+16,py+16,false);//Draw the squares
}
This way your path should be shown in a custom manner, altough this is just an example, see it and examine it, then try to do it yourself, dont copy it!, There you go.

Hope I was of help!

- John
 

Didjynn

Member
Thank you very much, you have no idea how much it helps me :)

I was missing the way to get the position easily and believe me I looked for it for more than a week. Once you have it, yes it's quite simple.
This is the code I'm using now in the draw event :

Code:
if mp_grid_path(grid, path, x, y, round(mouse_x /size *size), round(mouse_y /size *size), true)  && follow = 0 && (mouse_y < rh/3*2 or journal = 0) {
   draw_set_color(c_white)
   path_length = round(path_get_length(path)/16)
   for (var i = 1; i < floor(path_length); i ++){
       px = path_get_point_x(path,i);
       py = path_get_point_y(path,i);
       draw_rectangle(px - size/2, py - size/2, px + size/2, py + size/2, false)
   }
}

Right now I have only one problem : if I move my mouse once I began to follow the path (I'm not really following the path I'm making a ds list of it to try to resolve this problem but coordinates seems to change) and move my mouse, the final destination change.

And here is the code in my step event to launch the path :

Code:
switch(mouse_button) {
   case mb_left :
       show_debug_message("peuxclic = " + string(peuxclic) + " follow = " + string(follow))
       if follow = 0 && peuxclic = 1 && (journal = 0 or mouse_y < rh/3*2) {
           path_length = round(path_get_length(path)/16)
           list = ds_list_create()
           compteurlist = 0
           for (var i = 1; i < path_length; i ++){
               px = path_get_point_x(path,i);//we set the path i point x
               py = path_get_point_y(path,i);//we set the path i point y
               
               ds_list_add(list, px)
               ds_list_add(list, py)
               compteurlist += 2
           }
           follow = 1
       }
}

if follow = 1 {
   var Dist = distance_to_point(ds_list_find_value(list, nextdeplace), ds_list_find_value(list, nextdeplace + 1))
   if Dist <= .1 {
       x = ds_list_find_value(list, nextdeplace)
       y = ds_list_find_value(list, nextdeplace + 1)
       
       pa -= densite[round(x/16), round(y / 16)]
       randevent = irandom(5)
       
       if nextdeplace + 2 < ds_list_size(list) { // on continue de se déplacer
           nextdeplace += 2
       }else{ // on arrête de se déplacer suppression de la liste
           nextdeplace = 0
           speed = 0
           follow = 0
           
           ds_list_destroy(list)
           /*path_delete(path)
           path = path_add()*/
           mp_grid_path(grid, path, x, y, round((mouse_x +8) /size) *size, round((mouse_y +8) /size) *size, true)
       }
   }else{
       move_towards_point(ds_list_find_value(list, nextdeplace), ds_list_find_value(list, nextdeplace + 1), .5)
   }
}
Thx again !
 
Last edited:

Didjynn

Member
Anyone have an idea ?

I found a link between this bug and another which is... quite strange.



It will be a little hard to explain but I will do my best.
Normally, rectangles are drawn to show the path I will follow, it stops to draw under my mouse (like the right side of the picture).
But there is a "+" shaped area where the last rectangle isn't drawn so there is no white under my mouse (like on the left side of the picture).

I have no idea why and it does not stop here : if I move my mouse while the player is moving and I asked him to go in the red area, everything is fine. If I do the same out of the red area (which is always a 3 squares area horizontally or vertically of the player) once the player reach the end of the path, he just leaves it and got the the top-left hand corner.

I have no idea why and I'm sure I gave all the code drawing or activating the path to follow.
All the code about drawing the path and the path itself are in this tread.

Code:
if mp_grid_path(grid, path, x, y, round((mouse_x +8) /size) *size - 8, round((mouse_y +8) /size) *size - 8, true)  && follow = 0 && (mygui < rh/3*2 or journal = 0) {
   draw_set_color(c_white)
   //draw_path(path, x, y, 1)
   path_length = round(path_get_length(path)/16)
   //show_debug_message("path length (grid) = " + string(path_length))
   for (var i = 1; i < path_length; i ++){
       px = path_get_point_x(path,i);//we set the path i point x
       py = path_get_point_y(path,i);//we set the path i point y
       draw_rectangle(px - size/2, py - size/2, px + size/2, py + size/2, false)
   }
}
I'm really lost and this is annoying. If a savior come around he is welcome ;)
 
Last edited:
Top