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

LazESigma

Veteran
Veteran
Joined
Nov 9, 2013
Messages
50
Reaction score
2
First Language
English
Primarily Uses
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!
 

Doktor_Q

I'm not a real doktor, but I am a real Q
Veteran
Joined
Aug 1, 2016
Messages
874
Reaction score
562
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.
 

Aloe Guvner

Walrus
Veteran
Joined
Sep 28, 2017
Messages
1,628
Reaction score
1,115
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)
 

LazESigma

Veteran
Veteran
Joined
Nov 9, 2013
Messages
50
Reaction score
2
First Language
English
Primarily Uses
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!
 

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

Latest Threads

Latest Posts

Latest Profile Posts

People3_5 and People3_8 added!

so hopefully tomorrow i get to go home from the hospital i've been here for 5 days already and it's driving me mad. I miss my family like crazy but at least I get to use my own toiletries and my own clothes. My mom is coming to visit soon i can't wait to see her cause i miss her the most. :kaojoy:
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.

Forum statistics

Threads
105,868
Messages
1,017,085
Members
137,583
Latest member
write2dgray
Top