Jump to content


Photo

Help needed with a variable and instance_nearest


  • Please log in to reply
5 replies to this topic

#1 Legend Hoods

Legend Hoods

    GMC Member

  • New Member
  • 816 posts

Posted 17 October 2011 - 08:58 AM

Hello,
I'm trying to achieve that the friendly object (obj_friendly) checks if the enemy object (obj_enemy) is on the the same lane (the variable lane, being equal to either 1, 2 or 3), en then changes the variable nearestenemy to that enemy object, so I can target him with my fighting code.
I hope someone can provide me with the needed code for this, as this is a thing Ï've been trying to figure out for a long time.
Thanks!
  • 0

#2 amiel

amiel

    GMC Member

  • GMC Member
  • 1117 posts
  • Version:Unknown

Posted 17 October 2011 - 09:11 AM

In their creation event give them both a variable "lane"
then in step event of obj_friendly:
if lane=obj_enemy.lane{///do something here}
:whistle:
  • 0

#3 mads2194

mads2194

    GMC Member

  • GMC Member
  • 368 posts
  • Version:Unknown

Posted 17 October 2011 - 09:25 AM

In their creation event give them both a variable "lane"
then in step event of obj_friendly:

if lane=obj_enemy.lane{///do something here}
:whistle:


That will only check for one instance of obj_enemy.

You could try something like this
var nearest;
nearest = instance_nearest(x,y,obj_enemy)
if(lane==nearest.lane) 
    attack()

  • 0

#4 Legend Hoods

Legend Hoods

    GMC Member

  • New Member
  • 816 posts

Posted 17 October 2011 - 09:27 AM

I understand what you mean, but I was trying to get it to work, even if you have a situation like this:
e = enemy, f = friendly

----e
--f-------e <- needs to target this one!

Thanks :D
  • 0

#5 amiel

amiel

    GMC Member

  • GMC Member
  • 1117 posts
  • Version:Unknown

Posted 17 October 2011 - 09:41 AM


In their creation event give them both a variable "lane"
then in step event of obj_friendly:

if lane=obj_enemy.lane{///do something here}
:whistle:


That will only check for one instance of obj_enemy.

You could try something like this
var nearest;
nearest = instance_nearest(x,y,obj_enemy)
if(lane==nearest.lane) 
    attack()

yeah yeah,I know that,
But He is not asking on how to check all instances.
:whistle:

Edited by amiel, 17 October 2011 - 09:56 AM.

  • 0

#6 SenakuJin

SenakuJin

    GMC Member

  • New Member
  • 2765 posts

Posted 17 October 2011 - 10:20 AM

I understand what you mean, but I was trying to get it to work, even if you have a situation like this:
e = enemy, f = friendly

----e
--f-------e <- needs to target this one!

Thanks :D


//set a maximum distance to target from
dis=99*99;
//set a default instance return, -4/noone in this case.
nearestenemy=-4;

//loop through all instance's of obj_enemy
with(obj_enemy){
//if lane is equal to the lane of this object.
if(lane == other.lane){
//get distance from self to currently cycled enemy
dis=point_distance(x,y,other.x,other.y);
//if under current max distance, set the max distance to this distance
//and the instance returned to this enemy object
if(dis<other.dis){
other.dis=dis;
other.nearestenemy=id;
}

}

}

//now 'nearestenemy' should contain the id of the nearest enemy on the same lane.
//or -4/noone if no id was found.


And that's pretty much it. the 'other.prefix' calls variables from the calling object (obj_friendly) and any variable w/o the other. prefix is from the currently cycled through instance of the enemy object (obj_enemy). Hope that helps.

Edited by SenakuJin, 17 October 2011 - 10:21 AM.

  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users