Our script returns true or false depending on the outcome of the path creation check, which we will use as an additional check later in our tech blog. This step isn't really necessary, but I find that it gives the path a better "feel" later.
However if a path is found, it sets the path type to 1 to make a "smooth" path, and it sets the path precision to 8 to make the path as smooth as possible.
#Gamemaker studio 2 path code#
So, our code first tries to create a path through the MP grid, and if none is found, it tells us with a message in the Output Window (you would normally have some failsafe code in here to catch this problem and deal with it, but for our tech blog, a simple message is fine). Note that if the function returns true, it also creates the path for you and assigns it to the path resource that you use as the second argument (in this case the global path we defined at the start). This function will calculate a path between the two given points and returns true if one is found (ie: no obstacles flagged in the grid block it) or it will return false if none is found.
This code gets the start and finish coordinates for our path and then uses them in conjunction with the function mp_grid_path(). Show_debug_message("ERROR: mp_grid_define_path() - No path created") If !mp_grid_path(global.ai_grid, global.ai_path, _sx, _sy, _fx, _fy, true) / Create a path between two points using the path and MP grid / finish_y The finish Y position for the path
Now copy the following code into it: /// mp_grid_define_path(start_x, start_y, finish_x, finish_y) We need a script to create the path that the skeleton instances are going to follow, so make a new script resource and call it mp_grid_define_path. The final "function" in the code isn't a function at all, it's a script that we are going to write now to create the path between two points. These "flagged" cells will be the ones that we want the enemy instances to avoid. All this function does is loop through all instances of the object "obj_Wall" in the room, and then use their position to "flag" a cell in the MP grid. We call this a dynamic path, since it is one that will be created dynamically and change throughout the game (unlike the pre-defined path resources that can be created in the GameMaker Studio 2 Path Editor).Īfter that we then add the wall instances into the MP grid. We also make another global variable and assign a new dynamic path ID to that. You should note here that since MP grids are quite resource heavy, you should never make the grid smaller than is absolutely necessary - the smaller the cell size, the more processing it requires and the more possible it is that your game will lag later. With this code, we are creating a grid that is 16 x 12 cells in size (we divide the room width and height by 64 to get the number of cells since our "base" block size in the game is 64) and we are assigning its ID to a global variable. Mp_grid_define_path(obj_Start.x, obj_Start.y, obj_Finish.x, obj_Finish.y) Mp_grid_add_instances(global.ai_grid, obj_wall, false)
With the Create event open, add the following code: global.ai_grid = mp_grid_create(0, 0, room_width / 64, room_height / 64, 64, 64) In general, it's good practice when using MP grids to only ever create one and have all instances access that, since creating and using MP grids is a fairly processor intensive task. Both the grid and the path will have their unique ID's stored in a global scope variable - we use global variables since we only need one single path and one grid for all instances to use in this project. Here, apart from creating our MP grid, we will also make a single path resource too. To get started, we'll first need to create our MP grid resource, so open up the object "obj_Control" and open the Create Event code block now. You then assign this path resource to an instance and it will look to the player like the instance is displaying "intelligence" as it neatly avoids obstacles while following the path. This grid is then used by another mp_* function to create a unique path resource that will try to go around those squares flagged as "occupied" and go through those ones that are not. Which is where the MP grid comes into play!įor those that maybe haven't dipped their toes into this water yet, an MP grid is a "motion planning grid", and all it does is section up a room into individual grid "squares", and each of these squares can then be flagged as "occupied" or not. We need to spice things up a bit and have our enemy change direction and react dynamically to things that the player does, and in this case we are going to have it change direction and avoid walls that the player adds into the room while playing.