Here's an updated version of the method. This one targets the nearest visible enemy (as in enemy of the pawn running this script. Not necessarily enemy of the player):
Code: Select all
newtarget[ ()
{
nearesttarget = StringCopy("none");
nearestrange = ALERTSIGHTDIST + 0;
for targetnumber=0 to MAXENEMYPAWNS step 1
{
targetname = StringCopy(TARGETPREFIX # targetnumber);
if(FastDistance(targetname,nearestrange))
{
if(IsEntityVsible(targetname))
{
if(GetAttribute(DAMAGEATTRIBUTE,targetname) > 0)
{
nearesttarget = StringCopy(targetname);
nearestrange = GetDistanceTo(targetname);
}
}
}
}
if(HOSTILEPLAYER)
{
if(player_range < nearestrange)
{
if(player_vis)
{
if(GetAttribute(DAMAGEATTRIBUTE,"Player") > 0)
{
nearesttarget = StringCopy("Player");
}
}
}
}
if(nearesttarget = StringCopy("none"))
{
return false;
}
if(nearesttarget = StringCopy("Player"))
{
TargetPlayer();
return true;
}
SetTarget(nearesttarget);
return true;
} ]
MAXENEMYPAWNS simply tells the script how many enemies there are in the level. And again, enemies refer to pawns that the script can target.
TARGETPREFIX defines the names of the pawns the script can target. This prefix is used to identify the "sides" of the pawns. The names of the enemies should be the prefix followed by a number starting from 0 (instead of 1 as I said in the old post). Like enemy0, friend28 etc.
I've tested this piece of script - not very thoroughly, though - but it should work. So if you're having problems, it would help a lot if you posted the whole script or at least the parts where you're using this method.