(Solved) Check variables from multiple copies of the same object

Im trying to play a sound when a enemy is near.

if you are in range, play the sound.
if you are away of the range or the enemy is dead, the sound stop.

“near” variable of enemy is what trigger the sound.
The problem is when there are more than 1 enemy.

My idea is trying to check if all “near” variables near the player are true or false, to play or stop the sound correctly.
They way im trying to do, it can work? or, is there a better way to do this?

I don’t understand why you need variable for that where distance to player would be enough to play sound if enemy is in range?

I agree with ZeroX4, when distance of player is below x pixels and bool dead is false, you could just start the sound there. Also, do you only want one heartbeat sound played when any enemy approach, or a heartbeat sound for each enemy? How does your code work in game? Is it as you want?

Only for organization because i have all my sounds in a separate group event. I put everything on the same group just for the screenshot.
“near” var just trigger the sound on other group.

For example if you are facing a enemy, this suposed to work like it have to.

But when you engage 2 or more enemies within range and, for example, they die, the sound dont stop. Honestly, i dont know how it is evaluating the state of the variables here, because if you face 2 enemies and 1 dies, the dead variable changes to true, but the other one still has the false one and I dont know which variable is being prioritized.
Or for example if you are in range, and the code just need 1 “dead” true to stop the sound while you are being chasing by other enemy with “dead” false.

Is checking all the variables of enemies near the player or just the first one?

EDIT: i clean a bit the code to show the main mechanic, on this example there is not for each object. So the first enemy manage the sound mechanic. If the first die, or the player go far 200 pixel, the sound stop, even if the second is chasing you.
In for each object is the same history, but the sound dont stop, even if you kill al enemies.

is there a way to check all “dead” variables in range? and not only the first one?

So, I am thinking (and I am by no means an expert :slight_smile: ) what you want to do is something along these lines:

The reason being is that the variable right now is only working on one of all the enemies, so you want to cycle through all the enemies that are in range, because it is only cycling through one at the moment. At least this has been my experience.

Another thing, if it is shutting down all sound, you may want to link each enemies sound to it’s own sound channel, perhaps have a scene variable that gets 1 added to each enemy that gets made, and make each enemy instance get a unique sound channel identifier, so when they are dead, their sound gets turned off, without turning off the sound of the others (that is if the first solution doesn’t work) I am sure someone else with more knowledge may have a better solution, but this might be one way to tackle the issue.

Hope that may help! :slight_smile:

1 Like

Thanks for this guide :slight_smile: i tried the second option, i made the soundchannel id variable.

Now when i face the first enemy alone, the sound start and when i kill it, the sound stop and i can repeat the action if i go to the second one (works very well one by one).

But i still cant face 2 enemies at time while the sound persist if one is dead and only stop if both are dead.

Finally i could solve this with the condition “the number of object currently picked”, after the main condition, can be < 0, and the opposite = 0 so its usefull for conditions that include more than one of the same object under a specefic state.