External Exam Download Resources Web Applications Games Recycle Bin

ai patrol | gml



You can add as many objPoints as you like:



objPatroller Create

patrol_speed = 5;
patrol_points = ds_list_create();

//collect all points in the room:
for (var i = 0; i < instance_number(objPoint); i += 1)
{
    var new_point = instance_find(objPoint,i);
    new_point.image_alpha = 0; //make points invisble
    ds_list_add(patrol_points, new_point);
}

//goto first point:
var init = ds_list_find_value(patrol_points, 0); 
x = init.x;
y = init.y;

//determine target:
randomize();
var target_range = irandom_range(1, ds_list_size(patrol_points)-1);
target = ds_list_find_value(patrol_points, target_range);
//if you need to rotate your player sprite, uncomment this:
//image_angle = point_direction(x, y, target.x, target.y);
move_towards_point(target.x, target.y, patrol_speed);

objPatroller Step

if place_meeting(x, y, target){
    //determine new target:
    randomize();
    var target_range = irandom_range(0, ds_list_size(patrol_points)-1);
    target = ds_list_find_value(patrol_points, target_range);
    move_towards_point(target.x, target.y, patrol_speed);
};

objPatroller Draw GUI

draw_set_font(font0); //remember to add this to your resources
draw_text(5,5,"Patrol points found:");
for (var i = 0; i < ds_list_size(patrol_points); i++){
    draw_text(5,(25 + i*25),ds_list_find_value(patrol_points, i));
}