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

Trying to figure out the best way to fix this targeting issue

SophosMoros

Member
I may be doing this all wrong but I have an issue with targeting. I have a unit and a base and I'm trying to tell the units to target the units first and if there are no units around to target the base.

if(instance_exists(obj_enemy_ant)&& instance_nearest (x, y, obj_enemy_ant))
{
foe = instance_nearest(x, y, obj_enemy_ant);
}
else if(instance_exists(obj_enemy_base) && instance_nearest(x,y,obj_enemy_base))
{
foe = instance_nearest(x, y, obj_enemy_base);
}else{
foe = -1
}
Currently this solution works just fine unless there are multiple bases. Which there will be. I want to fix this to where as long as there are no enemies in the area of that base they can shoot the base. Right now it's waiting until there are no enemy units in the entire level before it will attack a base.

Do I need to set a range around the target base and say if it's clear within a range of the base or is there a better way of doing this?
 
S

Supercoder

Guest
You could go for proximity, e.g. distance_to_object(obj_enemy_ant)<1000 go for ant, otherwise go for base. You could extend this so it checks the nearest base for nearby enemies, and if it doesn't have any close by the object goes for the base.
 

Yal

šŸ§ *penguin noises*
GMC Elder
Another fun idea you could do would be to use 'weights' to make enemies matter more than bases, but also take distance into account.
Code:
var mostvital = noone, neardist = 9999999;
with(obj_enemy_ant){
  dd = 3*point_distance(x,y,other.x,other.x)
  if(dd < neardist){
    mostvital = id
    neardist = dd
  }
}

with(obj_enemy_base){
  dd = point_distance(x,y,other.x,other.x)
  if(dd < neardist){
    mostvital = id
    neardist = dd
  }
}

if(instance_exists(mostvital)){
  foe = mostvital
}
 
W

wagyu_so_gud

Guest
If you're not already, I'd suggest creating parents for the enemy and base. That way, you can differentiate two or more of the same obj.

here's my suggestion. I think it should work. ;)

Code:
var inst_nearest_enemy = instance_nearest(x,y,par_enemy);
var inst_nearest_base = instance_nearest(x,y,par_enemy_base);

if (inst_nearest_enemy != noone) && (inst_nearest_base == noone) //enemy exists, base does not
{
foe = inst_nearest_enemy;
}
else
if (inst_nearest_enemy == noone) && (inst_nearest_base != noone) //enemy doesn't exist, base exists
{
foe = inst_nearest_base;
}
else
if (inst_nearest_enemy != none) && (inst_nearest_base != noone) //both enemy and base exists
{
        if distance_to_object(inst_nearest_enemy) < distance_to_object(inst_nearest_base) //check to see if distance to enemy is closer than the base, otherwise, go for the base
        {
        foe = inst_nearest_enemy;
        }
       else
       {
       foe = inst_nearest_base;
       }
}
 
Top