Why Are Sounds Not Playing?

Ask and answer questions about making games and related topics. Unrelated topics go in that other forum.

Moderators: marionline, SDHawk

Post Reply
Eonhetwo
Red Slime
Posts: 48
Joined: Fri Oct 16, 2020 4:56 am
Location: Usa

Why Are Sounds Not Playing?

Post by Eonhetwo »

Hi folks, got a quick question,

So, I'm trying to make an alternate battle script where you must hit the spacebar at the appropriate time to land a hit, hopefully the gif below gives an idea of what I mean.

And here's the code for it, (well, the relevant part of the code anyways)

Code: Select all

while (check tag(6))
do(
alter npc (8,NPCstat:move type,npcmovetype:pace)

if (key is pressed (key:Space)) 
then (

 if (NPC x(8)==mex  && check tag(4))
then(
alter npc (8,NPCstat:move type,npcmovetype:standstill)
play sound (26)

show text box (2)
wait for text box
delete item(1,1)


monsterhp:=monsterhp--3
if &#40;monsterhp<=0&#41;
then &#40;
set tag &#40;3,on&#41;
set tag &#40;6,off&#41;
resume player&#41;
&#41;
else &#40; 

if &#40;check tag &#40;4&#41;&#41;
then &#40;  
alter npc &#40;8,NPCstat&#58;move type,npcmovetype&#58;standstill&#41;
play sound &#40;25&#41;

show text box &#40;3&#41;
wait for text box
delete item&#40;1,1&#41;

&#41; 
else&#40;
 playsound &#40;3&#41;
monsterdeath&#41;
&#41;&#41; 

wait&#40;1&#41;&#41;
replace backdrop sprite &#40;light,1&#41;
set npc position &#40;6,0,68&#41;
set npc position &#40;7,1,68&#41;
set npc position &#40;8,2,68&#41;
play sound &#40;27&#41;

end
The stuff I didn't include was declaring global variables, moving NPCs into place (the red crosshair is an NPC moving between two other NPCs) blah blah blah

Anyways!.. What's going sour here is this; Sometimes the sound won't play when it is supposed to! And it's kind of predictable in how it does this.

If I land a 'hit' three times in a row, the third hit does not play sound. If I land a 'miss' twice in a row, the second miss doesn't provide sound.

Soooo what's the deal? Is the fact that I'm using global variables instead of local variables at all related? Am I nesting too many 'if' statements? Any insight would be much appreciated![/code]
Attachments
SurrealBit0001.gif
SurrealBit0001.gif (277.26 KiB) Viewed 4216 times
Just a vessel for dank memes
User avatar
kylekrack
Liquid Metal Slime
Posts: 1244
Joined: Mon Jun 16, 2014 8:58 am
Location: USA
Contact:

Post by kylekrack »

The sound is probably not finished playing before the next one starts. You need to make the 'preempt' argument true in play sound. It should be "play sound(25, false, true)". Check out the dictionary entry for more details: http://hamsterrepublic.com/ohrrpgce/nig ... -playsound
My pronouns are they/them
Ps. I love my wife
User avatar
Bob the Hamster
Lord of the Slimes
Posts: 7684
Joined: Tue Oct 16, 2007 2:34 pm
Location: Hamster Republic (Ontario Enclave)
Contact:

Post by Bob the Hamster »

Have a look at the docs for "play sound" https://hamsterrepublic.com/ohrrpgce/ni ... -playsound

Notice that the optional "preempt" argument defaults to "false"

That means that if you play a sound, and then play it again a second time before the first play is finished, the second play will do nothing.

I find that default regrettable. Almost every time, what I want is:

Code: Select all

play sound&#40;ID, false, true&#41;
Which means that if the sound is already playing, it will be stopped, and replaced with the new sound.

Please let me know if this helps you! :)

EDIT: oops! typing-at-the-same-time-buddies! :D
Last edited by Bob the Hamster on Wed Jan 27, 2021 8:30 pm, edited 1 time in total.
Eonhetwo
Red Slime
Posts: 48
Joined: Fri Oct 16, 2020 4:56 am
Location: Usa

Post by Eonhetwo »

Yall are the BEST! It works like a charm, thanks lots!
Just a vessel for dank memes
User avatar
kylekrack
Liquid Metal Slime
Posts: 1244
Joined: Mon Jun 16, 2014 8:58 am
Location: USA
Contact:

Post by kylekrack »

Bob the Hamster wrote:EDIT: oops! typing-at-the-same-time-buddies! :D
;D
My pronouns are they/them
Ps. I love my wife
User avatar
Pepsi Ranger
Liquid Metal Slime
Posts: 1460
Joined: Thu Nov 22, 2007 6:25 am
Location: South Florida

Post by Pepsi Ranger »

My Convenience Functions scripting mod has a command that will "reset sound" when called. This allows you to cut off the in-progress sound and play a new iteration of it (if it's the same sound effect ID). This is great for shooters or anything that requires the same sound in rapid succession.

Of course, a better version (maybe for volume 2) would be to slightly alter the volume of the second sound to layer over the first one, and depending on how long it takes to finish each iteration of the sound, keep adding slight alterations to the volume for each successive repeat of that sound until the original finishes and the next iteration can play at the original volume. That would make resetting unnecessary in some repeat cases.

Might be something for James or TMC to tackle.
Place Obligatory Signature Here
Post Reply