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

Need some insights on "sound" system

Rexzqy

Member
Hi, so I am making a sound system where the enemy will track the sounds you or npcs make. My code is basically finding the loudest sound and the enemy will run toward it if they are within a certain distance. It works ok when theres only 1 of them, however, lets say 2 enemies are far away from each other, and 2 separate sounds were made by each of the enemies. Only the first sound created would be recognized and the one nearby would walk to it and the second enemy wouldnt move to the second sound. Any help is appreciated! Below is my code:

if instance_exists(obj_sound)&&obj_sound.magnitude >= 50
{
var max_sound = noone;
var max_sound_num = -1;
with(obj_sound)
{
if (max_sound == noone || sound>max_sound_num)
{
max_sound = id;
max_sound_num = sound;
}
/// this is for finding out the loudest sound
}

if distance_to_object(max_sound)<100
{
mp_potential_step(max_sound.x,max_sound.y,cspd,false);
}
if (x = obj_sound.x&& y = obj_sound.y)||state=states.alert
{
instance_destroy(max_sound);
}
/// this is the code that make enemy run toward the sound
}

EDIT: Sry for my broken english, maybe a better way to phrase this is: can there be multiple "Loudest Sound" for enemies at different positions?
 
Last edited:

Nidoking

Member
obj_sound.magnitude
Don't do this. obj_sound is an object. magnitude is a variable that exists in instances of obj_sound. You need to have an instance of obj_sound to get the magnitude of it. When you get max_sound, that's an instance. You can use max_sound.x and max_sound.y, but not obj_sound.x and obj_sound.y. Rewrite this with that in mind.
 
B

booksmaster

Guest
I didn't realy understand the problem but I'd advice you to use something like k=madnitude/dist and find max_k and go to that target.
 
Top