Play random sound effect via script call

Discussion in 'Javascript/Plugin Support' started by jrsuri, Apr 16, 2018.

  1. jrsuri

    jrsuri Villager Member

    Messages:
    8
    Likes Received:
    0
    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:
    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?
     
    #1
  2. Jonforum

    Jonforum Veteran Veteran

    Messages:
    1,581
    Likes Received:
    1,360
    Location:
    Canada / Québec
    First Language:
    French
    Primarily Uses:
    RMMV
    PHP:
    let ran = ["1.ogg","2.ogg","3.ogg","4.ogg"][~~(Math.random()*4)];
    AudioManager.playSe({name: `${ran}`, pan0pitch50volume70});
    //or other sugest
    PHP:
    const list = ["1.ogg","2.ogg","3.ogg","4.ogg"];
    AudioManager.playSe({name: `${list[~~(Math.random()*list.lenght)]}`, pan0pitch50volume70});
     
    Last edited: Apr 16, 2018
    #2
  3. jrsuri

    jrsuri Villager Member

    Messages:
    8
    Likes Received:
    0
    First Language:
    Português
    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!
     
    #3
  4. Jonforum

    Jonforum Veteran Veteran

    Messages:
    1,581
    Likes Received:
    1,360
    Location:
    Canada / Québec
    First Language:
    French
    Primarily Uses:
    RMMV
    try replace let by var
     
    #4
  5. jrsuri

    jrsuri Villager Member

    Messages:
    8
    Likes Received:
    0
    First Language:
    Português
    Primarily Uses:
    RMMV
    I get this error:
     
    #5
  6. jrsuri

    jrsuri Villager Member

    Messages:
    8
    Likes Received:
    0
    First Language:
    Português
    Primarily Uses:
    RMMV
    This one didn't work too.
     
    #6
  7. Jonforum

    Jonforum Veteran Veteran

    Messages:
    1,581
    Likes Received:
    1,360
    Location:
    Canada / Québec
    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 ?
     
    #7
  8. jrsuri

    jrsuri Villager Member

    Messages:
    8
    Likes Received:
    0
    First Language:
    Português
    Primarily Uses:
    RMMV
    It worked now, maybe I was typing something wrong. Thanks!
     
    #8

Share This Page