jrsuri

Villager
Member
Joined
Jan 1, 2018
Messages
8
Reaction score
1
First Language
Português
Primarily Uses
RMMV
I'm trying to play a random sound effect based on a variable.
My SE files have these names: sound1.ogg, sound2.ogg, ... sound9.ogg

Tried this:
Set the value of variable #24 to a random value between 1 and 9 via event.

Script:
AudioManager.playSe({name: 'sound'$gameVariables.value(24), pan: 0, pitch: 100, volume: 100});

Of course it didn't work, and it was the best idea I've ever had. I guess it's something simple, but I'm a complete noob in js. Can anyone help me with the code?
 

Jonforum

Veteran
Veteran
Joined
Mar 28, 2016
Messages
1,634
Reaction score
1,457
First Language
French
Primarily Uses
RMMV
PHP:
let ran = ["1.ogg","2.ogg","3.ogg","4.ogg"][~~(Math.random()*4)];
AudioManager.playSe({name: `${ran}`, pan: 0, pitch: 50, volume: 70});

//or other sugest
PHP:
const list = ["1.ogg","2.ogg","3.ogg","4.ogg"];
AudioManager.playSe({name: `${list[~~(Math.random()*list.lenght)]}`, pan: 0, pitch: 50, volume: 70});
 
Last edited:

jrsuri

Villager
Member
Joined
Jan 1, 2018
Messages
8
Reaction score
1
First Language
Português
Primarily Uses
RMMV
PHP:
let ran = ["1.ogg","2.ogg","3.ogg","4.ogg"][~~(Math.random()*4)];
AudioManager.playSe({name: `${ran}`, pan: 0, pitch: 50, volume: 70});

That didn't work, ran into some error related to that "let ran", but then I tried this:

x = ["sound1","sound2","sound3","sound4"][~~(Math.random()*4)];

AudioManager.playSe({name: x, pan: 0, pitch: 100, volume: 100});

Without the "let ran" and the file extensions, and it worked! Thank you!
 

Jonforum

Veteran
Veteran
Joined
Mar 28, 2016
Messages
1,634
Reaction score
1,457
First Language
French
Primarily Uses
RMMV
That didn't work, ran into some error related to that "let ran", but then I tried this:



Without the "let ran" and the file extensions, and it worked! Thank you!
try replace let by var
 

jrsuri

Villager
Member
Joined
Jan 1, 2018
Messages
8
Reaction score
1
First Language
Português
Primarily Uses
RMMV
PHP:
const list = ["1.ogg","2.ogg","3.ogg","4.ogg"];
AudioManager.playSe({name: `${list[~~(Math.random()*list.lenght)]}`, pan: 0, pitch: 50, volume: 70});

This one didn't work too.
 

Jonforum

Veteran
Veteran
Joined
Mar 28, 2016
Messages
1,634
Reaction score
1,457
First Language
French
Primarily Uses
RMMV
it work on my side, you probably do a mistake ! ?
Can you send a print screen with the snippet and console error ?
 

jrsuri

Villager
Member
Joined
Jan 1, 2018
Messages
8
Reaction score
1
First Language
Português
Primarily Uses
RMMV
it work on my side, you probably do a mistake ! ?
Can you send a print screen with the snippet and console error ?

It worked now, maybe I was typing something wrong. Thanks!
 

Latest Threads

Latest Profile Posts

so... I decided since a park was an area in my game... here's an enemy that's slide with a hand behind it. cause why not.
Hot chocolate and music make studying and note-taking much easier :kaoluv:
Until my hand cramps up from writing too much!
Doing RPG Maker News for 21st September 2021


Malak is still waiting for Dark Deception Chapter 4... This was him yesterday after another sugar crash.
Again, he's fine. He's just bored at this point, considering he's exhausted his entire vacation bucket list.
Am I the only one who files custom resources under specific folders to remember who to credit? :kaoswt2:
1632263519730.png

Forum statistics

Threads
115,270
Messages
1,088,497
Members
149,875
Latest member
BlaireBear1
Top