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

Game lagging when talk to NPC multiple times - audio. Frame rate drop.

C

Crispywrists

Guest
Hello!

So in my game, once you've done a certain task it flips global.cool to 1 and you can talk to an NPC called Sporty - so every time you press E on sporty he comes out with a different short 1-2 second audio clip.

The problem is I can feel / see the game slowing down hugely the more times I press E on Sporty. It's an extreme example but when I mash E on him the frame rate goes to -61 (according to the log) and it obviously becomes unplayable, and I know when I'm stuck in a game I have been known to press E many times, so this is a problem.

My code for the action is as follows:

(In the E pressed event)
:

if (distance_to_object(obj_cliff))<30


{
if (!audio_is_playing(snd_sportycool))
&& (!audio_is_playing(snd_sporty))
&& (!audio_is_playing(snd_sporty2))
&& (!audio_is_playing(snd_sporty3))
&& (!audio_is_playing(snd_sportyambient))

{
if global.cool = true


{
audio_play_sound (choose(snd_sporty,snd_sporty2,snd_sporty3,snd_sportyambient),1,0);
}
}
}
I've tried fiddling around with the audio settings - they're only 200-400kb in size each so they can't really get a million times smaller, and I feel even if they did it wouldn't fix the actual problem. It's like there's a memory leak, or the actions are getting repeated over and over - the lag doesn't go away if I leave and return to the room.

I assume it's something to do with the way I've coded it, or could it be something else? I hope it's glaringly obvious and you're like 'well duh you're an idiot that's a rubbish way of doing this' (but in a nicer way) and I can go back to mashing my E.

Is there a better way to code this? Or a way to flush the audio files / what's happened after each press? Is it the global variable in there?

Many thanks in advance!
 

Bingdom

Googledom
You should instead store the audio_play sound index to a variable and run a check
Code:
if !audio_is_playing(sound)
(Read about what audio_play returns in the manual)

Check that you have the right attribute for your sound file which can be changed when you open it from your resource tree, something like (high memory, low CPU).
 
C

Crispywrists

Guest
Brill thanks I'll have a read shortly and see if it helps any.

The sounds are set to high memory, low CPU - the part I'm mainly confused about is once all the sounds have finished playing, shouldn't it all return to normal speed, ie. stop lagging? Like if a resource heavy party of a game is happening, usually if you leave the area / the instance stops, your lag goes with it?
 

Bingdom

Googledom
Your game shouldn't be lagging when playing sounds. Check if there are any other scripts that could be lagging your game. Profile debugger should help ;)
 
C

Crispywrists

Guest
Your game shouldn't be lagging when playing sounds. Check if there are any other scripts that could be lagging your game. Profile debugger should help ;)
Phwoar I had no idea this was even a thing...nice!

I don't really know how to check which script is taking up the memory but I was able to delete the items one by one and find the source, turns out my object that creates / activates objects etc was continuously creating objects on top of each other, rather than just doing it once...

Amazing thanks for the help.
 
D

Dave Martinez

Guest
That code is messy. ;)
Code:
//(In the E pressed event)
//:

//var sound_timer;
//sound_timer = //when to play sound
var audio, range;

range = 30;
audio[0]=snd_sportycool;
audio[1]=snd_sporty;
audio[2]=snd_sporty2;
audio[3]=snd_sporty3;
audio[4]=snd_sportyambient;

if (distance_to_object(obj_cliff)) < range

{
    if global.cool = true
    {
        if (!audio_is_playing(audio[0]))
        && (!audio_is_playing(audio[1]))
        && (!audio_is_playing(audio[2]))
        && (!audio_is_playing(audio[3]))
        && (!audio_is_playing(audio[4]))
            {
            //global.cool = false // ?
            // alarm[0] = sound_timer; //alarm[0] = reset?
            //"alarm[0] code: global.cool = true;"
            audio_play_sound (choose(audio[1],audio[2],audio[3],audio[4]),1,0);
            }
    }

}
 
Last edited by a moderator:

Bingdom

Googledom
Code:
//(In the E pressed event)
//:

var audio;
audio[0]=snd_sportycool;
audio[1]=snd_sporty;
audio[2]=snd_sporty2;
audio[3]=snd_sporty3;
audio[4]=snd_sportyambient;

if (distance_to_object(obj_cliff)) < 30

{
    if global.cool = true
    {
        if (!audio_is_playing(audio[0]))
        && (!audio_is_playing(audio[1]))
        && (!audio_is_playing(audio[2]))
        && (!audio_is_playing(audio[3]))
        && (!audio_is_playing(audio[4]))
            {
            global.cool = false // ?
            //alarm[0] = reset? global.cool = true
            audio_play_sound (choose(audio[1],audio[2],audio[3],audio[4]),1,0);
            }
    }

}
That's not a good way of handling audio.
If you look at my first comment, you'll see why. ;)

Also, the problem is solved. It wasn't because of audio.
 
Top