Windows AI stops pathfinding when target collide with something[SOLVED]

E

eldinhelja

Guest
Hello, I have made a platform game and added AI who finds path and then follow the target in this case Player.
I made that player can fly, If I fly then Enemy spawns at the "spawner location" and from there he starts following me, BUT if I stop flying, basically if I collide with Grass Enemy instantly disappears and teleports to that corner.

-I tried various methods of PathFinding but always same.
With one method, when I stop (collide with something), enemies wouldnt teleport to the top corner, instead they would just stop in one place and again, If I start to fly (not colliding with anything) they would start following me again.

Code of AI pathfinder :
Code:
//END STEP EVENT
grid=mp_grid_create(0,0,room_width/32, room_height/32, 32,32);
path = path_add();
mp_grid_add_instances(path, obj_SolidParent, true);
mp_grid_path(grid,path,x,y,obj_Amila.x,obj_Amila.y,1);
path_start(path,3,path_action_stop, true);
That is it all.

My player step event :
Code:
var rkey;
rkey=keyboard_check(vk_right);
var lkey;
lkey=-keyboard_check(vk_left);
var jkey;
jkey=keyboard_check(vk_up);

move=rkey+lkey
hsp=move*movespeed
if(!keyboard_check(vk_space)){
if (vsp<10) vsp+=grav

if(place_meeting(x,y+1,obj_SolidParent) || place_meeting(x,y+1,obj_WALL)) {
    vsp = jkey*(-jumpspeed)
}
}


//Horizontal Collision
if(place_meeting(x+hsp,y,obj_SolidParent) || place_meeting(x+hsp,y,obj_WALL) ){

    while(!place_meeting(x+sign(hsp),y,obj_SolidParent) && !place_meeting(x+hsp,y,obj_WALL)) {
        x+=sign(hsp);
    }
    hsp=0;
}

//Vertical Collision
if(keyboard_check(vk_space)==false){

    if(place_meeting(x,y+vsp,obj_SolidParent) || place_meeting(x,y+vsp,obj_WALL)){
   
        while(!place_meeting(x,y+sign(vsp),obj_SolidParent) && !place_meeting(x,y+sign(vsp),obj_WALL)) {
            y+=sign(vsp);
        }
      vcol=true;
       vsp=0;
    } else{
    vcol=false;
    }
}



x+=hsp;
y+=vsp
Screenshot_2 (Custom).png
 

origamihero

Member
This may or may not help (because I don't exactly understand the problem very well), but if you want to move an object to a position and this object is currently moving along a path, you need to stop the path movement first (path_end). Then you can set its XY coordinates again, and start a new pathfind path for it.
 

BeardyElf

Member
You are creating new grid every step. Put this in your create event:
(sorry, dont know how to use code tags)

grid=mp_grid_create(0,0,room_width/32, room_height/32, 32,32);
path = path_add();
mp_grid_add_instances(path, obj_SolidParent, true); (dont add instances to path)
change it to mp_grid_add_instances(grid, obj_SolidParent, true);

remember to destroy grid and path when you dont need them anymore.
And if enemies can find path to player when in air but not in ground, it means player x/y is inside cell thats flagged "forbidden".
 
E

eldinhelja

Guest
Hello @BeardyElf,
Thanks for helpful reply. You solved my problem.

Enemies are following me now perfectly, just like I wanted.
This is what I did :
In the object_Enemy Create Event I put this code :
Code:
grid=mp_grid_create(0,0,room_width/16, room_height/16, 16,16);
path=path_add()
mp_grid_add_instances(grid, obj_SolidParent, false)
In obj_Enemy Step Event I put this code :
Code:
mp_grid_clear_all(grid)
mp_grid_add_instances(grid, obj_SolidParent, false)
path=path_add()
mp_grid_path(grid,path,x,y,obj_Amila.x,obj_Amila.y,1);
path_start(path,3,path_action_stop, true);
if (hp<=0) {
mp_grid_destroy(grid)
path_delete(path)
instance_destroy()
}
mp_grid_clear_all(grid) is there because, I can destroy and Spawn objects. So it needs to check it if there are freed cells or there are new ocupied cells.
I tried putting this to check only when I destroy or spawn objects so I could get better FPS, but I didn't go well because enemy needs to be created first and create grid and then I can check if grid exists...
Because the global variable GRID is put in obj_Enemy create event. So If enemy is not created, globalvar is not created as well, So I get an error if I try to check for variable grid while it doesn't exist...
ds_exists(grid, ds_type_grid) - Gives an error because grid variable doesn't exists.
 
Top