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

Windows Objects moving towards themselves!

E

Emperor88

Guest
in my code i have a variable called action, this variable changes depending on what state/equipment the spaceship has, the main problem is that because the game has ships with repair tools, these ships will try to repair themselves forever. because the nearest friendly ship they can see is themselves. please help!

the code is as follows
if instance_exists(action) then
{
action = instance_nearest(x,y,action)
move_towards_point(action.x,action.y,Speed)
point_direction(action.x,action.y,x,y)
image_angle = direction
}
note that in this case action will ALWAYS be the same instance as the code it is on. EG
f instance_exists(Fighter) then
{
action = instance_nearest(x,y,Fighter)
move_towards_point(Fighter.x,Fighter.y,Speed)
point_direction(Fighter.x,Fighter.y,x,y)
image_angle = direction
}
(as you can see above this Fighter code will be executed by a Fighter thus causing in infinite loop)
any tips or fixes for this?
thanks ~emporer88
 

Roderick

Member
From the example in instance_find:

Code:
var i;
for (i = 0; i < instance_number(obj_Enemy); i += 1)
   {
   enemy[i] = instance_find(obj_Enemy,i);
   }
Once you've got the list of all valid targets, you need to find the closest one:

Code:
closest_target = noone; // clear target if there's one already stored

for (i = 0; i < array_length_1d; i++)
{
 if (enemy[i] != self) // Don't check distance if this is the calling object
 {
  if (closest_target == noone) // If there's no target yet
  {
   closest_target = enemy[i];
  }
  else if (distance_to_object(enemy[i]) < distance_to_object(closest_target)) // Check after bc d_t_o(closest_target) will return an error if c_t is still noone
   {
   closest_target = enemy[i];
   }
 }
}
I don't have acces to GM right now, and this is typed on my phone, so hopefully I don't have any mustakes. If it's typo-free, that should work to get the closest not-you target.

I just went along with the variables and objects in the code I copy/pasted. You will, of course, need to adjust them to yours.
 
Top