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

AI Turn Based Pathfinding not QUITE working

huenix

Member
I have a real-time turn-based game. When an AI "can_take_action" if they decide to move towards their target (instance_nearest()), need them to move in one direction 32px increment,
(that is the size of each step). The AI "can take action" is turned to false, til a timer event. Everything EXEPT using the pathfinding works. With that I get one of two things:

1) If (goalx/goaly) are set to the target, it over rides all other code, including "can_take_action" & "turn alarms events" and goes to the targets x/y in one go

or

2) if i set it to the +/-(32) (see code below) ... it works great until it comes to a wall... then it can't find away around unless i hard code it to with a "keep_more(up/down/left/right)" but still causes issues

does anyone know how to use pathfinding/grid do make this work, or how to edit my code to get it work properly???



var grid = mp_grid_create(0, 0, room_width/32, room_height/32, 32, 32)
var path = path_add();
mp_grid_add_instances(path, obj_solid, 1);
mp_grid_path(grid, path, x, y, x + move_x, y + move_y, false);
path_start(path, 1, path_action_stop, false)
 

angelwire

Member
If you don't have any moving walls then there's no need to create a grid every time an AI unit moves. I suggest creating a script called "set_target(x,y)" and then in that script you compute the path for the AI. Then I would avoid using the built-in path_start functions in favor of just using the path_get_point_x and path_get_point_y. A path created by mp_grid_path will create a path point for every cell, so you could start at path point 0 and then every time an AI moves, simply add one to a current_path_point variable and the AI would move there.

Here's some code you can try out:

The grid should be global and doesn't need to be changed. This should be in a grid_controller object or something like that so there's only one grid per room and the AIs aren't all making their own grids
GML:
grid = mp_grid_create(0,0,room_width/32,room_height/32, 32,32);
mp_grid_add_instances(global.grid, obj_solid, true);
AI object Create event:
GML:
path = path_add();
current_path_point = 0;

//If you're using 2.3 this can be a function
//If not then it needs to be a script
function set_target(_x,_y)
{
    mp_grid_path(global.grid, path, x, y, _x, _y, false);
    current_path_point = 0;   
}
How to move to the next point:
GML:
x = path_get_point_x(path, current_path_point);
y = path_get_point_y(path, current_path_point);
current_path_point += 1;

if (current_path_point == path_get_number(path))
{
    show_message("DONE");
}
This is a pretty general answer so if it doesn't answer your question right then let me know. Also, let me know what version of Gamemaker you're using
 

huenix

Member
thank you for this! .... i will try it out and tinker.... it does make since to me a bit.. i have bee thinking i need to use path points...

also, i had moved the grid to its own obj_grid.

I am using the 2.3 version of GM
 

huenix

Member
hmmm i am having a hard time getting it to unzip/download, but i will work at it. and get back to you.
 

angelwire

Member
It's not zipped, so you should be able to just download it and double click on the .yyz file, and then choose a save location.
 

angelwire

Member
can you screen shot the code so i can look at it. i still can't get it to open
Sure thing!
Here's a screenshot of the assets:
assets.png

The code itself is too much to screenshot, but I copied it here.
GML:
///Create event
//Create a grid and path
grid = mp_grid_create(0,0,room_width/32,room_height/32, 32,32)
path = path_add();
//Add all solid objects to the grid
mp_grid_add_instances(grid, obj_solid, true);
current_path_point = 0; //This variable holds the point on the path that the object is on

//Use this function to set the object's target position and restart the path
function set_target(_x,_y)
{
    mp_grid_path(grid, path, x, y, _x, _y, false);
    current_path_point = 0;  
}
//These two variables allow the object to slowly move from one path point to another
x_to = x;
y_to = y;
//How fast the object moves from one path to another (1=instant, 0=never move)
movement_speed = .2;
GML:
///Step Event
//Slowly move the object to the x_to and y_to
x = lerp(x,x_to, movement_speed);
y = lerp(y,y_to, movement_speed);
GML:
///Draw event
//Just drawing the path so you can see what's happening
draw_path(path, x, y, true);
draw_self();
GML:
///Space key pressed event
///This doesn't have to be "Space"
///This is an example of how you move to the next point on the path

//Use x_to and y_to to slowly move the object
//if you want the object to instantly jump, then replace with just x and y
x_to = path_get_point_x(path, current_path_point);
y_to = path_get_point_y(path, current_path_point);

//Increment the path point to tell the object to go to the next point
current_path_point += 1;

//This will happen when the object reaches the end of the path
if (current_path_point == path_get_number(path))
{
    show_message("DONE");
}
GML:
///Global mouse pressed event
//Doesn't have to be this exact event
//Just an example of how to create a new path and set the target position
set_target(mouse_x, mouse_y);

Good luck!
 
Top