Character is colliding with any object

Started by glurex, Mon 12/06/2023 20:31:07

Previous topic - Next topic

glurex

I have a scenario where I need to play a sound every time the player collides with ANY object in the room, not just a particular one. I have a code that executes repeatedly and accomplishes this perfectly, but only with specific objects.

Is there a way to achieve the same result with every object in the room using the IsCollidingWithObject function? That way, I can write a single function that works globally in every case.

PPCollision module have a function named "CWithAnything" that does exactly this, but I have problems with the module (maybe because it's old).

Thanks!

Khris

#1
This should work:

Code: ags
// above rep_exe
AudioChannel* objCollision;

void roomCollisions() {
  if (objCollision == null || !objCollision.IsPlaying) {
    bool playIt;
    for (int i = 0; i < Room.ObjectCount; i++) {
      if (player.IsCollidingWithObject(object[i])) playIt = true;
    }
    if (playIt) objCollision = aBang.Play();
  }
}

  // inside rep_exe
  if (player.Room >= 3 and player.Room <= 7) roomCollisions();

glurex

Quote from: Khris on Mon 12/06/2023 21:13:40This should work:

Code: ags
// above rep_exe
AudioChannel* objCollision;

void roomCollisions() {
  if (objCollision == null || !objCollision.IsPlaying) {
    bool playIt;
    for (int i = 0; i < Room.ObjectCount; i++) {
      if (player.IsCollidingWithObject(object[i])) playIt = true;
    }
    if (playIt) objCollision = aBang.Play();
  }
}

  // inside rep_exe
  if (player.Room >= 3 and player.Room <= 7) roomCollisions();

Thanks, Khris! Your first attempt before the edit was the very same I did (and it gave me a 'Type mismatch: cannot convert 'int' to 'Object*' error). But with this new code, it works perfectly! (I only have to change a little thing because I want to play the sound only once when the player enter the collision zone each time, but it was easy to do)

Khris

Not sure how my code (or previous versions) could've caused that error, it probably stems from accidentally using player.IsCollidingWithObject(i) I guess.

Anyway, glad it works :)

glurex

Quote from: Khris on Mon 12/06/2023 22:16:45(...) it probably stems from accidentally using player.IsCollidingWithObject(i) I guess.

You're right, Khris! That was the problem. This is how the code ended up so that the sound plays only once every time the player enters the collision zone... and it works without problem:

Code: ags
// above rep_exe
AudioChannel* objCollision;
bool hasCollided = false;

void roomCollisions() 
{
  bool isColliding = false;

  for (int i = 0; i < Room.ObjectCount; i++) 
  {
    if (player.IsCollidingWithObject(object[i])) 
    {
      isColliding = true;
    }
  }
  
  if (isColliding && !hasCollided) 
  {
    objCollision = aCOLLISION.Play();
    hasCollided = true;
  }
  
  if (!isColliding)
  {
    hasCollided = false;
  }
}
// inside rep_exe
 roomCollisions();

Many thanks!

SMF spam blocked by CleanTalk