GML Ally and ememy AI

I have some code below for a simple ally Ai. However I am not sure how to have the ally drone skip the top code if there are no enemies nearby. If I use if(!instance_exists(obj_Player)) exit; then the ally will just float off brain dead. Very new to coding and not sure what to do here.

---------------------------------------------------------------------------------------------------------------------------
Ally Drone step:
if (point_distance(x,y, obj_Par_Enemy.x, obj_Par_Enemy.y) < 500){
follow_character(obj_Par_Enemy, 400, 300);
shoot_target("lazer", obj_Par_Enemy, 30, faction, 400);
}


else{
if(!instance_exists(obj_Player)) exit;
follow_character(obj_Player, 10000, 100);


}

--------------------------------------------------------------------------------------------------------------------------

follow_character script:
// Script assets have changed for v2.3.0 see
// https://help.yoyogames.com/hc/en-us/articles/360005277377 for more information
function follow_character(_character, _distance, _distanceStop){

var char = _character;
var dst = _distance;
var dsts = _distanceStop;

if(!instance_exists(char)) exit;

if(point_distance(x,y, char.x, char.y) <dst){
// Turn to look at player
var new_angle = point_direction(x,y, char.x, char.y);
new_angle = image_angle - angle_difference(image_angle, new_angle);
image_angle = lerp(image_angle, new_angle, 0.1);
direction = image_angle;

speed +=0.01;

// Clamp speed to maximum
speed = min (speed, 2)

if(point_distance(x,y, char.x, char.y) <dsts){
speed -= 0.02;
} else {
speed += 0.01;
}
} else {// otherwise, no change
image_angle = lerp(image_angle, direction, 0.1);
speed = lerp(speed,originalSpeed, 0.1);
}
}

--------------------------------------------------------------------------------------------------------------------

Shoot_target script:
// Script assets have changed for v2.3.0 see
// https://help.yoyogames.com/hc/en-us/articles/360005277377 for more information
function shoot_target(_weapon, _target, _speed, _faction, _distance){

var wp = _weapon;
var tgt = _target;
var spd = _speed;
var faction = _faction;
var dst = _distance

if(!instance_exists(tgt)) exit;

if(point_distance(x,y, tgt.x, tgt.y) <dst){
// Turn to look at player
var new_angle = point_direction(x,y, tgt.x, tgt.y);
new_angle = image_angle - angle_difference(image_angle, new_angle);
image_angle = lerp(image_angle, new_angle, 0.1);
direction = image_angle;

}

if _weapon = "lazer"
{
if(point_distance(x,y, tgt.x, tgt.y) <dst){
bulletCounter++;
if (bulletCounter >=40){
wp = create_Lazer(image_angle, spd, faction);
bulletCounter = 0;
}
}
}
}
 

Attachments

Fredrik

Member
if you need to exclude the first chunk of code if there is no enemy nearby you could try:
var e = instance_nearest(x,y,obj_enemy);
if distance_to_object(e) < range {// your code here.}.
 
My main issue is that I don't know how to get the ally drone to not crash my game whenever there is no enemy on the level. Everything works fine; ally drone follows player until they see a ship in range then attacks that ship until destroyed. But when all enemies are destroyed the ally drone's step event crashes the game since there are no enemy objects when it is checking for that. I am wondering if there is a way to have the game continue the code if there are no more enemies on the level.
 

bandman28

Member
Get the code that looks for obj_Par_enemy and put it in this bracket:
Code:
if (instance_exists(obj_Par_enemy)) {
   //stuff
}
else {
  //don't move
}
Or if all the levels are in one room, you can create a variable like enemy_count or something and use that.
I hope this helped! ;)
 

Jaydors19

Member
Bandman28 has the right idea. You can also alternatively use:
Code:
if instance_number(obj_specificenemy) > 0
{
// do stuff here
}
else
{
// it doesn't exist, so do nothing
}
 
Idk if this works well but I ended up making a code step for my chase_enemy script which looked for the target and put and output of exist = true, my follow player code then would exit if the chase_enemy code's exist was true. I do believe your code though is way more simple. Thank you guys. I was so confused on how to make the ally drone not crash the game when it doesn't see any more enemies.
 
Top