Jump to content


Photo

Finding the instance that has the highest speed


  • Please log in to reply
3 replies to this topic

#1 Person2

Person2

    GMC Member

  • GMC Member
  • 4 posts
  • Version:GM8

Posted 16 April 2012 - 03:56 AM

In my game, I have enemies moving at random speeds. There's a stationary turret that's supposed to shoot at the enemies that are moving the fastest. All of the enemies use the same object. I have no idea how to get the turret to detect how fast an enemy is moving. How can I go about this?

Thanks in advance.

Edited by Person2, 16 April 2012 - 04:32 AM.

  • 0

#2 Desert Dog

Desert Dog

    GMC Member

  • Global Moderators
  • 6409 posts
  • Version:Unknown

Posted 16 April 2012 - 03:59 AM


var top_speed, inst; //declare 2 temporary variables.

inst=noone;   //this is a reference to the fastest enemy

top_speed=0; //this is a reference to the top speed (so far!)



with (obj_enemy) //loop through the enemy objects

{

    if  (speed>top_speed) //check their speed...

   {

         inst=id; //if they have the top speed, store a reference to them in inst!

    }

}



if instance_exists(inst) //always double check..

{

//inst is the fastest!!

//shoot him!!

}


  • 0

#3 Person2

Person2

    GMC Member

  • GMC Member
  • 4 posts
  • Version:GM8

Posted 16 April 2012 - 04:35 AM

Thank you very much! It worked, although I had to add this line below the "inst=id;" :

top_speed = inst.speed;

  • 1

#4 Desert Dog

Desert Dog

    GMC Member

  • Global Moderators
  • 6409 posts
  • Version:Unknown

Posted 16 April 2012 - 04:57 AM

Good catch! :biggrin:
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users