Script call to see if a variable exists in an array.

Discussion in 'RPG Maker MV' started by LazESigma, Apr 18, 2019.

    Tags:
  1. LazESigma

    LazESigma Veteran Veteran

    Messages:
    50
    Likes Received:
    2
    Location:
    Chicagoland
    First Language:
    English
    Hello, I'm trying to simulate some aspects of a roulette wheel in a gambling game.
    I'm using variables and currently have the numbers working.
    I'm attempting to use a variable array to easily check if the Number entered is either Red or Black.
    Rather than have 36 conditional branches, I was hoping to use a script call in the event.
    Something like this:
    Variable 38 holds the Number the player entered. Variable 40 simply holds an array of all the Red Numbers.
    Variable 41 is a test variable to see if this works.
    No matter what Number I enter into variable 38, my 41 variable is always zero.


    obj = $gameVariables.value(38)

    arr = $gameVariables.setValue(40,[1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35]);

    for(var i=0; i<arr.length; i++)

    {

    if (arr == obj) $gameVariables.setValue(41,1)

    }

    Any help would be greatly appreciated!
     
    #1
  2. Doktor_Q

    Doktor_Q I'm not a real doktor, but I am a real Q Veteran

    Messages:
    581
    Likes Received:
    321
    Location:
    Denial
    First Language:
    English
    Primarily Uses:
    RMMV
    So it looks like red/black here is just odd vs even. In that case, you can save yourself the pain in the butt of writing them all out:
    Code:
    if (obj % 2 == 1) { do your thing here }
    % in javascript is the "modulo" or "remainder" operator, which nobody taught me about in highschool math for some reason. It has nothing to do with percents, so do not be confused.

    Basically, if you do "A % B", it divides A by B and gives you the remainder. 10 % 5 = 0, because 10 is an exactly multiple of 5. 12 % 5 = 2, because if you divide 12 by 5, you get a remainder of 2.

    So if you want to tell if a number is even or odd, all you gotta do is ask "if I divide it by 2, does it have a remainder?" Because that's how even and odd numbers are defined.

    https://www.w3schools.com/js/js_arithmetic.asp Check out javascript's arithmetic in general, it can save you a lot of trouble sometimes.
     
    #2
  3. Aloe Guvner

    Aloe Guvner Walrus Veteran

    Messages:
    1,600
    Likes Received:
    1,005
    Location:
    USA
    First Language:
    English
    Primarily Uses:
    RMMV
    The suggestion above is definitely the best if you want to check evens vs. odds. Note that red/black is different than even/odd in roulette. I wanted to go through your posted code and show why it wasn't working the way that you wanted it to:

    Code:
    arr = $gameVariables.setValue(40,[1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35]);
    This line of code isn't assigning anything to the variable you've named "arr". It's assigning the array [1, 3, 5, ...] to game variable #40.

    Code:
    if (arr == obj) $gameVariables.setValue(41,1)
    This line of code is comparing a number to an array. Those will never be equal to each other, a number can't be equal to an array of numbers.

    If you wanted instead to assign all of the red spaces to a variable named "reds", you would do:
    Code:
    const reds = [1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, 36]
    Then if you want to check whether the reds array includes the number the player picked:

    Code:
    const playerPick = $gameVariables.value(38);
    console.log('Is it included? ', reds.includes(playerPick));
    Whether something is included in an array or not is a true/false question - it either is included in the array, or it isn't. So this would be best saved to a switch, which is meant for true/false values.

    The entire code could be:
    Code:
    const playerPick = $gameVariables.value(38);
    const reds = [1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, 36];
    $gameSwitches.setValue(42, reds.includes(playerPick));
    So if there's anything different than odds/evens (like the colors), you can use this suggestion. For even/odd, definitely use the modulo as described above, it's much simpler.

    (As a side-note, if you could use the CODE tags in the future it would make it easier to read the code)
     
    #3
  4. LazESigma

    LazESigma Veteran Veteran

    Messages:
    50
    Likes Received:
    2
    Location:
    Chicagoland
    First Language:
    English
    Thank you both very much for both usage of the modulo operator and for correcting my incorrect code.
    The roulette wheel is up and spinning!
     
    #4

Share This Page