Play random sound effect via script call

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,623
Reaction score
1,439
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,623
Reaction score
1,439
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,623
Reaction score
1,439
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!
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.
Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD
How many parameters is 'too many'??

Forum statistics

Threads
105,862
Messages
1,017,047
Members
137,569
Latest member
Shtelsky
Top