0 Favourites

How do I check whether all instances are not on-screen?

  • I'm making a game and I want audio to play when there is at least one instance of an object on-screen. If there isn't, I want to turn it off.

    I've tried many things*, but currently I've resorted to making an instance variable 'OnScreen' and then, every tick, I make it so each instance either has 1 or 0, depending on whether they are on-screen. Then, I check whether an instance has 'OnScreen' = 1 and whether the sound isn't playing yet. If that's true, I play the sound.

    To turn it off, I look up the nearest instance (to player) and whether it has 'OnScreen' = 0 and then I turn off the sound.

    This works, but sometimes the closest instance isn't on screen, while a more further away instance is. There is no sound, even though I want there to be.

    I would love it if there would be some way to check whether there is any instance on-screen. Thanks for any help in advance!

    *I can't just simply check whether an instance is on-screen, because some are and some aren't. I want the sound to turn on even when not all are on screen, but at least one is.

  • Pick all instances, if on-screen add 1 to a global variable for each instance on screen. Check every tick. If global variable ever drops to 0 turn off the audio. Is how I would do it. There are a few ways though. Rather than add to a variable you could probably use picked count in the comparison as another way of doing it.

  • Construct 3

    Buy Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Buy Now Construct 3 users don't see these ads
  • I didn't realise there was a "pick all instances of an object" event. Why is it in system though? Shouldn't it be in the object category, for example as "pick by UID" is?

    Anyway, I was inspired and looked more into logic and found a much easier way of doing it:

      Is "Object" on-screen \/
        Tag "Sound" isn't playing -> Play "Sound"
      Else -> Stop "Sound"

    This seems to work. Thanks for the help anyway!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)