Well, I got double ninja'd, but I need to address some issues in the replies before me.
What about this ?
Code:
target = instance_nearest(x, y, obj_players);
if (target != noone and target.team != team)
{
direction = point_direction{x, y, target.x, target.y}
image_angle = direction
}
1) The question was to check every object in the project, not just 1 obj_players.
2) You search for the nearest instance of all teams, then you check if that instance is from a different team.
You need to perform the operations in the opposite order.
Now, if there is an enemy close to you, but there is a partner even closer to you, you won't shoot the enemy.
3) What are the curley brackets after point_direction for?
That's unvalid syntax and will throw an error before even running the game.
Put this inside a script. Avoid using this too much, it can become resource intensive.
Code:
var ds = ds_priority_create();
ds_priority_add(ds, noone, 9999999);
with(obj_ai) if team != other.team ds_priority_add(ds, id, point_distance(other.x,other.y,x,y);
return ds_priority_find_min(ds);
ds_priority_destroy(ds);
It no enemy is found, it will return noone.
That only goes for the closest instance, and will have issues with multiple enemies.
1) Again, the question was to check every object in the project, not just 1 obj_ai.
2) Your method doesn't work if the nearest enemy target is further than 9999999 pixels away.
You could have better left the value "noon" out of the queue and checked if the queue was empty before returning a result.
3) Your code creates a memory leak.
You do realize the last line of your script will never get executed as you've put a return statement before it, don't you?
4) Your code is not as performant as mine, as it needs constant reallocation of memory and ignoring that,
it would still perform in a time complexity O( n * log(n) ), whereas my code performs in a linear time O( n ).
I inicate with n the amount of instances searched through by the with structure and assume that with goes through
an instance in constant time and that the priority queues perform in a typical O( log(n) ) time complexity.