Help needed with a variable and instance_nearest
#1
Posted 17 October 2011 - 08:58 AM
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!
#2
Posted 17 October 2011 - 09:11 AM
then in step event of obj_friendly:
if lane=obj_enemy.lane{///do something here}
#3
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}
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()
#4
Posted 17 October 2011 - 09:27 AM
e = enemy, f = friendly
----e
--f-------e <- needs to target this one!
Thanks
#5
Posted 17 October 2011 - 09:41 AM
yeah yeah,I know that,
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}
That will only check for one instance of obj_enemy.
You could try something like thisvar nearest; nearest = instance_nearest(x,y,obj_enemy) if(lane==nearest.lane) attack()
But He is not asking on how to check all instances.
Edited by amiel, 17 October 2011 - 09:56 AM.
#6
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 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users











