if the variable for nearest instance = 1: stuff happens

Hey,
basically, I want to make it so that if an enemy object with a certain variable (effect) set to 1 comes near enough to another enemy object whose var is still 0, then that enemy object's var 'effect' also becomes 1. Sort of that the effect spreads to all nearby enemies from each other.

I have this in enemy-object's Step:

GML:
if effect = 0{
if distance_to_object(instance_nearest(x,y,enemypar)) < 64{
with instance_nearest(x,y,enemypar)
    {
    if effect = 1
        {
        self.spd -=1;
        self.effect = 1;
        }
    }
}
}
Somethings wrong here tho, can't figure out what. Ive tried altering it slightly, but to no avail...
 

chamaeleon

Member
Hey,
basically, I want to make it so that if an enemy object with a certain variable (effect) set to 1 comes near enough to another enemy object whose var is still 0, then that enemy object's var 'effect' also becomes 1. Sort of that the effect spreads to all nearby enemies from each other.

I have this in enemy-object's Step:

GML:
if effect = 0{
if distance_to_object(instance_nearest(x,y,enemypar)) < 64{
with instance_nearest(x,y,enemypar)
    {
    if effect = 1
        {
        self.spd -=1;
        self.effect = 1;
        }
    }
}
}
Somethings wrong here tho, can't figure out what. Ive tried altering it slightly, but to no avail...
You'll be better off storing the instance found in a local variable, and only letting with() deal with things that need to affect that instance. Inside with() the current instance will be the found enemypar instance so spd and effect will be set on it, instead of whatever instance you're trying to find the closest enemypar instance for.
GML:
if (effect == 0) {
    inst = instance_nearest(x, y,enemypar);
    if (distance_to_object(inst) < 64) {
        if (inst.effect == 1) {
            spd = 1;
            effect = 1;
        }
    }
}
Could also be written like this but I don't see why one would given nothing else to do inside the with() block.
GML:
if (effect == 0) {
    inst = instance_nearest(x, y,enemypar);
    if (distance_to_object(inst) < 64) {
        with (inst) {
            if (effect == 1) {
                other.spd = 1;
                other.effect = 1;
            }
        }
    }
}
 
I can't remember the exact functionality for instance_nearest as I haven't used it in a long time, but I get the feeling that it can also return the calling instance, so a neat little trick to get around that is something like this:
Code:
var xx = x;
x += 10000;
var inst = instance_nearest(xx,y,enemypar);
x = xx;
This stores your current x in the local var xx, moves your instance far away from it's original position, checks for nearest enemies against the original position and then moves the instance back. So if you're still having trouble after following @chamaeleon's advice, you might need to do this.
 

Sabnock

Member
is this the sort of thing you are looking for?


GML:
if (effect == 0) {
    var _inst_id = instance_nearest(x, y, enemypar);
    if (distance_to_object(_inst_id) < 64 && _inst_id.effect == 1)  {
        effect = 1;
        spd -= 1;    
    }
}
 
Last edited:
thank you all! @chamaeleon 's fix didn't work for this, I haven't yet tried @RefresherTowel and @Sabnock s advices, but I found a workaround to my problem. Its a bit clumsier than what I originally hoped for, but will do for now, nevertheless I'll try the ideas you guys provided here and see if I can make it work with them as well.
 

chamaeleon

Member
thank you all! @chamaeleon 's fix didn't work for this, I haven't yet tried @RefresherTowel and @Sabnock s advices, but I found a workaround to my problem. Its a bit clumsier than what I originally hoped for, but will do for now, nevertheless I'll try the ideas you guys provided here and see if I can make it work with them as well.
Definitely give @RefresherTowel's idea a try. I missed that the code runs in an instance that is of the same object type that it is trying to detect (so the instance itself would be the closest, with a distance of zero, unless you temporarily move it away).
 
Top