GML - Artificial Intelligence
From Global Programming Syntax
In a lot of games you will need an artifial intelligence (giving the computer a brain). This way the computer can respond to the players actions. The artifical intelligence scripts are as follows:
Duke Style Artificial Intelligence
Wondered how the artificial intelligence was written for old games made around the mid 1990's. Well here is an example of the First Person Shooter AI for games made around that period. Also, this AI can work for 2D games as well. For this example we will assume you have called the player obj_player. In obj_enemy object put the following in the Create event.
globalvar startai;
startai=0;
In the collision event for the bullet (not in obj_player): For this piece of script, the collision event must be in obj_bullet and not obj_player.
{
health-=8;
instance_destroy()
}
In the step event of obj_enemy:
{
var speeds;
speeds=4;
{
if distance_to_object(obj_player)<speeds*64 then
{
startai=1;
}
else
{
startai=0;
speed=0;
}
if startai=1 then
{
mp_potential_step(obj_player.x,obj_player.y,speeds,false);
if instance_number(obj_bullet)<1 then
{
instance_create(x,y,obj_bullet);
}
}
}
}
Note that on the third line of the script above, replace the number 4 with the speed of obj_enemy.
In the create event of obj_bullet:
{
move_towards_point(obj_player.x,obj_player.y,10);
alarm[0]=room_speed*3;
}
In the alarm 0 event of obj_bullet put:
{
instance_destroy();
}
Scrolling Shooter Boss Artificial Intelligence
In this script, you will see how to add an AI to the boss and at the same time, making the player able to attack the boss. Remember that if your object names are different to the example, you will need to change the below GML to match the names. First lets set up the player.
In the obj_player step event, add the following:
if y<room_height/3 then y=yprevious;
In the obj_player Global Left Press (or button to shoot) event, add the following (make sure it is global click event):
if instance_number(obj_player_bullet)<1 then
{
instance_create(x,y-20,obj_player_bullet);
}
You need to now create a bullet object for obj_player. Lets call that object obj_player_bullet. In obj_player_bullet, add the following in the create event:
{
move_towards_point(obj_boss.x,obj_boss.y,10);
alarm[0]=room_speed*2;
}
Then add the following to the Alarm 0 event for obj_player_bullet:
instance_destroy();
Now for obj_boss. Add the following to obj_boss step event:
{
if distance_to_point(obj_player.x,y)<room_width/4 then
{
if instance_exists(obj_player_bullet)
{
if obj_player_bullet.x-x<10 then
{
if room_width-x>room_width/2 then
{
move_towards_point(room_width-16,y,5);
}
else
{
move_towards_point(16,y,5);
}
}
}
else
{
move_towards_point(obj_player.x,y,8);
}
}
if instance_number(obj_bullet)<1 then
{
instance_create(x,y,obj_bullet);
}
if x<16 then
x=17;
if x>room_width-16 then
x=room_width-17;
}
And you now need to create a bullet for obj_boss. So call the bullet obj_bullet and add the following GML to obj_bullet create event:
{
move_towards_point(obj_player.x,obj_player.y,10);
alarm[0]=room_speed*2;
}
Then in alarm 0, add the following GML:
instance_destroy();
Basic Pacman Artificial Intelligence
For this Artificial Intelligence to work, you must have a 8 pixel/dot/gridmark gap on the left and on top of the enemy. This is so the computer can calculate movements more easily. So if your enemy is agains a wall on both sides (left+right side or top+bottom side) then you may find the enemy just moving in circles or not moving at all. So when designing your levels, you will need to make sure there is a small gap above and to the left of the enemy at all times. So now for the GML.
Place the following GML in obj_enemy Create event:
globalvar playerx,playery;
playerx=obj_player.x;
playery=obj_player.y;
if floor(random(3))=0 then
{
alarm[0]=room_speed*5;
}
else
{
alarm[0]=room_speed*3;
}
Now place the following GML in obj_enemy Alarm 0 event:
{
playerx=obj_player.x;
playery=obj_player.y;
alarm[0]=room_speed*5;
}
Now add the following GML into obj_enemy step event:
if distance_to_object(obj_player)<560 then
{
mp_potential_step(playerx,playery,4,true);
}
