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

Legacy GM [SOLVED] with (other) not working

Dr_Nomz

Member
Code:
if place_meeting(x,y,obj_Barricade_1) && attack=0{
  audio_play_sound(snd_Empty,100,false);
  with (other){
    HP-=1;
  }
  attack=1;
  //instance_destroy();
}
If the obj collides with the barricade, then with(other) should take away it's health, right? But every time it's run it acts like the attack object hasn't had it's HP value set, like it's completely ignoring the "with other" statement. I'm not trying to take HP out of the attack, I'm trying to take it out of the barricade.
 
The keyword 'other' only works in a collision event, or within a with expression. For example, these two use-cases will do what you expect them to:
Code:
// With
with obj_player {
   health -= other.damage;  // Player's 'health' minus our 'damage'.
}

// Collision event:
// (In a collision)
with other {  // With the instance we're colliding with.
   health -= self.damage;  // Other's health minus our 'damage'.
}
For this case, you'll either want to go to the Collision Event (lame imo), or do the instance capturing process yourself. For that, you need more than a bool check like 'place_meeting'. I usually use 'collision_rectangle', which lets you define a boundary for the collision, but you can also use 'collision_point' - as long as the function returns the id of the collided instance, you're good. Some example code:
Code:
var _inst = collision_point(x, y,obj_Barricade_1, false, false);
if instance_exists(_inst) && !(attack) {
  audio_play_sound(snd_Empty,100,false);
  with (_inst){
    HP--;  // You get the idea.
  }
}
 

FrostyCat

Redemption Seeker
For this case, you'll either want to go to the Collision Event (lame imo), or do the instance capturing process yourself. For that, you need more than a bool check like 'place_meeting'. I usually use 'collision_rectangle', which lets you define a boundary for the collision, but you can also use 'collision_point' - as long as the function returns the id of the collided instance, you're good.
You should know there is an instance-ID-oriented true equivalent to place_meeting(), and it's instance_place(), not collision_rectangle().
 

Dr_Nomz

Member
Yeah I figured that out and it seems to work fine:
Code:
if place_meeting(x,y,obj_Barricade_1) && attack=0{
  audio_play_sound(snd_Empty,100,false);
  with (instance_place(x,y,obj_Barricade_1)){
    HP-=1;
  }
  attack=1;
  //instance_destroy();
}
Thanks for telling me about how Other works, that's good to know it's useless outside of a collision event.

Also, this does allow the barricade that's been hit to be the one, and only one that takes damage. But even though I think it works fine, is there anything I should know about before continuing to use this? (Since I start off with place_meeting instead of instance_place)
 

TsukaYuriko

☄️
Forum Staff
Moderator
Yes, namely that you're essentially doing the same collision check twice and that that's inefficient.

Store the result of instance_place in a variable. Use it in both the if statement and the with statement. Throw out place_meeting altogether.
 
Top