How to set up an array and pick a random element from it?

Discussion in 'Javascript/Plugin Support' started by vectorinox, Dec 29, 2016.

    Tags:
  1. vectorinox

    vectorinox Airport Luggage Veteran

    Messages:
    45
    Likes Received:
    9
    Location:
    Singapore
    First Language:
    Singlish
    Sorry, I'm still new to java, and I don't really know how to apply the code from stack overflow to rmmv.


    Basically I'm setting up an event (chest) that gives random item from a pool (array with their IDs).


    So I come up with something like this:


    pool = {12,23,44,56,77};


    loot= Random().pool(array.length);


    $gameParty.gainItem($dataItems[loot], 1);


    of course it doesn't work... can someone show me the proper way?
     
    #1
  2. Shaz

    Shaz Veteran Veteran

    Messages:
    37,359
    Likes Received:
    11,150
    Location:
    Australia
    First Language:
    English
    Primarily Uses:
    RMMV
    { } is an object


    [ ] is an array


    pool = [12, 23, 44, 56, 77];     


    loot = pool[Math.floor(Math.random() * pool.length];


    $gameParty.gainItem($dataItems[loot], 1);
     
    #2
  3. vectorinox

    vectorinox Airport Luggage Veteran

    Messages:
    45
    Likes Received:
    9
    Location:
    Singapore
    First Language:
    Singlish
    Thanks Shaz! That was fast.


    But when I run it, the game give "Syntax error, unexpected token ]". 


    I don't see anything wrong with the code, is it the game faulty?
     
    #3
  4. Shaz

    Shaz Veteran Veteran

    Messages:
    37,359
    Likes Received:
    11,150
    Location:
    Australia
    First Language:
    English
    Primarily Uses:
    RMMV
    Sorry - my mistake - I left out an )


    pool = [12, 23, 44, 56, 77];     


    loot = pool[Math.floor(Math.random() * pool.length)];


    $gameParty.gainItem($dataItems[loot], 1);
     
    #4
  5. vectorinox

    vectorinox Airport Luggage Veteran

    Messages:
    45
    Likes Received:
    9
    Location:
    Singapore
    First Language:
    Singlish
    Oh right, silly me. How come I didn't notice that. Thanks! It work perfectly.
     
    #5

Share This Page