[GameMaker Tutorial] Enemy AI Maze Pathfinding
Вставка
- Опубліковано 11 чер 2015
- Check out my popular GameMaker Book and GameMaker Courses: www.heartbeaststudios.com/
Follow me on Twitch for GameMaker livestreams: / uheartbeast
Follow my twitter: / uheartbeast
Like my Facebook page: / heartbeast.studios
Follow me on Tumblr: / uheartbeast
Thank you all so much for your support! - Навчання та стиль
GUYS IMPORTANT!!!
HeartBeast forgot to destroy the path variable at the end. If you don't destroy it, it will cause memory leak!
Just put this at the Room End:
path_delete(path);
Thank me later ;)
Thanks I'll remember this
+Nikorasu Chan (Nichii) what is room end?
+Nikorasu Chan (Nichii) I guess you didn't watch 9:15 then
+DeathSwagga He only destroyed the grid, not the path.
+Nikorasu Chan (Nichii) So within the same event as 9 15 put path_delete(path)?
This is exactly what I was looking for - a no frills introduction to GMS's pathfinding.
My most humble thank you!
I've been banging my head on this on and off for months, and continued to bang my head with this tutorial since things just weren't working...but I finally got it! Thank you for helping me finally get my head around pathfinding.
your channel deserves the world, man
Mate I dunno if you read these but I wanna say this was really helpful, and worked a charm. You're also a cool guy!
This is amazing, I didn't think something like this would be possible with so little code! You've helped me so much, thank you!
Being able to see you explain the code helps me a lot, man, thanks! Looking forward for more tutorials!
12:12 he discovered the answer of the life, the universe and the rest lol
It is Vietnam flashback
This tutorial was great. Easy to use and to the point. Thank you!
Phenomenal work. Glad I bought your book too!
Nice one as usual Benjamin, thanks. I like the synopsis at the end, just to review.
amazing tutorial my friend,exactly what i was needing,cheers
Man, this is awesome! Definetly going to use it on my next RPG game. Thanks!
I did this in GMS2, and the the collision detection is set to the centre point rather than being pixel accurate, and its causing the enemy to overlap with the walls which looks bad. Do you know how to fix that?
Thank you very much. I've been looking for this for a while now and you explained it really good.
Edo Player Glad I could help :D
how do i add more then 1 instance. I tried just placing it under but the enemy went right through
This is awesome, thank you! So easy to quickly add AI into the game.
Question, is it also possible to use a tile-based pathfinding system, so without having to set wall objects? For instance, when u create generated map using wall and floor tiles.
How can I use this with gamemakers physics engine? My enemies stop if I active physics on them.
Would I need to recreate the grid between rooms? Or change the values on room start?
I've seen your courses on udemy. Your awesome. I might buy your course. keep going.
Hey How would i go about adding this to a game where the room is expanding as i move around
Would it be possible to spawn the enemy object into the room and have it path towards another object instead of the mouse? I'm thinking along the lines of a Tower Defense game where the path changes depending on how the player decides to place their towers. Also if there are anymore great tutorials relating to TD games, let me know please!
great tutorial thanks heartbeast!! now any idea how I can give the enemies collision with eachother on top of this pathfinding? If i added the obj_enemies to the grid like we did the walls, they cant move at all because it's checking for its own instance I think.
would this work with the AI we made for the Slimes in the Rpg beginner series?
if so how do i set it up
thank you so much for this amazing tutorial
Great video. I would like my enemy to navigate on dynamic objects. Example : Player casts nova spell and it creates few ice walls around him that should block the way. Should I just then update the grid ? What's the best way to detect new added "walls"?
+HeartBeast
How could you adapt this for a platformer like with gravity and such
owesome could you help me id like using sprite idle animation and it with walking jumping and so on. so may you to give the code where the enemy searcing by walking trough the wall and jump to find player
Cheers! Great tutorial.
if my obj_player collisiob with obj_wall, obj_enemy stops
how know how to fix this?
awesome tutorial HeartBeast,
interested to know how the A* Algo could be used in a platformer scenario where players and enemies need to take into consideration obsticles (obj_wall) and gravity + jumping.
would you perhaps be able to create in the future a tutorial like this?
Thanks!
Is it possible to do this in the Standard Edition of Gamemaker? Its not recognizing some functions...
I wanted to point out that this only works if the objects doing the pathfinding are the same size or smaller than the grid cells. If your object is larger, it will partially ignore collision detection. I haven't figured out a way to account for it just yet.
This is excellent, thanks HeartBeast.
I wish you would do a new video on this for people looking for GMS2 ( Even though this is still compatible. )
trying to incorporate this with your rts tutorial... right now i am having and issue where it will sometimes go through the objects but about 90% of the time it will find a way around my object.
this is also in my step event for my unit as thats where the move state is in
if (state == 'move')
{
scr_wizard_move()
if instance_exists(target)
if (mp_grid_path(global.grid, path, x, y, target.x, target.y, 1))
{
path_start(path, spd, path_action_stop, false)
};
}
You saved me! Thanks so much!
So what is the point of creating a variable call path and putting it equal to path add? I understand it all the way up until that point and then i see that and dont see a point to the path variable.
Is there a way to do this with ds_grids? so I can combine this with the Random World gen tutorial
+LiamDoesProgramming Why not just make two grids? Generation grid won't ever be used again after it generates the world anyway... Unless you want it to be infinite, well still, you won't be generating all the time, pathfinding will be used more.
Nice tutorial! Thanks a lot!
when I try to do this it gives me an error message in line two of the grid object, the equal sign is unexpected. any reason why?
billy mays here baxck with another GREEEEEAAAAATTTTT PRODUUUUCCCCTTTT
Thats really ggod because I needed it for my game!
Thank you!
Julius Herrmann Glad I could help
So if I use this in a desktop defender type game, where you can place towers and the enemies have to update their path whenever you do so, is it going to lag badly?
You would probably have to delete the current grid and add more "wall" objects. Otherwise, that is a great way to use this algorithm. :D
No you should be fine. Just update the grid and paths with each new placed tower :)
Awfulmasterhat Just a little tip if you don't know it yet. Use 'mp_grid_add_cell()' for new towers... it's a lot faster than deleting grid and adding all the towers etc again. You just have to calculate a position of that cell... X and Y indices which is just X, Y div wcell, hcell (if you start grid at [0, 0]).
On the other hand... if you... lose/destroy any tower use 'mp_grid_clear_cell()'... same as above but it marks that cell as free in your grid.
David Horáček Thank you!
does anyone know how to add more than one type of object to the grid (buildings or others (say im doing something like starcraft)
I'm in the process of learning how to make a top down shooter. I programmed a modified version of Shaun Spauldings collision code for platformers so it would work with my Top down shooter. I have a 3 burst machine gun and I can't wait to implement this when I get my enemies into the game. 😁
How do you make path finding with multiple targets. like a lot of zombies
How would i change the xscale based on where he is going? Please help.
hi, how can i do this if my wall is mask with tiles collision mask?
omgosh this is amazing ty so much
My enemy partially phases through walls sometimes. I followed this tutorial exactly with the only exception that I made the enemy chase a player object. What is wrong?
Hello guys!
I wonder if there is any way to find a path at an X / Y point.
Something like the instance_position, which returns true if a path were located in the inserted coordinates.
I looked in the documentation but found nothing about it. The two
functions (path_get_x / path_get_y) however they show the value of X / Y
already inside the path.
Thank you all!
Easy follow, thank you!
You are the best my friend.
hi big fan love your work, i'm using your level gen system with tiles, ds grids ect.
and i tried to overlay an mp_grid with objects in the same place as the wall to see if i can get the ai in this working in the random level gen slight problem it spawns right but as if its a mini map i guess you could say pretty much all of the instances are in the top left corner and seem to map out the same pattern that the level makes anyway i can fix this or is there a way to use the ordinal ds_grid for this without using objects
this is what i used to make it happen
in player create event
var width = room_width div CELL_WIDTH;
var height = room_height div CELL_HEIGHT;
var cx = width div 2;
var cy = height div 2;
global.aigrid = mp_grid_create(cx,cy,width,height,CELL_WIDTH,CELL_HEIGHT);
mp_grid_add_instances(global.aigrid,obj_ewall,false);
in level create event
this is in the for loop for creating the walls (btw)
if (grid[# xx+1, yy] !=FLOOR) instance_create(grid[# xx+1,noone],grid[# noone,yy],obj_ewall);
if (grid[# xx-1, yy] !=FLOOR) instance_create(grid[# xx-1,noone],grid[# noone,yy],obj_ewall);
if (grid[# xx, yy-1] !=FLOOR) instance_create(grid[# xx,noone],grid[# noone,yy-1],obj_ewall);
if (grid[# xx, yy+1] !=FLOOR) instance_create(grid[# xx,noone],grid[# noone,yy+1],obj_ewall;
that codes are compatible whit the game maker 8 ?
when I add more than 1 enemy, obviously all of them will move toward the Player once the Player get into the range "to be detected" by one of the enemies. How do I make it so that it's only that certain Enemy (that the Player approach) to chase after the Player, not triggering all Enemies?
Awesome! thanks for the video.
Amazing, thank you so much
Thats awesome! lot of thx !
working in GM Studio 2. My enemies are "cutting" corners in a "smooth fashion". As if the corners of the walls are rounded. But I set diagonal motion to false, and triple checked; the collision masks are all square.
How would you go about adding collisions with multiple enemies or moving entities with a grid system?
For anyone else wondering and still stuck a few years down the line:
pathList = []; //need to store paths so they can be deleted later
var i;
for (i = 0; i < instance_number(obj_enemy_base); i += 1){
enemyIds[i] = instance_find(obj_enemy_base,i);
pathList[i] = path_add();
show_debug_message("creating path " + string(pathList[i]));
enemyIds[i] .path = pathList[i];
}
Then delete with:
var i;
for (i = 0; i < array_length_1d(pathList); i += 1){
path_delete(pathList[i]);
show_debug_message("deleting path " + string(pathList[i]));
}
where appropriate for your code.
Great, it worked, thanks!
Hey Ben, I am trying to use the path finding with an RTS system it works but, the characters move up and down and around very violently which seems weird, can you supply a solution.
TheFlameincchannel ***** HeartBeast Instead of a grid will I need to use mp_linear functions followed by mp_potential functions? If you have a solution, reply and I will contact you personally.
how can add two instance collision for the grid??
how do i do this with the built in physics engine?
I've tried to figure this out on my own but what do I type instead of (mouse_x div 32)*32+16; if I want the object to follow obj_player?
this doesnt work they completely ignore walls added to the grid
obj_player.x and obj_player.y are the (x,y) coordinates for your player
Hey Heartbeast, I'm currently making an isometric game (30 degree angles) and am in need of pathfinding. Is there a way to make the AI snap to the isometric grid and only use 4 way movement? I can't find anything on it anywhere and don't know where to begin.
Thanks :)
+james drake I do not know what you mean 30 degree angle im not fond of isometric games but you could just try writing some if's on direction or hspeed n vspeed and set vertical movement on pathing to false so it move left right up and down and say if direction = 0 90 180 270 etc as many angles so if you decided to add diagnal movement and then do like image_index or sprite_index depending on how you send up you're images/animations
can you help me out with path find but for platform?
Hey, great tutorial, but how would you aproach a everchanging enviroment? Meaning you have to create and destroy the grids for new results in the pathfinding?
+sachonski You can add and delete objects from the grid with: mp_grid_clear_all and mp_add_instances
wow been along ime since i wach one of your vids(probably because i switched to unity3d) but sill good tutorial
To go along with this video, could you also go over some of the maze/dungeon creation algorithms?
gaurdian0digger I shared a useful video on that a few days ago. Let me see if I can find it for you: ua-cam.com/video/wb6u2JImsyE/v-deo.html
HeartBeast seem to me like the kind of guy that practices meditation. Waaay calm and zen bru.
+WolfySnackrib666 haha yeah i think i would be insulting in front the pc screen if i miss something in a vide for youtube
Dirtroad Games Haha yeap. And then reshoot the whole thing, as a perfectionist :P
How do I change pathfinder from following mousex and y to following the players constant coordinates?
replace mouse_x and mouse_y to object_player.x and obj_player.y
If your player object isn't called object_player, alter that as necessary.
I've attempted using this and the collision detection doesn't work properly. The object goes halfway inside of the object it's supposed to avoid.
does this work with gms2 too..?
will it still work if you put the grid code in a script and have the enemy run that script?
probably
Does this work in GMS2?
How do you make gamemaker tell you if you are giving it an impossible path? Currently if you click where it can't reach, the enemy just gives up. I want the game to basically check to see if a path is possible before allowing it... how would I do that?
Example: Tower Defense game. Enemy moves from point A to point B. You build a full wall between A and B and enemies just give up.... how do I prevent the game from allowing a perfect wall?
Check a path is possible from a point A to point B of your choice each time you build a wall block. If it is possible then allow placement otherwise prevent it being made.
For those having difficulty, try making sure it's a global mouse click. This is working for me in GMS:2.
Obrigado Ben!
My game freezez when i add multiple enemies what do to?
Once I try to use this method but the enemy was intersecting a bit with the wall.
I'm not sure why, but I started to use an a* extension.
When I do this the enemy crosses over the walls too, unless I set wall to solid then they just get stuck! Cant get the enemy to just avoid the wall!
+guigrillo101 Is your enemy sprite centered?
Yep, thats right, you are the best!
Thanks, great tutorial!! Is it possible to make it works with physics?
Roi Díaz Souto That is a good question, you would probably have to do some sort of a work around. I"m not sure how paths work with the physics engine.
Is there an option to remove an object from the grid? My player can destroy the walls so I want the enemy to be able to go through the space where the walls used to be.
you probably figured this out but you could update the grid each time (You would need to destroy the other to avoid a memory leak)
I know this isn't related but I couldn't find anything on it and I've learned the most from you so here we go. Would you be able to/ willing to make a tutorial on skill trees, and also like a level cap of sorts? I've tried a few ways but couldn't get any results.
Chuckyboy311 Hmmm, that sounds like it might take some thought for sure. I'll have to think about how I might teach that best.
HeartBeast Cool, thanks for the reply man
Wow ben! this tutorial is "AI-maze-ing!"
dinohorseaborus Nice haha
amAIzing
super nice
Ayyyyy......
Is there a way that instead of the enemy avoiding the walls while moving, it went over those walls as if they were a path, a street or something similar
Just have the walls outline the space you want your enemy to follow
How would this go along with the DS Grid we created in the Random Level generating?
Edit: I now see that there are already a few posts asking about this, haha
Will it lag if there are..let's say 500 AIs? each have different paths?
Amazing
How would i make the enemy chases my player object through a maze.
Replace the mouse coordinates with player coordinates. Also u need to use another event cus he used the mouse click button action. Step event should be fine ;-)
Gms2 trial version doesn't allow for Path_Add
Does anyone know the difference between a mouse event and a global mouse event? Why do I need to use the global one? Thanks :)
A mouse event only triggers when you use the mouse on the object.
How can I make it so that the enemy following the grid collides with other enemies instead of overlapping?
enemy->collision->enemy - Step event - Empty code.
No.. The code would be like this or similar.
mp_grid_clear////0
with (obj_parent) {
if id != other.id {
mp_grid_add_cell(....)///1
}
}
Or when character moves, mark 1 in the coords to walk in the grid and then a 0 in the previous coords
there would only be an empty code if I'm using physics but I'm not. I used 'if other.x > x {
x += 1;
}'
etc.
Use mp_grid (is the best option for me) or, another example... if !instance_place...
HeartBeast, How do you make the camera follow the screen and the sprites still be normal? I create a 1024x768 room, but then I dont know what should I put for the camera...
Check Shaun Spalding's video with camera views :) he shows everything
+UPI Design well if you are referring to this very example just set your view to the enemie obj... you would have to continously click to move from one side of the room to other but hey... theres others ways that are easy 2 like a invisible object that follows the camera that is controls by arrows or wasd
+Aura Octane Would not be very good for a profesional game as it would troubling to know where youre screen object is as when you hit edges of the room the screen stops as the objects continues
you're a genius
I can't use path_action_stop I don't know why! help
thank you.